From f65446ec7d815cd429f36c306de5905712ec89af Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:48 +0000 Subject: Utilisateur : Fcolin Date : 2/02/01 Heure : 18:30 Archivé dans $/Ivy Commentaire: win CE compile not finished (vss 6) --- Ivy/ThreadedSocket.h | 120 ++++++++++++++++++++++----------------------------- 1 file changed, 51 insertions(+), 69 deletions(-) (limited to 'Ivy') diff --git a/Ivy/ThreadedSocket.h b/Ivy/ThreadedSocket.h index 829c562..ab8e3cd 100644 --- a/Ivy/ThreadedSocket.h +++ b/Ivy/ThreadedSocket.h @@ -5,22 +5,7 @@ #pragma once -class CThreadedSocketException -{ - -public: -// Constructor - CThreadedSocketException(char* pchMessage); -public: - ~CThreadedSocketException() {} - virtual bool GetErrorMessage(string& lpstrError, unsigned int nMaxError, - unsigned int * pnHelpContext = 0); - int GetError() { return m_nError; }; -private: - int m_nError; - char * m_strMessage; -}; class CThreadedSocket { @@ -30,9 +15,7 @@ public: virtual ~CThreadedSocket(); // Construction public: - void Create(unsigned int nSocketPort = 0, int nSocketType=SOCK_STREAM, - long lEvent = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, - const char * lpszSocketAddress = 0); + int Create(unsigned int nSocketPort = 0, int nSocketType=SOCK_STREAM, const char * lpszSocketAddress = 0); // Attributes public: @@ -46,21 +29,19 @@ public: // Operations public: - void Socket(int nSocketType=SOCK_STREAM, long lEvent = - FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, - int nProtocolType = 0, int nAddressFormat = PF_INET); + int Socket(int nSocketType=SOCK_STREAM, int nProtocolType = 0, int nAddressFormat = PF_INET); - void AddMember( const char * lpszHostAddress ); + int AddMember( const char * lpszHostAddress ); - virtual void Accept(CThreadedSocket& rConnectedSocket, + virtual int Accept(CThreadedSocket& rConnectedSocket, SOCKADDR* lpSockAddr = NULL, int* lpSockAddrLen = NULL); - void Bind(unsigned int nSocketPort, const char * lpszSocketAddress = 0); + int Bind(unsigned int nSocketPort, const char * lpszSocketAddress = 0); virtual void Close(); - bool Connect(const char * lpszHostAddress, unsigned int nHostPort); - bool Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen); + int Connect(const char * lpszHostAddress, unsigned int nHostPort); + int Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen); virtual int Receive(void* lpBuf, int nBufLen, int nFlags = 0); @@ -69,76 +50,67 @@ public: enum { receives = 0, sends = 1, both = 2 }; - virtual void Send(const void* lpBuf, int nBufLen, int nFlags = 0); + virtual int Send(const void* lpBuf, int nBufLen, int nFlags = 0); - void SendTo(const void* lpBuf, int nBufLen, + int SendTo(const void* lpBuf, int nBufLen, unsigned int nHostPort, const char * lpszHostAddress = 0, int nFlags = 0); - void AsyncSelect(long lEvent = - FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE); + inline CThreadedSocket::operator SOCKET() const { return m_hSocket; } -inline void GetPeerName(SOCKADDR* lpSockAddr, int* lpSockAddrLen) +inline int GetPeerName(SOCKADDR* lpSockAddr, int* lpSockAddrLen) { - if ( getpeername(m_hSocket, lpSockAddr, lpSockAddrLen) == SOCKET_ERROR) - throw ( new CThreadedSocketException( "getpeername" ) ); + return getpeername(m_hSocket, lpSockAddr, lpSockAddrLen); } -inline void GetSockName(SOCKADDR* lpSockAddr, int* lpSockAddrLen) - { if ( getsockname(m_hSocket, lpSockAddr, lpSockAddrLen)== SOCKET_ERROR) - throw( new CThreadedSocketException( "getsockname" ) ); +inline int GetSockName(SOCKADDR* lpSockAddr, int* lpSockAddrLen) + { + return getsockname(m_hSocket, lpSockAddr, lpSockAddrLen); } -inline void SetSockOpt(int nOptionName, const void* lpOptionValue, int nOptionLen, int nLevel = SOL_SOCKET) +inline int SetSockOpt(int nOptionName, const void* lpOptionValue, int nOptionLen, int nLevel = SOL_SOCKET) { - if ( setsockopt(m_hSocket, nLevel, nOptionName, (LPCSTR)lpOptionValue, nOptionLen)== SOCKET_ERROR) - throw( new CThreadedSocketException( "setsockopt" ) ); + return setsockopt(m_hSocket, nLevel, nOptionName, (LPCSTR)lpOptionValue, nOptionLen); } -inline void GetSockOpt(int nOptionName, void* lpOptionValue, int* lpOptionLen, int nLevel = SOL_SOCKET) +inline int GetSockOpt(int nOptionName, void* lpOptionValue, int* lpOptionLen, int nLevel = SOL_SOCKET) { - if ( getsockopt(m_hSocket, nLevel, nOptionName, (LPSTR)lpOptionValue, lpOptionLen)== SOCKET_ERROR) - throw( new CThreadedSocketException( "getsockopt" ) ); + return getsockopt(m_hSocket, nLevel, nOptionName, (LPSTR)lpOptionValue, lpOptionLen); } static inline int CThreadedSocket::GetLastError() - { return WSAGetLastError(); } -inline void Bind(const SOCKADDR* lpSockAddr, int nSockAddrLen) + { return ::WSAGetLastError(); } +inline int Bind(const SOCKADDR* lpSockAddr, int nSockAddrLen) { - if ( bind(m_hSocket, lpSockAddr, nSockAddrLen)== SOCKET_ERROR ) - throw( new CThreadedSocketException( "bind" )); + return bind(m_hSocket, lpSockAddr, nSockAddrLen); } -inline void IOCtl(long lCommand, unsigned long* lpArgument) - { if ( ioctlsocket(m_hSocket, lCommand, lpArgument)== SOCKET_ERROR) - throw( new CThreadedSocketException( "bind" ) ); +inline int IOCtl(long lCommand, unsigned long* lpArgument) + { + return ioctlsocket(m_hSocket, lCommand, lpArgument); } -inline void Listen(int nConnectionBacklog=5) +inline int Listen(int nConnectionBacklog=5) { - if ( listen(m_hSocket, nConnectionBacklog)== SOCKET_ERROR) - throw( new CThreadedSocketException( "listen" ) ); + int err = listen(m_hSocket, nConnectionBacklog); + if ( !err ) + listen_mode = true; + return err; } inline int ReceiveFrom(void* lpBuf, int nBufLen, SOCKADDR* lpSockAddr, int* lpSockAddrLen, int nFlags = 0) { int lg = recvfrom(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags, lpSockAddr, lpSockAddrLen); - if ( lg == SOCKET_ERROR ) - throw( new CThreadedSocketException( "recvfrom" ) ); return lg; } -inline bool ShutDown(int nHow = sends) +inline int ShutDown(int nHow = sends) { - if ( shutdown(m_hSocket,nHow) == SOCKET_ERROR ) - throw( new CThreadedSocketException( "shutdown" ) ); + return shutdown(m_hSocket,nHow); } -inline void SendTo(const void* lpBuf, int nBufLen, const SOCKADDR* lpSockAddr, int nSockAddrLen, int nFlags = 0) +inline int SendTo(const void* lpBuf, int nBufLen, const SOCKADDR* lpSockAddr, int nSockAddrLen, int nFlags = 0) { - if ( sendto(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags, lpSockAddr, nSockAddrLen)== SOCKET_ERROR ) - throw( new CThreadedSocketException( "sendto" ) ); + return sendto(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags, lpSockAddr, nSockAddrLen); } - // Overridable callbacks protected: - virtual void OnWakeup(); virtual void OnReceive(int nErrorCode); virtual void OnSend(int nErrorCode); virtual void OnOutOfBandData(int nErrorCode); @@ -146,14 +118,24 @@ protected: virtual void OnConnect(int nErrorCode); virtual void OnClose(int nErrorCode); // Implementation - void StartListener(long lEvent); - long m_EventMask; - unsigned long thread_id; - HANDLE h_thread; - WSAEVENT m_hEvent[2]; // socket event for receive and send actions - unsigned int SocketThread(); //Client Thread + int StartListener(); + int StartWriter(); + bool SignalWriter(); + + HANDLE h_reader; + HANDLE h_writer; + unsigned long reader_id; + unsigned long writer_id; + unsigned int SocketReader(); //Receiver Thread + unsigned int SocketWriter(); //Sender Thread + bool listen_mode; + bool connect_pending; + bool send_pending; + HANDLE send_count; + // wrapper for thread routine -static DWORD WINAPI SocketThreadProc( void * pParam ); +static DWORD WINAPI SocketReaderProc( void * pParam ); +static DWORD WINAPI SocketWriterProc( void * pParam ); }; -- cgit v1.1