summaryrefslogtreecommitdiff
path: root/Ivy/ThreadedSocket.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'Ivy/ThreadedSocket.cxx')
-rw-r--r--Ivy/ThreadedSocket.cxx79
1 files changed, 51 insertions, 28 deletions
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;
}