From d42a6937e7b9eba39de3c7d8edc975499d1f43ff Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:04:49 +0000 Subject: Utilisateur : Fcolin Date : 16/06/00 Heure : 10:14 Créé (vss 1) --- Ivy/ThreadedSocket.cxx | 439 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 439 insertions(+) create mode 100644 Ivy/ThreadedSocket.cxx (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx new file mode 100644 index 0000000..f00e6f5 --- /dev/null +++ b/Ivy/ThreadedSocket.cxx @@ -0,0 +1,439 @@ +// ThreadedSocket.cpp: implementation of the CThreadedSocket class. +// +////////////////////////////////////////////////////////////////////// + +#include "stdafx.h" +#include + +#include "ThreadedSocket.h" + + +// Class CThreadedSocketException +//IMPLEMENT_DYNAMIC(CThreadedSocketException, CException) + +CThreadedSocketException::CThreadedSocketException(char* pchMessage) +{ + m_strMessage = pchMessage; + m_nError = WSAGetLastError(); +} + +bool CThreadedSocketException::GetErrorMessage(string & lpstrError, UINT nMaxError, + PUINT pnHelpContext /*= NULL*/) +{ + lpstrError = m_strMessage; + lpstrError += "error"; + if(m_nError != 0) + { + lpstrError += " #"; + lpstrError += m_nError; + } + return true; +} + +//IMPLEMENT_DYNAMIC(CThreadedSocket, CObject) + +bool CThreadedSocket::Initialized = false; + +void CThreadedSocket::Init() +{ + WORD wVersionRequested; + WSADATA wsaData; + int err; + + wVersionRequested = MAKEWORD( 2, 2 ); + + err = WSAStartup( wVersionRequested, &wsaData ); + if ( err != 0 ) { + /* Tell the user that we could not find a usable */ + /* WinSock DLL. */ + return; + } + Initialized = true; +} + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +CThreadedSocket::CThreadedSocket() +{ + if ( ! Initialized ) Init(); + + m_hSocket = INVALID_SOCKET; + h_thread = NULL; + // Create events to wait on + // for receivier part + m_hEvent[0] = WSACreateEvent(); + if ( m_hEvent[0] == WSA_INVALID_EVENT ) + { + TRACE( "CThreadedSocketException(WSACreateEvent )\n"); + throw( new CThreadedSocketException( "WSACreateEvent" )); + } + // for transmitter part + m_hEvent[1] = WSACreateEvent(); + if ( m_hEvent[1] == WSA_INVALID_EVENT ) + { + TRACE( "CThreadedSocketException(WSACreateEvent )\n"); + throw( new CThreadedSocketException( "WSACreateEvent" )); + } +} + +CThreadedSocket::~CThreadedSocket() +{ + if (m_hSocket != INVALID_SOCKET) + Close(); + //if ( thread ) // On fait de l'auto delete mais dans le cas de terminaison anormale l'object reste ????!!! + // delete thread; + WSACloseEvent( m_hEvent[0] ); + WSACloseEvent( m_hEvent[1] ); +} +void CThreadedSocket::Create(UINT nSocketPort, int nSocketType, + long lEvent, const char * lpszSocketAddress) +{ + Socket(nSocketType, lEvent); + Bind(nSocketPort,lpszSocketAddress); +} + +///////////////////////////////////////////////////////////////////////////// +// CThreadedSocket Attributes + + + +void CThreadedSocket::GetPeerName(string & rPeerAddress, UINT& rPeerPort) +{ + SOCKADDR_IN sockAddr; + memset(&sockAddr, 0, sizeof(sockAddr)); + + int nSockAddrLen = sizeof(sockAddr); + GetPeerName((SOCKADDR*)&sockAddr, &nSockAddrLen); + rPeerPort = ntohs(sockAddr.sin_port); + rPeerAddress = inet_ntoa(sockAddr.sin_addr); +} + +void CThreadedSocket::GetSockName(string & rSocketAddress, UINT& rSocketPort) +{ + SOCKADDR_IN sockAddr; + memset(&sockAddr, 0, sizeof(sockAddr)); + + int nSockAddrLen = sizeof(sockAddr); + GetSockName((SOCKADDR*)&sockAddr, &nSockAddrLen); + rSocketPort = ntohs(sockAddr.sin_port); + rSocketAddress = inet_ntoa(sockAddr.sin_addr); +} + +///////////////////////////////////////////////////////////////////////////// +// CAscynSocket Operations + +void CThreadedSocket::Accept(CThreadedSocket& rConnectedSocket, + SOCKADDR* lpSockAddr, int* lpSockAddrLen) +{ + assert(rConnectedSocket.m_hSocket == INVALID_SOCKET); + + + SOCKET hTemp = accept(m_hSocket, lpSockAddr, lpSockAddrLen); + + if (hTemp == INVALID_SOCKET) + { + rConnectedSocket.m_hSocket = INVALID_SOCKET; + throw( new CThreadedSocketException( "accept" ) ); + } + else + { + rConnectedSocket.m_hSocket = hTemp; + rConnectedSocket.StartListener(m_EventMask); + } +} + +void CThreadedSocket::Bind(UINT nSocketPort, const char * lpszSocketAddress) +{ + + SOCKADDR_IN sockAddr; + memset(&sockAddr,0,sizeof(sockAddr)); + + sockAddr.sin_family = AF_INET; + + if (lpszSocketAddress == NULL) + sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); + else + { + DWORD lResult = inet_addr(lpszSocketAddress); + if (lResult == INADDR_NONE) + { + WSASetLastError(WSAEINVAL); + throw( new CThreadedSocketException( "bind" ) ); + } + sockAddr.sin_addr.s_addr = lResult; + } + + sockAddr.sin_port = htons((u_short)nSocketPort); + + Bind((SOCKADDR*)&sockAddr, sizeof(sockAddr)); +} + +void CThreadedSocket::Close() +{ + if (m_hSocket != INVALID_SOCKET) + { + AsyncSelect(0); + assert(SOCKET_ERROR != closesocket(m_hSocket)); + m_hSocket = INVALID_SOCKET; + //WSASetEvent( m_hEventObject ); + } +} + +bool CThreadedSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen) +{ + if ( !connect(m_hSocket, lpSockAddr, nSockAddrLen) ) + { + DWORD err = WSAGetLastError(); + if ( err == 0 ) return TRUE; + TRACE( "***************************************Error connect %d\n", err); + if ( err != WSAEWOULDBLOCK ) + return FALSE; + // Wait for connection to complete + TRACE( "***************************************Waiting for connection to complete\n"); + err = WSAWaitForMultipleEvents( 1, m_hEvent, FALSE, WSA_INFINITE, TRUE); + //TRACE( "Thread socket %d wait return %d\n", m_hSocket, err ); + if ( err == WSA_WAIT_FAILED ) + { + return FALSE; + } + + return TRUE; + } + return TRUE; +} + +bool CThreadedSocket::Connect(const char * lpszHostAddress, UINT nHostPort) +{ + + assert(lpszHostAddress != NULL); + + SOCKADDR_IN sockAddr; + memset(&sockAddr,0,sizeof(sockAddr)); + + sockAddr.sin_family = AF_INET; + sockAddr.sin_addr.s_addr = inet_addr(lpszHostAddress); + + if (sockAddr.sin_addr.s_addr == INADDR_NONE) + { + LPHOSTENT lphost; + lphost = gethostbyname(lpszHostAddress); + if (lphost != NULL) + sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr; + else + { + WSASetLastError(WSAEINVAL); + return FALSE; + } + } + + sockAddr.sin_port = htons((u_short)nHostPort); + + return Connect((SOCKADDR*)&sockAddr, sizeof(sockAddr)); +} + +int CThreadedSocket::Receive(void* lpBuf, int nBufLen, int nFlags) +{ + int lg = recv(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags); + if ( lg == SOCKET_ERROR ) + throw( new CThreadedSocketException( "recv" ) ); + return lg; +} + +int CThreadedSocket::ReceiveFrom(void* lpBuf, int nBufLen, string & rSocketAddress, UINT& rSocketPort, int nFlags) +{ + SOCKADDR_IN sockAddr; + + memset(&sockAddr, 0, sizeof(sockAddr)); + + int nSockAddrLen = sizeof(sockAddr); + int nResult = ReceiveFrom(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, &nSockAddrLen, nFlags); + rSocketPort = ntohs(sockAddr.sin_port); + rSocketAddress = inet_ntoa(sockAddr.sin_addr); + + return nResult; +} + +void CThreadedSocket::Send(const void* lpBuf, int nBufLen, int nFlags) +{ + if ( send(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags) != nBufLen ) + throw( new CThreadedSocketException( "send" ) ); +} + +void CThreadedSocket::SendTo(const void* lpBuf, int nBufLen, UINT nHostPort, const char * lpszHostAddress, int nFlags) +{ + SOCKADDR_IN sockAddr; + + memset(&sockAddr,0,sizeof(sockAddr)); + + sockAddr.sin_family = AF_INET; + + if (lpszHostAddress == NULL) + sockAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST); + else + { + sockAddr.sin_addr.s_addr = inet_addr(lpszHostAddress); + if (sockAddr.sin_addr.s_addr == INADDR_NONE) + { + LPHOSTENT lphost; + lphost = gethostbyname(lpszHostAddress); + if (lphost != NULL) + sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr; + else + { + WSASetLastError(WSAEINVAL); + throw( new CThreadedSocketException( "SendTo" )); + return; + } + } + } + + sockAddr.sin_port = htons((u_short)nHostPort); + + SendTo(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, sizeof(sockAddr), nFlags); +} +void CThreadedSocket::AsyncSelect(long lEvent) +{ + assert(m_hSocket != INVALID_SOCKET); + if ( WSAEventSelect(m_hSocket, m_hEvent[0], lEvent) ) + throw( new CThreadedSocketException( "WSAEventSelect" )); +} +///////////////////////////////////////////////////////////////////////////// +// CThreadedSocket Overridable callbacks +void CThreadedSocket::OnWakeup() +{ +} +void CThreadedSocket::OnReceive(int /*nErrorCode*/) +{ +} + +void CThreadedSocket::OnSend(int /*nErrorCode*/) +{ +} + +void CThreadedSocket::OnOutOfBandData(int /*nErrorCode*/) +{ +} + +void CThreadedSocket::OnAccept(int /*nErrorCode*/) +{ +} + +void CThreadedSocket::OnConnect(int /*nErrorCode*/) +{ +} + +void CThreadedSocket::OnClose(int /*nErrorCode*/) +{ +} + +///////////////////////////////////////////////////////////////////////////// +// CThreadedSocket Implementation + + +void CThreadedSocket::Socket(int nSocketType, long lEvent, int nProtocolType, int nAddressFormat) +{ + assert(m_hSocket == INVALID_SOCKET); + + m_hSocket = socket(nAddressFormat,nSocketType,nProtocolType); +// m_hSocket = WSASocket ( nAddressFormat, nSocketType,nProtocolType, NULL, 0, 0 ); + + + if (m_hSocket == INVALID_SOCKET) + { + throw( new CThreadedSocketException( "socket" )); + } + StartListener(lEvent); + +} + +void CThreadedSocket::StartListener(long lEvent) +{ + m_EventMask = lEvent; + h_thread = CreateThread(NULL,0,SocketThreadProc, this, 0, &thread_id); + if ( ! h_thread ) + throw( new CThreadedSocketException( "socket listener" )); +} +// Implementation +DWORD WINAPI CThreadedSocket::SocketThreadProc( LPVOID pParam ) +{ + CThreadedSocket* pObject = (CThreadedSocket*)pParam; + + if (pObject == NULL ) + { + throw( new CThreadedSocketException( "bad socket proc" )); + return -1; // if pObject is not valid + } + + return pObject->SocketThread(); +} + +UINT CThreadedSocket::SocketThread( ) +{ + DWORD err; + WSANETWORKEVENTS NetworkEvents; + + AsyncSelect(m_EventMask) ; + while ( m_hSocket != INVALID_SOCKET ) + { + err = WSAWaitForMultipleEvents( 2, m_hEvent, FALSE, WSA_INFINITE, TRUE); + switch ( err ) + { + case WSA_WAIT_FAILED: + TRACE( "CThreadedSocketException(WSAWaitForMultipleEvents )\n"); + throw( new CThreadedSocketException( "WSAWaitForMultipleEvents" )); + return err; + break; + case WSA_WAIT_EVENT_0: + + NetworkEvents.lNetworkEvents=0; + + if ( WSAEnumNetworkEvents ( m_hSocket, m_hEvent[0], &NetworkEvents )) + { + TRACE( "CThreadedSocketException(WSAEnumNetworkEvents )\n"); + throw( new CThreadedSocketException( "WSAEnumNetworkEvents" )); + return -1; + } + + + if ( NetworkEvents.lNetworkEvents & FD_READ ) + OnReceive(NetworkEvents.iErrorCode[ FD_READ_BIT ]); + + if ( NetworkEvents.lNetworkEvents & FD_WRITE ) + OnSend( NetworkEvents.iErrorCode[ FD_WRITE_BIT ] ); + + if ( NetworkEvents.lNetworkEvents & FD_ACCEPT ) + OnAccept( NetworkEvents.iErrorCode[ FD_ACCEPT_BIT ] ); + + + if ( NetworkEvents.lNetworkEvents & FD_CONNECT ) + OnConnect( NetworkEvents.iErrorCode[ FD_CONNECT_BIT ] ); + + if ( NetworkEvents.lNetworkEvents & FD_OOB ) + OnOutOfBandData( NetworkEvents.iErrorCode[ FD_OOB_BIT ] ); + + if ( NetworkEvents.lNetworkEvents & FD_CLOSE ) + { + OnClose( NetworkEvents.iErrorCode[ FD_CLOSE_BIT ] ); + } + + break; + case WSA_WAIT_EVENT_0 +1 : + if ( m_hSocket != INVALID_SOCKET ) + { + OnWakeup(); + } + // else Close by other Thread do silent terminaison + WSAResetEvent( m_hEvent[1] ); + + break; + default: + TRACE( "CThreadedSocket::SocketThread Unhandled Events !\n"); + break; + } + } + Close(); + h_thread = NULL; + return 0; +} -- cgit v1.1 From ff43d6adcef7ed20cf896553ae050f9ee70da412 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:04:50 +0000 Subject: Utilisateur : Fcolin Date : 16/06/00 Heure : 10:40 Archivé dans $/Ivy Commentaire: Init dll socket dans DLLMain (vss 2) --- Ivy/ThreadedSocket.cxx | 22 ---------------------- 1 file changed, 22 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index f00e6f5..64df41e 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -30,34 +30,12 @@ bool CThreadedSocketException::GetErrorMessage(string & lpstrError, UINT nMaxErr return true; } -//IMPLEMENT_DYNAMIC(CThreadedSocket, CObject) - -bool CThreadedSocket::Initialized = false; - -void CThreadedSocket::Init() -{ - WORD wVersionRequested; - WSADATA wsaData; - int err; - - wVersionRequested = MAKEWORD( 2, 2 ); - - err = WSAStartup( wVersionRequested, &wsaData ); - if ( err != 0 ) { - /* Tell the user that we could not find a usable */ - /* WinSock DLL. */ - return; - } - Initialized = true; -} - ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CThreadedSocket::CThreadedSocket() { - if ( ! Initialized ) Init(); m_hSocket = INVALID_SOCKET; h_thread = NULL; -- cgit v1.1 From f2abd2f272583782738a190c0d59a05e86a8198a Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:04:52 +0000 Subject: Utilisateur : Fcolin Date : 29/06/00 Heure : 15:59 Archivé dans $/Ivy Commentaire: Version multicast (vss 3) --- Ivy/ThreadedSocket.cxx | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 64df41e..1e20e8e 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -242,7 +242,7 @@ void CThreadedSocket::Send(const void* lpBuf, int nBufLen, int nFlags) void CThreadedSocket::SendTo(const void* lpBuf, int nBufLen, UINT nHostPort, const char * lpszHostAddress, int nFlags) { SOCKADDR_IN sockAddr; - + memset(&sockAddr,0,sizeof(sockAddr)); sockAddr.sin_family = AF_INET; @@ -266,11 +266,44 @@ void CThreadedSocket::SendTo(const void* lpBuf, int nBufLen, UINT nHostPort, con } } } - + sockAddr.sin_port = htons((u_short)nHostPort); SendTo(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, sizeof(sockAddr), nFlags); } +void CThreadedSocket::AddMember( const char * lpszHostAddress ) +{ + int multicast_ttl = 64; // region + struct ip_mreq imr; + + TRACE("CThreadedSocket::AddMember adding %s\n", lpszHostAddress ); + imr.imr_multiaddr.s_addr = INADDR_ANY; + imr.imr_interface.s_addr = INADDR_ANY; + + if (lpszHostAddress ) + { + imr.imr_multiaddr.s_addr = inet_addr(lpszHostAddress); + if (imr.imr_multiaddr.s_addr == INADDR_NONE) + { + LPHOSTENT lphost; + lphost = gethostbyname(lpszHostAddress); + if (lphost != NULL) + imr.imr_multiaddr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr; + else + { + WSASetLastError(WSAEINVAL); + throw( new CThreadedSocketException( "AddMenber" )); + return; + } + } + } + if ( IN_CLASSD( htonl(imr.imr_multiaddr.s_addr) ) ) + { + SetSockOpt( IP_MULTICAST_TTL, &multicast_ttl, sizeof( multicast_ttl ), IPPROTO_IP ); + SetSockOpt( IP_ADD_MEMBERSHIP, &imr, sizeof( imr ), IPPROTO_IP ); + } + +} void CThreadedSocket::AsyncSelect(long lEvent) { assert(m_hSocket != INVALID_SOCKET); -- cgit v1.1 From cc506f8b04ce0506c75321b4d5a25da3ca550876 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:04:54 +0000 Subject: Utilisateur : Fcolin Date : 23/01/01 Heure : 13:12 Archivé dans $/Ivy Commentaire: remove 'using name space std;' (vss 4) --- Ivy/ThreadedSocket.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 1e20e8e..5f4c183 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -17,7 +17,7 @@ CThreadedSocketException::CThreadedSocketException(char* pchMessage) m_nError = WSAGetLastError(); } -bool CThreadedSocketException::GetErrorMessage(string & lpstrError, UINT nMaxError, +bool CThreadedSocketException::GetErrorMessage(String & lpstrError, UINT nMaxError, PUINT pnHelpContext /*= NULL*/) { lpstrError = m_strMessage; @@ -77,7 +77,7 @@ void CThreadedSocket::Create(UINT nSocketPort, int nSocketType, -void CThreadedSocket::GetPeerName(string & rPeerAddress, UINT& rPeerPort) +void CThreadedSocket::GetPeerName(String & rPeerAddress, UINT& rPeerPort) { SOCKADDR_IN sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); @@ -88,7 +88,7 @@ void CThreadedSocket::GetPeerName(string & rPeerAddress, UINT& rPeerPort) rPeerAddress = inet_ntoa(sockAddr.sin_addr); } -void CThreadedSocket::GetSockName(string & rSocketAddress, UINT& rSocketPort) +void CThreadedSocket::GetSockName(String & rSocketAddress, UINT& rSocketPort) { SOCKADDR_IN sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); @@ -219,7 +219,7 @@ int CThreadedSocket::Receive(void* lpBuf, int nBufLen, int nFlags) return lg; } -int CThreadedSocket::ReceiveFrom(void* lpBuf, int nBufLen, string & rSocketAddress, UINT& rSocketPort, int nFlags) +int CThreadedSocket::ReceiveFrom(void* lpBuf, int nBufLen, String & rSocketAddress, UINT& rSocketPort, int nFlags) { SOCKADDR_IN sockAddr; -- cgit v1.1 From 7c626ebb6751ab4dc1a86d1939cded83dfea70bd Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:04:56 +0000 Subject: Utilisateur : Fcolin Date : 31/01/01 Heure : 11:18 Archivé dans $/Ivy (vss 5) --- Ivy/ThreadedSocket.cxx | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 5f4c183..955af12 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -8,16 +8,13 @@ #include "ThreadedSocket.h" -// Class CThreadedSocketException -//IMPLEMENT_DYNAMIC(CThreadedSocketException, CException) - CThreadedSocketException::CThreadedSocketException(char* pchMessage) { m_strMessage = pchMessage; m_nError = WSAGetLastError(); } -bool CThreadedSocketException::GetErrorMessage(String & lpstrError, UINT nMaxError, +bool CThreadedSocketException::GetErrorMessage(string & lpstrError, UINT nMaxError, PUINT pnHelpContext /*= NULL*/) { lpstrError = m_strMessage; @@ -77,7 +74,7 @@ void CThreadedSocket::Create(UINT nSocketPort, int nSocketType, -void CThreadedSocket::GetPeerName(String & rPeerAddress, UINT& rPeerPort) +void CThreadedSocket::GetPeerName(string & rPeerAddress, UINT& rPeerPort) { SOCKADDR_IN sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); @@ -88,7 +85,7 @@ void CThreadedSocket::GetPeerName(String & rPeerAddress, UINT& rPeerPort) rPeerAddress = inet_ntoa(sockAddr.sin_addr); } -void CThreadedSocket::GetSockName(String & rSocketAddress, UINT& rSocketPort) +void CThreadedSocket::GetSockName(string & rSocketAddress, UINT& rSocketPort) { SOCKADDR_IN sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); @@ -105,7 +102,7 @@ void CThreadedSocket::GetSockName(String & rSocketAddress, UINT& rSocketPort) void CThreadedSocket::Accept(CThreadedSocket& rConnectedSocket, SOCKADDR* lpSockAddr, int* lpSockAddrLen) { - assert(rConnectedSocket.m_hSocket == INVALID_SOCKET); + ASSERT(rConnectedSocket.m_hSocket == INVALID_SOCKET); SOCKET hTemp = accept(m_hSocket, lpSockAddr, lpSockAddrLen); @@ -153,7 +150,7 @@ void CThreadedSocket::Close() if (m_hSocket != INVALID_SOCKET) { AsyncSelect(0); - assert(SOCKET_ERROR != closesocket(m_hSocket)); + ASSERT(SOCKET_ERROR != closesocket(m_hSocket)); m_hSocket = INVALID_SOCKET; //WSASetEvent( m_hEventObject ); } @@ -164,28 +161,28 @@ bool CThreadedSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen) if ( !connect(m_hSocket, lpSockAddr, nSockAddrLen) ) { DWORD err = WSAGetLastError(); - if ( err == 0 ) return TRUE; + if ( err == 0 ) return true; TRACE( "***************************************Error connect %d\n", err); if ( err != WSAEWOULDBLOCK ) - return FALSE; + return false; // Wait for connection to complete TRACE( "***************************************Waiting for connection to complete\n"); - err = WSAWaitForMultipleEvents( 1, m_hEvent, FALSE, WSA_INFINITE, TRUE); + err = WSAWaitForMultipleEvents( 1, m_hEvent, false, WSA_INFINITE, true); //TRACE( "Thread socket %d wait return %d\n", m_hSocket, err ); if ( err == WSA_WAIT_FAILED ) { - return FALSE; + return false; } - return TRUE; + return true; } - return TRUE; + return true; } bool CThreadedSocket::Connect(const char * lpszHostAddress, UINT nHostPort) { - assert(lpszHostAddress != NULL); + ASSERT(lpszHostAddress != NULL); SOCKADDR_IN sockAddr; memset(&sockAddr,0,sizeof(sockAddr)); @@ -202,7 +199,7 @@ bool CThreadedSocket::Connect(const char * lpszHostAddress, UINT nHostPort) else { WSASetLastError(WSAEINVAL); - return FALSE; + return false; } } @@ -219,7 +216,7 @@ int CThreadedSocket::Receive(void* lpBuf, int nBufLen, int nFlags) return lg; } -int CThreadedSocket::ReceiveFrom(void* lpBuf, int nBufLen, String & rSocketAddress, UINT& rSocketPort, int nFlags) +int CThreadedSocket::ReceiveFrom(void* lpBuf, int nBufLen, string & rSocketAddress, UINT& rSocketPort, int nFlags) { SOCKADDR_IN sockAddr; @@ -306,7 +303,7 @@ void CThreadedSocket::AddMember( const char * lpszHostAddress ) } void CThreadedSocket::AsyncSelect(long lEvent) { - assert(m_hSocket != INVALID_SOCKET); + ASSERT(m_hSocket != INVALID_SOCKET); if ( WSAEventSelect(m_hSocket, m_hEvent[0], lEvent) ) throw( new CThreadedSocketException( "WSAEventSelect" )); } @@ -345,7 +342,7 @@ void CThreadedSocket::OnClose(int /*nErrorCode*/) void CThreadedSocket::Socket(int nSocketType, long lEvent, int nProtocolType, int nAddressFormat) { - assert(m_hSocket == INVALID_SOCKET); + ASSERT(m_hSocket == INVALID_SOCKET); m_hSocket = socket(nAddressFormat,nSocketType,nProtocolType); // m_hSocket = WSASocket ( nAddressFormat, nSocketType,nProtocolType, NULL, 0, 0 ); @@ -388,7 +385,7 @@ UINT CThreadedSocket::SocketThread( ) AsyncSelect(m_EventMask) ; while ( m_hSocket != INVALID_SOCKET ) { - err = WSAWaitForMultipleEvents( 2, m_hEvent, FALSE, WSA_INFINITE, TRUE); + err = WSAWaitForMultipleEvents( 2, m_hEvent, false, WSA_INFINITE, true); switch ( err ) { case WSA_WAIT_FAILED: -- cgit v1.1 From cf2e43e95c4eb20314f627bd473e4c99d3c3c9e3 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:04:58 +0000 Subject: Utilisateur : Fcolin Date : 2/02/01 Heure : 18:30 Archivé dans $/Ivy Commentaire: win CE compile not finished (vss 6) --- Ivy/ThreadedSocket.cxx | 357 +++++++++++++++++++++++++++++-------------------- 1 file changed, 209 insertions(+), 148 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 955af12..7f36f07 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -3,29 +3,15 @@ ////////////////////////////////////////////////////////////////////// #include "stdafx.h" -#include + +#ifdef _DEBUG +#define DEBUG_NEW new(__FILE__, __LINE__) +#define new DEBUG_NEW +#endif #include "ThreadedSocket.h" -CThreadedSocketException::CThreadedSocketException(char* pchMessage) -{ - m_strMessage = pchMessage; - m_nError = WSAGetLastError(); -} - -bool CThreadedSocketException::GetErrorMessage(string & lpstrError, UINT nMaxError, - PUINT pnHelpContext /*= NULL*/) -{ - lpstrError = m_strMessage; - lpstrError += "error"; - if(m_nError != 0) - { - lpstrError += " #"; - lpstrError += m_nError; - } - return true; -} ////////////////////////////////////////////////////////////////////// // Construction/Destruction @@ -35,22 +21,11 @@ CThreadedSocket::CThreadedSocket() { m_hSocket = INVALID_SOCKET; - h_thread = NULL; - // Create events to wait on - // for receivier part - m_hEvent[0] = WSACreateEvent(); - if ( m_hEvent[0] == WSA_INVALID_EVENT ) - { - TRACE( "CThreadedSocketException(WSACreateEvent )\n"); - throw( new CThreadedSocketException( "WSACreateEvent" )); - } - // for transmitter part - m_hEvent[1] = WSACreateEvent(); - if ( m_hEvent[1] == WSA_INVALID_EVENT ) - { - TRACE( "CThreadedSocketException(WSACreateEvent )\n"); - throw( new CThreadedSocketException( "WSACreateEvent" )); - } + h_reader = NULL; + h_writer = NULL; + listen_mode = false; + connect_pending = true; + send_pending = false; } CThreadedSocket::~CThreadedSocket() @@ -59,14 +34,17 @@ CThreadedSocket::~CThreadedSocket() Close(); //if ( thread ) // On fait de l'auto delete mais dans le cas de terminaison anormale l'object reste ????!!! // delete thread; - WSACloseEvent( m_hEvent[0] ); - WSACloseEvent( m_hEvent[1] ); } -void CThreadedSocket::Create(UINT nSocketPort, int nSocketType, - long lEvent, const char * lpszSocketAddress) +int CThreadedSocket::Create(UINT nSocketPort, int nSocketType, const char * lpszSocketAddress) { - Socket(nSocketType, lEvent); - Bind(nSocketPort,lpszSocketAddress); + if ( Socket(nSocketType) < 0 ) + return SOCKET_ERROR; + + send_count = CreateSemaphore( NULL, 0, 100, NULL); // unnamed semaphore + if (send_count == NULL) + return SOCKET_ERROR; + + return Bind(nSocketPort,lpszSocketAddress); } ///////////////////////////////////////////////////////////////////////////// @@ -99,27 +77,31 @@ void CThreadedSocket::GetSockName(string & rSocketAddress, UINT& rSocketPort) ///////////////////////////////////////////////////////////////////////////// // CAscynSocket Operations -void CThreadedSocket::Accept(CThreadedSocket& rConnectedSocket, +int CThreadedSocket::Accept(CThreadedSocket& rConnectedSocket, SOCKADDR* lpSockAddr, int* lpSockAddrLen) { ASSERT(rConnectedSocket.m_hSocket == INVALID_SOCKET); - SOCKET hTemp = accept(m_hSocket, lpSockAddr, lpSockAddrLen); if (hTemp == INVALID_SOCKET) { rConnectedSocket.m_hSocket = INVALID_SOCKET; - throw( new CThreadedSocketException( "accept" ) ); + return SOCKET_ERROR; } else { rConnectedSocket.m_hSocket = hTemp; - rConnectedSocket.StartListener(m_EventMask); + rConnectedSocket.send_count = CreateSemaphore( NULL, 0, 100, NULL); // unnamed semaphore + if (rConnectedSocket.send_count == NULL) + return SOCKET_ERROR; + + rConnectedSocket.StartListener(); + return hTemp; } } -void CThreadedSocket::Bind(UINT nSocketPort, const char * lpszSocketAddress) +int CThreadedSocket::Bind(UINT nSocketPort, const char * lpszSocketAddress) { SOCKADDR_IN sockAddr; @@ -135,51 +117,57 @@ void CThreadedSocket::Bind(UINT nSocketPort, const char * lpszSocketAddress) if (lResult == INADDR_NONE) { WSASetLastError(WSAEINVAL); - throw( new CThreadedSocketException( "bind" ) ); + return SOCKET_ERROR; } sockAddr.sin_addr.s_addr = lResult; } sockAddr.sin_port = htons((u_short)nSocketPort); - Bind((SOCKADDR*)&sockAddr, sizeof(sockAddr)); + return Bind((SOCKADDR*)&sockAddr, sizeof(sockAddr)); } void CThreadedSocket::Close() { if (m_hSocket != INVALID_SOCKET) { - AsyncSelect(0); ASSERT(SOCKET_ERROR != closesocket(m_hSocket)); m_hSocket = INVALID_SOCKET; - //WSASetEvent( m_hEventObject ); + } } -bool CThreadedSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen) +int CThreadedSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen) { - if ( !connect(m_hSocket, lpSockAddr, nSockAddrLen) ) + int ok; + fd_set writefds; + + FD_ZERO(&writefds); + + FD_SET(m_hSocket, &writefds); + + ok = connect(m_hSocket, lpSockAddr, nSockAddrLen); + if ( !ok ) { - DWORD err = WSAGetLastError(); - if ( err == 0 ) return true; + int err = GetLastError(); + if ( err == 0 ) return err; TRACE( "***************************************Error connect %d\n", err); if ( err != WSAEWOULDBLOCK ) - return false; + return -1; // Wait for connection to complete TRACE( "***************************************Waiting for connection to complete\n"); - err = WSAWaitForMultipleEvents( 1, m_hEvent, false, WSA_INFINITE, true); + FD_SET(m_hSocket, &writefds); + err = select( 1, NULL, &writefds, NULL, NULL ); //TRACE( "Thread socket %d wait return %d\n", m_hSocket, err ); - if ( err == WSA_WAIT_FAILED ) - { - return false; - } - - return true; + if ( !err ) + connect_pending = true; + return err; } - return true; + connect_pending = true; + return 0; } -bool CThreadedSocket::Connect(const char * lpszHostAddress, UINT nHostPort) +int CThreadedSocket::Connect(const char * lpszHostAddress, UINT nHostPort) { ASSERT(lpszHostAddress != NULL); @@ -199,7 +187,7 @@ bool CThreadedSocket::Connect(const char * lpszHostAddress, UINT nHostPort) else { WSASetLastError(WSAEINVAL); - return false; + return SOCKET_ERROR; } } @@ -211,8 +199,6 @@ bool CThreadedSocket::Connect(const char * lpszHostAddress, UINT nHostPort) int CThreadedSocket::Receive(void* lpBuf, int nBufLen, int nFlags) { int lg = recv(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags); - if ( lg == SOCKET_ERROR ) - throw( new CThreadedSocketException( "recv" ) ); return lg; } @@ -230,13 +216,12 @@ int CThreadedSocket::ReceiveFrom(void* lpBuf, int nBufLen, string & rSocketAddre return nResult; } -void CThreadedSocket::Send(const void* lpBuf, int nBufLen, int nFlags) +int CThreadedSocket::Send(const void* lpBuf, int nBufLen, int nFlags) { - if ( send(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags) != nBufLen ) - throw( new CThreadedSocketException( "send" ) ); + return send(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags); } -void CThreadedSocket::SendTo(const void* lpBuf, int nBufLen, UINT nHostPort, const char * lpszHostAddress, int nFlags) +int CThreadedSocket::SendTo(const void* lpBuf, int nBufLen, UINT nHostPort, const char * lpszHostAddress, int nFlags) { SOCKADDR_IN sockAddr; @@ -258,17 +243,16 @@ void CThreadedSocket::SendTo(const void* lpBuf, int nBufLen, UINT nHostPort, con else { WSASetLastError(WSAEINVAL); - throw( new CThreadedSocketException( "SendTo" )); - return; + return SOCKET_ERROR; } } } sockAddr.sin_port = htons((u_short)nHostPort); - SendTo(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, sizeof(sockAddr), nFlags); + return SendTo(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, sizeof(sockAddr), nFlags); } -void CThreadedSocket::AddMember( const char * lpszHostAddress ) +int CThreadedSocket::AddMember( const char * lpszHostAddress ) { int multicast_ttl = 64; // region struct ip_mreq imr; @@ -289,8 +273,7 @@ void CThreadedSocket::AddMember( const char * lpszHostAddress ) else { WSASetLastError(WSAEINVAL); - throw( new CThreadedSocketException( "AddMenber" )); - return; + return SOCKET_ERROR; } } } @@ -299,19 +282,11 @@ void CThreadedSocket::AddMember( const char * lpszHostAddress ) SetSockOpt( IP_MULTICAST_TTL, &multicast_ttl, sizeof( multicast_ttl ), IPPROTO_IP ); SetSockOpt( IP_ADD_MEMBERSHIP, &imr, sizeof( imr ), IPPROTO_IP ); } - -} -void CThreadedSocket::AsyncSelect(long lEvent) -{ - ASSERT(m_hSocket != INVALID_SOCKET); - if ( WSAEventSelect(m_hSocket, m_hEvent[0], lEvent) ) - throw( new CThreadedSocketException( "WSAEventSelect" )); + return 0; } + ///////////////////////////////////////////////////////////////////////////// // CThreadedSocket Overridable callbacks -void CThreadedSocket::OnWakeup() -{ -} void CThreadedSocket::OnReceive(int /*nErrorCode*/) { } @@ -340,108 +315,194 @@ void CThreadedSocket::OnClose(int /*nErrorCode*/) // CThreadedSocket Implementation -void CThreadedSocket::Socket(int nSocketType, long lEvent, int nProtocolType, int nAddressFormat) +int CThreadedSocket::Socket(int nSocketType, int nProtocolType, int nAddressFormat) { ASSERT(m_hSocket == INVALID_SOCKET); - m_hSocket = socket(nAddressFormat,nSocketType,nProtocolType); -// m_hSocket = WSASocket ( nAddressFormat, nSocketType,nProtocolType, NULL, 0, 0 ); - + m_hSocket = socket(nAddressFormat,nSocketType,nProtocolType); - if (m_hSocket == INVALID_SOCKET) + if ( m_hSocket < 0 ) { - throw( new CThreadedSocketException( "socket" )); + return m_hSocket; } - StartListener(lEvent); + return StartListener(); +} +int CThreadedSocket::StartListener() +{ + h_reader = CreateThread(NULL,0,SocketReaderProc, this, 0, &reader_id); + return h_reader != NULL ? 0 : SOCKET_ERROR; +} +int CThreadedSocket::StartWriter() +{ + h_writer = CreateThread(NULL,0,SocketWriterProc, this, 0, &writer_id); + return h_writer != NULL ? 0 : SOCKET_ERROR; +} +bool CThreadedSocket::SignalWriter() +{ + long PreviousCount = 0; + int err; + err = ReleaseSemaphore(send_count, 1, &PreviousCount); + TRACE("CThreadedSocket::SignalWriter() PreviousCount = %ld \n", PreviousCount ); + return (err != 0); } -void CThreadedSocket::StartListener(long lEvent) +// Implementation +DWORD WINAPI CThreadedSocket::SocketReaderProc( LPVOID pParam ) { - m_EventMask = lEvent; - h_thread = CreateThread(NULL,0,SocketThreadProc, this, 0, &thread_id); - if ( ! h_thread ) - throw( new CThreadedSocketException( "socket listener" )); + CThreadedSocket* pObject = (CThreadedSocket*)pParam; + + if (pObject == NULL ) + { + return SOCKET_ERROR; // if pObject is not valid + } + + return pObject->SocketReader(); } // Implementation -DWORD WINAPI CThreadedSocket::SocketThreadProc( LPVOID pParam ) +DWORD WINAPI CThreadedSocket::SocketWriterProc( LPVOID pParam ) { CThreadedSocket* pObject = (CThreadedSocket*)pParam; if (pObject == NULL ) { - throw( new CThreadedSocketException( "bad socket proc" )); - return -1; // if pObject is not valid + return SOCKET_ERROR; // if pObject is not valid } - return pObject->SocketThread(); + return pObject->SocketWriter(); } -UINT CThreadedSocket::SocketThread( ) +UINT CThreadedSocket::SocketReader( ) { - DWORD err; - WSANETWORKEVENTS NetworkEvents; + int err; + int sock_err; + unsigned long readcount; + fd_set readfds; + fd_set writefds; + fd_set exceptfds; + + FD_ZERO(&readfds); + FD_ZERO(&writefds); + FD_ZERO(&exceptfds); - AsyncSelect(m_EventMask) ; while ( m_hSocket != INVALID_SOCKET ) { - err = WSAWaitForMultipleEvents( 2, m_hEvent, false, WSA_INFINITE, true); + FD_SET(m_hSocket, &readfds); + if ( connect_pending ) + FD_SET(m_hSocket, &writefds); + FD_SET(m_hSocket, &exceptfds); + + err = select( 1, &readfds, &writefds, &exceptfds, NULL ); + sock_err = GetLastError(); + switch ( err ) { - case WSA_WAIT_FAILED: - TRACE( "CThreadedSocketException(WSAWaitForMultipleEvents )\n"); - throw( new CThreadedSocketException( "WSAWaitForMultipleEvents" )); - return err; + case 0: + TRACE( "CThreadedSocket::SocketThread Unhandled Timeout Event !\n"); break; - case WSA_WAIT_EVENT_0: - - NetworkEvents.lNetworkEvents=0; - - if ( WSAEnumNetworkEvents ( m_hSocket, m_hEvent[0], &NetworkEvents )) - { - TRACE( "CThreadedSocketException(WSAEnumNetworkEvents )\n"); - throw( new CThreadedSocketException( "WSAEnumNetworkEvents" )); - return -1; - } + case SOCKET_ERROR: - - if ( NetworkEvents.lNetworkEvents & FD_READ ) - OnReceive(NetworkEvents.iErrorCode[ FD_READ_BIT ]); - - if ( NetworkEvents.lNetworkEvents & FD_WRITE ) - OnSend( NetworkEvents.iErrorCode[ FD_WRITE_BIT ] ); - - if ( NetworkEvents.lNetworkEvents & FD_ACCEPT ) - OnAccept( NetworkEvents.iErrorCode[ FD_ACCEPT_BIT ] ); - - - if ( NetworkEvents.lNetworkEvents & FD_CONNECT ) - OnConnect( NetworkEvents.iErrorCode[ FD_CONNECT_BIT ] ); + TRACE( "CThreadedSocketException( select )\n"); + Close(); + h_reader = NULL; + return sock_err; + break; + default: - if ( NetworkEvents.lNetworkEvents & FD_OOB ) - OnOutOfBandData( NetworkEvents.iErrorCode[ FD_OOB_BIT ] ); - if ( NetworkEvents.lNetworkEvents & FD_CLOSE ) + if ( FD_ISSET(m_hSocket, &readfds) ) { - OnClose( NetworkEvents.iErrorCode[ FD_CLOSE_BIT ] ); + IOCtl( FIONREAD, &readcount ); + if ( listen_mode ) + OnAccept( sock_err ); + else if ( ! readcount ) + OnClose( sock_err ); + else OnReceive(sock_err); } - break; - case WSA_WAIT_EVENT_0 +1 : - if ( m_hSocket != INVALID_SOCKET ) + if ( FD_ISSET(m_hSocket, &writefds) ) { - OnWakeup(); + if ( connect_pending ) + { + OnConnect( sock_err ); + connect_pending = false; + } } - // else Close by other Thread do silent terminaison - WSAResetEvent( m_hEvent[1] ); - + + if ( FD_ISSET(m_hSocket, &exceptfds) ) + OnOutOfBandData( sock_err ); + + + break; + } + } + Close(); + h_reader = NULL; + return 0; +} + +UINT CThreadedSocket::SocketWriter( ) +{ + int err; + int sock_err; + fd_set writefds; + + FD_ZERO(&writefds); + + + while ( m_hSocket != INVALID_SOCKET ) + { + DWORD dwWaitResult; + + // Wait for message to send + + dwWaitResult = WaitForSingleObject( send_count, INFINITE ); + + switch (dwWaitResult) { + + // The semaphore object was signaled. + case WAIT_OBJECT_0: + // OK to really send the message. + break; + + // Semaphore was nonsignaled, so a time-out occurred. + case WAIT_TIMEOUT: + case WAIT_FAILED: + default: + TRACE( "CThreadedSocketException( SocketWriter WaitForSingleObject )\n"); + Close(); + h_writer = NULL; + return GetLastError(); + break; + } + + FD_SET(m_hSocket, &writefds); + + err = select( 1, NULL, &writefds, NULL, NULL ); + sock_err = GetLastError(); + + switch ( err ) + { + case 0: + TRACE( "CThreadedSocket::SocketThread Unhandled Timeout Event !\n"); + break; + case SOCKET_ERROR: + TRACE( "CThreadedSocketException( select )\n"); + Close(); + h_reader = NULL; + return sock_err; break; default: - TRACE( "CThreadedSocket::SocketThread Unhandled Events !\n"); + + if ( FD_ISSET(m_hSocket, &writefds) ) + { + OnSend( sock_err ); + } + break; } } Close(); - h_thread = NULL; + h_writer = NULL; return 0; } -- cgit v1.1 From 043e12f158bf3abe0b981b2f69c8376338701afc Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:00 +0000 Subject: Utilisateur : Fcolin Date : 14/02/01 Heure : 18:47 Archivé dans $/Ivy (vss 7) --- Ivy/ThreadedSocket.cxx | 79 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 28 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 7f36f07..4f94573 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -30,10 +30,21 @@ CThreadedSocket::CThreadedSocket() CThreadedSocket::~CThreadedSocket() { + long PreviousCount = 0; + int err; if (m_hSocket != INVALID_SOCKET) Close(); //if ( thread ) // On fait de l'auto delete mais dans le cas de terminaison anormale l'object reste ????!!! // delete thread; + TRACE("CThreadedSocket waiting for thread end ...\n"); + if ( h_reader ) WaitForSingleObject( h_reader, INFINITE ); + // wake up writer + if ( h_writer ) + { + err = ReleaseSemaphore(send_count, 1, &PreviousCount); + WaitForSingleObject( h_writer, INFINITE ); + } + TRACE("CThreadedSocket all thread ended\n"); } int CThreadedSocket::Create(UINT nSocketPort, int nSocketType, const char * lpszSocketAddress) { @@ -376,7 +387,7 @@ UINT CThreadedSocket::SocketReader( ) { int err; int sock_err; - unsigned long readcount; + unsigned long readcount = 0; fd_set readfds; fd_set writefds; fd_set exceptfds; @@ -390,6 +401,8 @@ UINT CThreadedSocket::SocketReader( ) FD_SET(m_hSocket, &readfds); if ( connect_pending ) FD_SET(m_hSocket, &writefds); + else + FD_CLR(m_hSocket, &writefds); FD_SET(m_hSocket, &exceptfds); err = select( 1, &readfds, &writefds, &exceptfds, NULL ); @@ -402,8 +415,10 @@ UINT CThreadedSocket::SocketReader( ) break; case SOCKET_ERROR: - TRACE( "CThreadedSocketException( select )\n"); - Close(); + TRACE( "CThreadedSocket::SocketReader( select error thread_id =( %d) )\n",reader_id); + + if (m_hSocket != INVALID_SOCKET) + Close(); h_reader = NULL; return sock_err; break; @@ -436,8 +451,11 @@ UINT CThreadedSocket::SocketReader( ) break; } } - Close(); + if (m_hSocket != INVALID_SOCKET) + Close(); h_reader = NULL; + TRACE( "CThreadedSocket::SocketReader( END thread_id =( 0x%x) )\n",reader_id); + return 0; } @@ -469,40 +487,45 @@ UINT CThreadedSocket::SocketWriter( ) case WAIT_TIMEOUT: case WAIT_FAILED: default: - TRACE( "CThreadedSocketException( SocketWriter WaitForSingleObject )\n"); - Close(); + TRACE( "CThreadedSocket::SocketWriter( WaitForSingleObject error thread_id =( %d) )\n",writer_id); + if (m_hSocket != INVALID_SOCKET) + Close(); h_writer = NULL; return GetLastError(); break; } + if (m_hSocket != INVALID_SOCKET) + { + FD_SET(m_hSocket, &writefds); - FD_SET(m_hSocket, &writefds); - - err = select( 1, NULL, &writefds, NULL, NULL ); - sock_err = GetLastError(); + err = select( 1, NULL, &writefds, NULL, NULL ); + sock_err = GetLastError(); - switch ( err ) - { - case 0: - TRACE( "CThreadedSocket::SocketThread Unhandled Timeout Event !\n"); - break; - case SOCKET_ERROR: - TRACE( "CThreadedSocketException( select )\n"); - Close(); - h_reader = NULL; - return sock_err; - break; - default: - - if ( FD_ISSET(m_hSocket, &writefds) ) - { - OnSend( sock_err ); - } + switch ( err ) + { + case 0: + TRACE( "CThreadedSocket::SocketThread Unhandled Timeout Event !\n"); + break; + case SOCKET_ERROR: + TRACE( "CThreadedSocketException( select )\n"); + Close(); + h_reader = NULL; + return sock_err; + break; + default: + + if ( FD_ISSET(m_hSocket, &writefds) ) + { + OnSend( sock_err ); + } - break; + break; + } } } Close(); h_writer = NULL; + TRACE( "CThreadedSocket::SocketWriter( END thread_id =( 0x%x) )\n",writer_id); + return 0; } -- cgit v1.1 From ddc6e6f2bbcce4a247952f216c6c50478654bb8a Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:02 +0000 Subject: Utilisateur : Fcolin Date : 19/02/01 Heure : 10:39 Archivé dans $/Ivy (vss 8) --- Ivy/ThreadedSocket.cxx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 4f94573..a9a1760 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -36,7 +36,7 @@ CThreadedSocket::~CThreadedSocket() Close(); //if ( thread ) // On fait de l'auto delete mais dans le cas de terminaison anormale l'object reste ????!!! // delete thread; - TRACE("CThreadedSocket waiting for thread end ...\n"); +// TRACE("CThreadedSocket waiting for thread end ...\n"); if ( h_reader ) WaitForSingleObject( h_reader, INFINITE ); // wake up writer if ( h_writer ) @@ -44,7 +44,7 @@ CThreadedSocket::~CThreadedSocket() err = ReleaseSemaphore(send_count, 1, &PreviousCount); WaitForSingleObject( h_writer, INFINITE ); } - TRACE("CThreadedSocket all thread ended\n"); +// TRACE("CThreadedSocket all thread ended\n"); } int CThreadedSocket::Create(UINT nSocketPort, int nSocketType, const char * lpszSocketAddress) { @@ -354,7 +354,7 @@ bool CThreadedSocket::SignalWriter() long PreviousCount = 0; int err; err = ReleaseSemaphore(send_count, 1, &PreviousCount); - TRACE("CThreadedSocket::SignalWriter() PreviousCount = %ld \n", PreviousCount ); +// TRACE("CThreadedSocket::SignalWriter() PreviousCount = %ld \n", PreviousCount ); return (err != 0); } @@ -454,7 +454,7 @@ UINT CThreadedSocket::SocketReader( ) if (m_hSocket != INVALID_SOCKET) Close(); h_reader = NULL; - TRACE( "CThreadedSocket::SocketReader( END thread_id =( 0x%x) )\n",reader_id); +// TRACE( "CThreadedSocket::SocketReader( END thread_id =( 0x%x) )\n",reader_id); return 0; } @@ -525,7 +525,7 @@ UINT CThreadedSocket::SocketWriter( ) } Close(); h_writer = NULL; - TRACE( "CThreadedSocket::SocketWriter( END thread_id =( 0x%x) )\n",writer_id); +// TRACE( "CThreadedSocket::SocketWriter( END thread_id =( 0x%x) )\n",writer_id); return 0; } -- cgit v1.1 From 3a34f0291c92edb20203611e85ba26909db58977 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:04 +0000 Subject: Utilisateur : Fcolin Date : 23/05/01 Heure : 10:25 Archivé dans $/Ivy (vss 9) --- Ivy/ThreadedSocket.cxx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index a9a1760..d113f8e 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -160,7 +160,7 @@ int CThreadedSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen) ok = connect(m_hSocket, lpSockAddr, nSockAddrLen); if ( !ok ) { - int err = GetLastError(); + int err = this->GetLastError(); if ( err == 0 ) return err; TRACE( "***************************************Error connect %d\n", err); if ( err != WSAEWOULDBLOCK ) @@ -262,7 +262,7 @@ int CThreadedSocket::SendTo(const void* lpBuf, int nBufLen, UINT nHostPort, cons sockAddr.sin_port = htons((u_short)nHostPort); return SendTo(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, sizeof(sockAddr), nFlags); -} + } int CThreadedSocket::AddMember( const char * lpszHostAddress ) { int multicast_ttl = 64; // region @@ -406,7 +406,7 @@ UINT CThreadedSocket::SocketReader( ) FD_SET(m_hSocket, &exceptfds); err = select( 1, &readfds, &writefds, &exceptfds, NULL ); - sock_err = GetLastError(); + sock_err = this->GetLastError(); switch ( err ) { @@ -427,6 +427,7 @@ UINT CThreadedSocket::SocketReader( ) if ( FD_ISSET(m_hSocket, &readfds) ) { + readcount = 0; IOCtl( FIONREAD, &readcount ); if ( listen_mode ) OnAccept( sock_err ); @@ -491,7 +492,7 @@ UINT CThreadedSocket::SocketWriter( ) if (m_hSocket != INVALID_SOCKET) Close(); h_writer = NULL; - return GetLastError(); + return this->GetLastError(); break; } if (m_hSocket != INVALID_SOCKET) @@ -499,7 +500,7 @@ UINT CThreadedSocket::SocketWriter( ) FD_SET(m_hSocket, &writefds); err = select( 1, NULL, &writefds, NULL, NULL ); - sock_err = GetLastError(); + sock_err = this->GetLastError(); switch ( err ) { -- cgit v1.1 From 7dbb8513b01bb2489b9b9672ea97682ed69610e5 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:06 +0000 Subject: Utilisateur : Fcolin Date : 6/06/01 Heure : 15:50 Archivé dans $/Ivy (vss 10) --- Ivy/ThreadedSocket.cxx | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index d113f8e..1afb304 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -392,6 +392,8 @@ UINT CThreadedSocket::SocketReader( ) fd_set writefds; fd_set exceptfds; + TRACE( "CThreadedSocket::SocketReader( START thread_id =( 0x%x) )\n",reader_id); + FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); @@ -466,6 +468,8 @@ UINT CThreadedSocket::SocketWriter( ) int sock_err; fd_set writefds; + TRACE( "CThreadedSocket::SocketWriter( START thread_id =( 0x%x) )\n",writer_id); + FD_ZERO(&writefds); -- cgit v1.1 From 2610a7a99b4bcff0d8faf22695d35f5342ccbdd3 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:08 +0000 Subject: Utilisateur : Fcolin Date : 17/07/01 Heure : 18:25 Archivé dans $/Ivy (vss 11) --- Ivy/ThreadedSocket.cxx | 4 ---- 1 file changed, 4 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 1afb304..5fa7322 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -4,10 +4,6 @@ #include "stdafx.h" -#ifdef _DEBUG -#define DEBUG_NEW new(__FILE__, __LINE__) -#define new DEBUG_NEW -#endif #include "ThreadedSocket.h" -- cgit v1.1 From 24304c151f70945ab52e52afb2bf120cef7d7020 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:10 +0000 Subject: Utilisateur : Fcolin Date : 13/08/01 Heure : 16:12 Archivé dans $/Ivy (vss 12) --- Ivy/ThreadedSocket.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 5fa7322..10af005 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -348,7 +348,7 @@ int CThreadedSocket::StartWriter() bool CThreadedSocket::SignalWriter() { long PreviousCount = 0; - int err; + BOOL err; err = ReleaseSemaphore(send_count, 1, &PreviousCount); // TRACE("CThreadedSocket::SignalWriter() PreviousCount = %ld \n", PreviousCount ); return (err != 0); -- cgit v1.1 From 0b1b7a6d41bb5a10da80de3d29946e29450b69f4 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:12 +0000 Subject: Utilisateur : Fcolin Date : 14/09/01 Heure : 16:44 Archivé dans $/Ivy Commentaire: correction BUG Ivy socket Listen apres start Listener et regexp_in.resize (vss 13) --- Ivy/ThreadedSocket.cxx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 10af005..be2bdf0 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -143,6 +143,13 @@ void CThreadedSocket::Close() } } +int CThreadedSocket::Listen(int nConnectionBacklog) +{ + int err = listen(m_hSocket, nConnectionBacklog); + if ( !err ) + listen_mode = true; + return StartListener(); +} int CThreadedSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen) { @@ -171,7 +178,7 @@ int CThreadedSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen) return err; } connect_pending = true; - return 0; + return StartListener(); } int CThreadedSocket::Connect(const char * lpszHostAddress, UINT nHostPort) @@ -332,7 +339,8 @@ int CThreadedSocket::Socket(int nSocketType, int nProtocolType, int nAddressForm { return m_hSocket; } - return StartListener(); + return m_hSocket; + //return StartListener(); } int CThreadedSocket::StartListener() -- cgit v1.1 From 3006e231e192a91fa6d132ddc3e483e1c1a31114 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:14 +0000 Subject: Utilisateur : Fcolin Date : 20/09/01 Heure : 9:40 Archivé dans $/Ivy (vss 14) --- Ivy/ThreadedSocket.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index be2bdf0..f14d79a 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -161,7 +161,7 @@ int CThreadedSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen) FD_SET(m_hSocket, &writefds); ok = connect(m_hSocket, lpSockAddr, nSockAddrLen); - if ( !ok ) + if ( ok != 0 ) { int err = this->GetLastError(); if ( err == 0 ) return err; -- cgit v1.1 From 1c70f693d16840f4afe269239b4c57dd58a914fe Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:16 +0000 Subject: Utilisateur : Fcolin Date : 19/06/02 Heure : 16:45 Archivé dans $/Ivy Commentaire: (vss 15) --- Ivy/ThreadedSocket.cxx | 4 ---- 1 file changed, 4 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index f14d79a..c93ffa9 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -335,10 +335,6 @@ int CThreadedSocket::Socket(int nSocketType, int nProtocolType, int nAddressForm m_hSocket = socket(nAddressFormat,nSocketType,nProtocolType); - if ( m_hSocket < 0 ) - { - return m_hSocket; - } return m_hSocket; //return StartListener(); } -- cgit v1.1 From 5d015386435c0c79ac5b973cd86d8c74c5d6d326 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:18 +0000 Subject: Utilisateur : Fcolin Date : 27/09/02 Heure : 12:47 Archivé dans $/Ivy Commentaire: (vss 16) --- Ivy/ThreadedSocket.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index c93ffa9..19670f7 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -513,7 +513,7 @@ UINT CThreadedSocket::SocketWriter( ) break; case SOCKET_ERROR: TRACE( "CThreadedSocketException( select )\n"); - Close(); + if (m_hSocket != INVALID_SOCKET) Close(); h_reader = NULL; return sock_err; break; -- cgit v1.1 From 5d97d1f9916d4c8a7880b965200b0d95510e6dfc Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:20 +0000 Subject: Utilisateur : Fcolin Date : 14/11/02 Heure : 15:45 Archivé dans $/Bus/Ivy Commentaire: (vss 17) --- Ivy/ThreadedSocket.cxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 19670f7..31bcf18 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -138,7 +138,8 @@ void CThreadedSocket::Close() { if (m_hSocket != INVALID_SOCKET) { - ASSERT(SOCKET_ERROR != closesocket(m_hSocket)); + //ASSERT(SOCKET_ERROR != closesocket(m_hSocket)); + closesocket(m_hSocket); // close silently m_hSocket = INVALID_SOCKET; } @@ -419,8 +420,7 @@ UINT CThreadedSocket::SocketReader( ) TRACE( "CThreadedSocket::SocketReader( select error thread_id =( %d) )\n",reader_id); - if (m_hSocket != INVALID_SOCKET) - Close(); + Close(); h_reader = NULL; return sock_err; break; -- cgit v1.1 From e8934aef52d003b97a36a651ced63b7587d85eec Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:22 +0000 Subject: Utilisateur : Fcolin Date : 25/11/02 Heure : 16:15 Archivé dans $/Bus/Ivy Commentaire: (vss 18) --- Ivy/ThreadedSocket.cxx | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 31bcf18..42a8216 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -26,21 +26,8 @@ CThreadedSocket::CThreadedSocket() CThreadedSocket::~CThreadedSocket() { - long PreviousCount = 0; - int err; if (m_hSocket != INVALID_SOCKET) Close(); - //if ( thread ) // On fait de l'auto delete mais dans le cas de terminaison anormale l'object reste ????!!! - // delete thread; -// TRACE("CThreadedSocket waiting for thread end ...\n"); - if ( h_reader ) WaitForSingleObject( h_reader, INFINITE ); - // wake up writer - if ( h_writer ) - { - err = ReleaseSemaphore(send_count, 1, &PreviousCount); - WaitForSingleObject( h_writer, INFINITE ); - } -// TRACE("CThreadedSocket all thread ended\n"); } int CThreadedSocket::Create(UINT nSocketPort, int nSocketType, const char * lpszSocketAddress) { @@ -136,12 +123,24 @@ int CThreadedSocket::Bind(UINT nSocketPort, const char * lpszSocketAddress) void CThreadedSocket::Close() { + long PreviousCount = 0; + int err; if (m_hSocket != INVALID_SOCKET) { //ASSERT(SOCKET_ERROR != closesocket(m_hSocket)); closesocket(m_hSocket); // close silently m_hSocket = INVALID_SOCKET; - + //if ( thread ) // On fait de l'auto delete mais dans le cas de terminaison anormale l'object reste ????!!! + // delete thread; +// TRACE("CThreadedSocket waiting for thread end ...\n"); + if ( h_reader ) WaitForSingleObject( h_reader, INFINITE ); + // wake up writer + if ( h_writer ) + { + err = ReleaseSemaphore(send_count, 1, &PreviousCount); + WaitForSingleObject( h_writer, INFINITE ); + } +// TRACE("CThreadedSocket all thread ended\n"); } } int CThreadedSocket::Listen(int nConnectionBacklog) -- cgit v1.1 From 9700b7c0d1ec515e0ea59dfa5c7e42acd02815aa Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:24 +0000 Subject: Utilisateur : Fcolin Date : 23/01/04 Heure : 16:30 Archivé dans $/Bus/Ivy Commentaire: (vss 19) --- Ivy/ThreadedSocket.cxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 42a8216..2f32a26 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -133,12 +133,12 @@ void CThreadedSocket::Close() //if ( thread ) // On fait de l'auto delete mais dans le cas de terminaison anormale l'object reste ????!!! // delete thread; // TRACE("CThreadedSocket waiting for thread end ...\n"); - if ( h_reader ) WaitForSingleObject( h_reader, INFINITE ); + if ( h_reader ) WaitForSingleObject( h_reader, 5000 ); // wake up writer if ( h_writer ) { err = ReleaseSemaphore(send_count, 1, &PreviousCount); - WaitForSingleObject( h_writer, INFINITE ); + WaitForSingleObject( h_writer, 5000 ); } // TRACE("CThreadedSocket all thread ended\n"); } @@ -456,7 +456,7 @@ UINT CThreadedSocket::SocketReader( ) if (m_hSocket != INVALID_SOCKET) Close(); h_reader = NULL; -// TRACE( "CThreadedSocket::SocketReader( END thread_id =( 0x%x) )\n",reader_id); + TRACE( "CThreadedSocket::SocketReader( END thread_id =( 0x%x) )\n",reader_id); return 0; } -- cgit v1.1 From 2fab956d8131fb79a0dce36b34733afc88fe23ca Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:26 +0000 Subject: Utilisateur : Fcolin Date : 9/02/04 Heure : 16:14 Archivé dans $/Bus/Ivy Commentaire: (vss 20) --- Ivy/ThreadedSocket.cxx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 2f32a26..3934095 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -128,8 +128,11 @@ void CThreadedSocket::Close() if (m_hSocket != INVALID_SOCKET) { //ASSERT(SOCKET_ERROR != closesocket(m_hSocket)); - closesocket(m_hSocket); // close silently + SOCKET temp = m_hSocket; // Thread ACK m_hSocket = INVALID_SOCKET; + TRACE( "CThreadedSocket::Close (reader=0x%0x) (writer=0x%0x)%\n", reader_id, writer_id ); + closesocket(temp); // close silently + //if ( thread ) // On fait de l'auto delete mais dans le cas de terminaison anormale l'object reste ????!!! // delete thread; // TRACE("CThreadedSocket waiting for thread end ...\n"); @@ -417,9 +420,9 @@ UINT CThreadedSocket::SocketReader( ) break; case SOCKET_ERROR: - TRACE( "CThreadedSocket::SocketReader( select error thread_id =( %d) )\n",reader_id); - - Close(); + TRACE( "CThreadedSocket::SocketReader( select error thread_id =( 0x%x) )\n",reader_id); + if ( (sock_err != WSAENOTSOCK ) && ( m_hSocket != INVALID_SOCKET )) // could be Invalid if close when in select + Close(); h_reader = NULL; return sock_err; break; @@ -512,7 +515,8 @@ UINT CThreadedSocket::SocketWriter( ) break; case SOCKET_ERROR: TRACE( "CThreadedSocketException( select )\n"); - if (m_hSocket != INVALID_SOCKET) Close(); + if ( (sock_err != WSAENOTSOCK ) && ( m_hSocket != INVALID_SOCKET )) // could be Invalid if close when in select + Close(); h_reader = NULL; return sock_err; break; -- cgit v1.1 From bb6dabd8e1afc8dd2474183a97d7663f1154d687 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:28 +0000 Subject: Utilisateur : Fcolin Date : 1/06/05 Heure : 16:45 Archivé dans $/Bus/Ivy Commentaire: (vss 21) --- Ivy/ThreadedSocket.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 3934095..f85b84d 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -2,7 +2,7 @@ // ////////////////////////////////////////////////////////////////////// -#include "stdafx.h" +#include "IvyStdAfx.h" #include "ThreadedSocket.h" -- cgit v1.1 From 97bfd72ffebfef20dedceb78c06ed2aa0742ea7c Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:30 +0000 Subject: Utilisateur : Fcolin Date : 2/06/05 Heure : 18:42 Archivé dans $/Bus/Ivy Commentaire: Suppression de la STL et ajout d'un namespace pour les datatypes internes Ivy (vss 22) --- Ivy/ThreadedSocket.cxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index f85b84d..98235af 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -46,7 +46,7 @@ int CThreadedSocket::Create(UINT nSocketPort, int nSocketType, const char * lpsz -void CThreadedSocket::GetPeerName(string & rPeerAddress, UINT& rPeerPort) +void CThreadedSocket::GetPeerName(ivy::string & rPeerAddress, UINT& rPeerPort) { SOCKADDR_IN sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); @@ -57,7 +57,7 @@ void CThreadedSocket::GetPeerName(string & rPeerAddress, UINT& rPeerPort) rPeerAddress = inet_ntoa(sockAddr.sin_addr); } -void CThreadedSocket::GetSockName(string & rSocketAddress, UINT& rSocketPort) +void CThreadedSocket::GetSockName(ivy::string & rSocketAddress, UINT& rSocketPort) { SOCKADDR_IN sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); @@ -219,7 +219,7 @@ int CThreadedSocket::Receive(void* lpBuf, int nBufLen, int nFlags) return lg; } -int CThreadedSocket::ReceiveFrom(void* lpBuf, int nBufLen, string & rSocketAddress, UINT& rSocketPort, int nFlags) +int CThreadedSocket::ReceiveFrom(void* lpBuf, int nBufLen, ivy::string & rSocketAddress, UINT& rSocketPort, int nFlags) { SOCKADDR_IN sockAddr; -- cgit v1.1 From fba86770a42e8aae5c76fe63136ad648978b53bd Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:32 +0000 Subject: Utilisateur : Fcolin Date : 23/09/05 Heure : 15:27 Archivé dans $/Bus/Ivy Commentaire: (vss 23) --- Ivy/ThreadedSocket.cxx | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 98235af..5654758 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -71,7 +71,7 @@ void CThreadedSocket::GetSockName(ivy::string & rSocketAddress, UINT& rSocketPor ///////////////////////////////////////////////////////////////////////////// // CAscynSocket Operations -int CThreadedSocket::Accept(CThreadedSocket& rConnectedSocket, +SOCKET CThreadedSocket::Accept(CThreadedSocket& rConnectedSocket, SOCKADDR* lpSockAddr, int* lpSockAddrLen) { ASSERT(rConnectedSocket.m_hSocket == INVALID_SOCKET); @@ -213,32 +213,32 @@ int CThreadedSocket::Connect(const char * lpszHostAddress, UINT nHostPort) return Connect((SOCKADDR*)&sockAddr, sizeof(sockAddr)); } -int CThreadedSocket::Receive(void* lpBuf, int nBufLen, int nFlags) +size_t CThreadedSocket::Receive(void* lpBuf, size_t nBufLen, int nFlags) { - int lg = recv(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags); + int lg = recv(m_hSocket, (LPSTR)lpBuf, (int)nBufLen, nFlags); return lg; } -int CThreadedSocket::ReceiveFrom(void* lpBuf, int nBufLen, ivy::string & rSocketAddress, UINT& rSocketPort, int nFlags) +size_t CThreadedSocket::ReceiveFrom(void* lpBuf, size_t nBufLen, ivy::string & rSocketAddress, UINT& rSocketPort, int nFlags) { SOCKADDR_IN sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); - int nSockAddrLen = sizeof(sockAddr); - int nResult = ReceiveFrom(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, &nSockAddrLen, nFlags); + size_t nSockAddrLen = sizeof(sockAddr); + size_t nResult = ReceiveFrom(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, &nSockAddrLen, nFlags); rSocketPort = ntohs(sockAddr.sin_port); rSocketAddress = inet_ntoa(sockAddr.sin_addr); return nResult; } -int CThreadedSocket::Send(const void* lpBuf, int nBufLen, int nFlags) +size_t CThreadedSocket::Send(const void* lpBuf, size_t nBufLen, int nFlags) { - return send(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags); + return send(m_hSocket, (LPSTR)lpBuf, (int)nBufLen, nFlags); } -int CThreadedSocket::SendTo(const void* lpBuf, int nBufLen, UINT nHostPort, const char * lpszHostAddress, int nFlags) +size_t CThreadedSocket::SendTo(const void* lpBuf, size_t nBufLen, UINT nHostPort, const char * lpszHostAddress, int nFlags) { SOCKADDR_IN sockAddr; @@ -332,7 +332,7 @@ void CThreadedSocket::OnClose(int /*nErrorCode*/) // CThreadedSocket Implementation -int CThreadedSocket::Socket(int nSocketType, int nProtocolType, int nAddressFormat) +SOCKET CThreadedSocket::Socket(int nSocketType, int nProtocolType, int nAddressFormat) { ASSERT(m_hSocket == INVALID_SOCKET); -- cgit v1.1 From bc919bfe58f3dd881182082570f1c2424612734d Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:34 +0000 Subject: Utilisateur : Fcolin Date : 16/11/05 Heure : 9:54 Archivé dans $/Bus/Ivy Commentaire: 64 bits ports (vss 24) --- Ivy/ThreadedSocket.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 5654758..610f9b7 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -130,7 +130,7 @@ void CThreadedSocket::Close() //ASSERT(SOCKET_ERROR != closesocket(m_hSocket)); SOCKET temp = m_hSocket; // Thread ACK m_hSocket = INVALID_SOCKET; - TRACE( "CThreadedSocket::Close (reader=0x%0x) (writer=0x%0x)%\n", reader_id, writer_id ); + TRACE( "CThreadedSocket::Close (reader=0x%0lx) (writer=0x%0lx)\n", reader_id, writer_id ); closesocket(temp); // close silently //if ( thread ) // On fait de l'auto delete mais dans le cas de terminaison anormale l'object reste ????!!! -- cgit v1.1 From 79e97e573fc073eb9554ca0b89e2d0580e89a286 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 13:05:36 +0000 Subject: Utilisateur : Fcolin Date : 18/11/05 Heure : 11:46 Archivé dans $/Bus/Ivy Commentaire: repassage a la STL et correction bug multithread (vss 25) --- Ivy/ThreadedSocket.cxx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'Ivy/ThreadedSocket.cxx') diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 610f9b7..ee1e03b 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -16,9 +16,11 @@ CThreadedSocket::CThreadedSocket() { - m_hSocket = INVALID_SOCKET; + m_hSocket = INVALID_SOCKET; h_reader = NULL; h_writer = NULL; + reader_id = 0; + writer_id = 0; listen_mode = false; connect_pending = true; send_pending = false; @@ -125,8 +127,13 @@ void CThreadedSocket::Close() { long PreviousCount = 0; int err; + DWORD currentThreadId; + + if (m_hSocket != INVALID_SOCKET) { + currentThreadId = GetCurrentThreadId(); + //ASSERT(SOCKET_ERROR != closesocket(m_hSocket)); SOCKET temp = m_hSocket; // Thread ACK m_hSocket = INVALID_SOCKET; @@ -136,9 +143,10 @@ void CThreadedSocket::Close() //if ( thread ) // On fait de l'auto delete mais dans le cas de terminaison anormale l'object reste ????!!! // delete thread; // TRACE("CThreadedSocket waiting for thread end ...\n"); - if ( h_reader ) WaitForSingleObject( h_reader, 5000 ); + if ( h_reader && currentThreadId != reader_id ) + WaitForSingleObject( h_reader, 5000 ); // wake up writer - if ( h_writer ) + if ( h_writer && currentThreadId != writer_id ) { err = ReleaseSemaphore(send_count, 1, &PreviousCount); WaitForSingleObject( h_writer, 5000 ); @@ -533,7 +541,7 @@ UINT CThreadedSocket::SocketWriter( ) } Close(); h_writer = NULL; -// TRACE( "CThreadedSocket::SocketWriter( END thread_id =( 0x%x) )\n",writer_id); + TRACE( "CThreadedSocket::SocketWriter( END thread_id =( 0x%x) )\n",writer_id); return 0; } -- cgit v1.1