some cleanup and implemented a nice socket thread exit on shutdown (fixed Qt warnings on shutdown of the software)

This commit is contained in:
Volker Fischer 2014-02-13 18:38:25 +00:00
parent 7c37d6d018
commit bab6152f8c
2 changed files with 37 additions and 13 deletions

View file

@ -142,6 +142,15 @@ QObject::connect ( this,
#endif #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() CSocket::~CSocket()
{ {
#ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD #ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD
@ -240,8 +249,8 @@ void CSocket::OnDataReceived()
&SenderPort ); &SenderPort );
#endif #endif
// check if an error occurred // check if an error occurred or no data could be read
if ( iNumBytesRead < 0 ) if ( iNumBytesRead <= 0 )
{ {
return; return;
} }

View file

@ -81,6 +81,8 @@ public:
bool GetAndResetbJitterBufferOKFlag(); bool GetAndResetbJitterBufferOKFlag();
#ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD #ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD
void Close();
void EmitDetectedCLMessage ( const CVector<uint8_t>& vecbyMesBodyData, void EmitDetectedCLMessage ( const CVector<uint8_t>& vecbyMesBodyData,
const int iRecID ) const int iRecID )
{ {
@ -147,22 +149,22 @@ class CHighPrioSocket : public QObject
public: public:
CHighPrioSocket ( CChannel* pNewChannel, CHighPrioSocket ( CChannel* pNewChannel,
const quint16 iPortNumber ) const quint16 iPortNumber ) :
Socket ( pNewChannel, iPortNumber )
{ {
// Creation of the new socket thread which has to have the highest // Creation of the new socket thread which has to have the highest
// possible thread priority to make sure the jitter buffer is reliably // possible thread priority to make sure the jitter buffer is reliably
// filled with the network audio packets and does not get interrupted // filled with the network audio packets and does not get interrupted
// by other GUI threads. The following code is based on: // by other GUI threads. The following code is based on:
// http://qt-project.org/wiki/Threads_Events_QObjects // http://qt-project.org/wiki/Threads_Events_QObjects
pSocket = new CSocket ( pNewChannel, iPortNumber ); Socket.moveToThread ( &NetworkWorkerThread );
pSocket->moveToThread ( &NetworkWorkerThread );
NetworkWorkerThread.SetSocket ( pSocket ); NetworkWorkerThread.SetSocket ( &Socket );
NetworkWorkerThread.start ( QThread::TimeCriticalPriority ); NetworkWorkerThread.start ( QThread::TimeCriticalPriority );
// connect the "InvalidPacketReceived" signal // connect the "InvalidPacketReceived" signal
QObject::connect ( pSocket, QObject::connect ( &Socket,
SIGNAL ( InvalidPacketReceived ( CVector<uint8_t>, int, CHostAddress ) ), SIGNAL ( InvalidPacketReceived ( CVector<uint8_t>, int, CHostAddress ) ),
SIGNAL ( InvalidPacketReceived ( CVector<uint8_t>, int, CHostAddress ) ) ); SIGNAL ( InvalidPacketReceived ( CVector<uint8_t>, int, CHostAddress ) ) );
} }
@ -175,12 +177,12 @@ public:
void SendPacket ( const CVector<uint8_t>& vecbySendBuf, void SendPacket ( const CVector<uint8_t>& vecbySendBuf,
const CHostAddress& HostAddr ) const CHostAddress& HostAddr )
{ {
pSocket->SendPacket ( vecbySendBuf, HostAddr ); Socket.SendPacket ( vecbySendBuf, HostAddr );
} }
bool GetAndResetbJitterBufferOKFlag() bool GetAndResetbJitterBufferOKFlag()
{ {
return pSocket->GetAndResetbJitterBufferOKFlag(); return Socket.GetAndResetbJitterBufferOKFlag();
} }
protected: protected:
@ -190,17 +192,30 @@ protected:
CSocketThread ( CSocket* pNewSocket = NULL, QObject* parent = 0 ) : CSocketThread ( CSocket* pNewSocket = NULL, QObject* parent = 0 ) :
QThread ( parent ), pSocket ( pNewSocket ), bRun ( true ) {} QThread ( parent ), pSocket ( pNewSocket ), bRun ( true ) {}
void SetSocket ( CSocket* pNewSocket ) { pSocket = pNewSocket; } virtual ~CSocketThread()
void Stop() { bRun = false; {
// TODO wait for thread to be deleted... // 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: protected:
void run() { void run() {
// make sure the socket pointer is initialized (should be always the
// case)
if ( pSocket != NULL ) if ( pSocket != NULL )
{ {
while ( bRun ) while ( bRun )
{ {
// this function is a blocking function (waiting for network
// packets to be received and processed)
pSocket->OnDataReceived(); pSocket->OnDataReceived();
} }
} }
@ -211,7 +226,7 @@ protected:
}; };
CSocketThread NetworkWorkerThread; CSocketThread NetworkWorkerThread;
CSocket* pSocket; CSocket Socket;
signals: signals:
void InvalidPacketReceived ( CVector<uint8_t> vecbyRecBuf, void InvalidPacketReceived ( CVector<uint8_t> vecbyRecBuf,