diff --git a/src/socket.cpp b/src/socket.cpp index 095af9f9..8534861b 100755 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -142,6 +142,15 @@ QObject::connect ( this, #endif } +#ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD +void CSocket::Close() +{ + // closesocket will cause recvfrom to return with an error because the + // socket is closed -> then the thread can safely be shut down + closesocket ( UdpSocket ); +} +#endif + CSocket::~CSocket() { #ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD @@ -240,8 +249,8 @@ void CSocket::OnDataReceived() &SenderPort ); #endif - // check if an error occurred - if ( iNumBytesRead < 0 ) + // check if an error occurred or no data could be read + if ( iNumBytesRead <= 0 ) { return; } diff --git a/src/socket.h b/src/socket.h index c9cde5ba..f0c06ef3 100755 --- a/src/socket.h +++ b/src/socket.h @@ -81,6 +81,8 @@ public: bool GetAndResetbJitterBufferOKFlag(); #ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD + void Close(); + void EmitDetectedCLMessage ( const CVector& vecbyMesBodyData, const int iRecID ) { @@ -147,22 +149,22 @@ class CHighPrioSocket : public QObject public: CHighPrioSocket ( CChannel* pNewChannel, - const quint16 iPortNumber ) + const quint16 iPortNumber ) : + Socket ( pNewChannel, iPortNumber ) { // Creation of the new socket thread which has to have the highest // possible thread priority to make sure the jitter buffer is reliably // filled with the network audio packets and does not get interrupted // by other GUI threads. The following code is based on: // http://qt-project.org/wiki/Threads_Events_QObjects - pSocket = new CSocket ( pNewChannel, iPortNumber ); - pSocket->moveToThread ( &NetworkWorkerThread ); + Socket.moveToThread ( &NetworkWorkerThread ); - NetworkWorkerThread.SetSocket ( pSocket ); + NetworkWorkerThread.SetSocket ( &Socket ); NetworkWorkerThread.start ( QThread::TimeCriticalPriority ); // connect the "InvalidPacketReceived" signal - QObject::connect ( pSocket, + QObject::connect ( &Socket, SIGNAL ( InvalidPacketReceived ( CVector, int, CHostAddress ) ), SIGNAL ( InvalidPacketReceived ( CVector, int, CHostAddress ) ) ); } @@ -175,12 +177,12 @@ public: void SendPacket ( const CVector& vecbySendBuf, const CHostAddress& HostAddr ) { - pSocket->SendPacket ( vecbySendBuf, HostAddr ); + Socket.SendPacket ( vecbySendBuf, HostAddr ); } bool GetAndResetbJitterBufferOKFlag() { - return pSocket->GetAndResetbJitterBufferOKFlag(); + return Socket.GetAndResetbJitterBufferOKFlag(); } protected: @@ -190,17 +192,30 @@ protected: CSocketThread ( CSocket* pNewSocket = NULL, QObject* parent = 0 ) : QThread ( parent ), pSocket ( pNewSocket ), bRun ( true ) {} - void SetSocket ( CSocket* pNewSocket ) { pSocket = pNewSocket; } - void Stop() { bRun = false; -// TODO wait for thread to be deleted... + virtual ~CSocketThread() + { + // disable run flag so that the thread loop can be exit + bRun = false; + + // to leave blocking wait for receive + pSocket->Close(); + + // give thread some time to terminate + wait ( 5000 ); } + void SetSocket ( CSocket* pNewSocket ) { pSocket = pNewSocket; } + protected: void run() { + // make sure the socket pointer is initialized (should be always the + // case) if ( pSocket != NULL ) { while ( bRun ) { + // this function is a blocking function (waiting for network + // packets to be received and processed) pSocket->OnDataReceived(); } } @@ -211,7 +226,7 @@ protected: }; CSocketThread NetworkWorkerThread; - CSocket* pSocket; + CSocket Socket; signals: void InvalidPacketReceived ( CVector vecbyRecBuf,