clean up the socket so that we do not have direct calls to e.g. protocol messages

This commit is contained in:
Volker Fischer 2013-05-10 19:34:55 +00:00
parent 21707b0e17
commit 8e95b548cb
4 changed files with 106 additions and 40 deletions

View file

@ -133,7 +133,7 @@ CClient::CClient ( const quint16 iPortNumber ) :
// Connections -------------------------------------------------------------
// connection for protocol
// connections for the protocol mechanism
QObject::connect ( &Channel,
SIGNAL ( MessReadyForSending ( CVector<uint8_t> ) ),
this, SLOT ( OnSendProtMessage ( CVector<uint8_t> ) ) );
@ -192,8 +192,13 @@ QObject::connect ( &Channel, SIGNAL ( OpusSupported() ),
SIGNAL ( CLPingWithNumClientsReceived ( CHostAddress, int, int ) ),
this, SLOT ( OnCLPingWithNumClientsReceived ( CHostAddress, int, int ) ) );
// other
QObject::connect ( &Sound, SIGNAL ( ReinitRequest ( int ) ),
this, SLOT ( OnSndCrdReinitRequest ( int ) ) );
QObject::connect ( &Socket, SIGNAL ( InvalidPacketReceived ( CVector<uint8_t>, int, CHostAddress ) ),
this, SLOT ( OnInvalidPacketReceived ( CVector<uint8_t>, int, CHostAddress ) ) );
}
void CClient::OnSendProtMessage ( CVector<uint8_t> vecMessage )
@ -211,6 +216,30 @@ void CClient::OnSendCLProtMessage ( CHostAddress InetAddr,
Socket.SendPacket ( vecMessage, InetAddr );
}
void CClient::OnInvalidPacketReceived ( CVector<uint8_t> vecbyRecBuf,
int iNumBytesRead,
CHostAddress RecHostAddr )
{
// this is an unknown address or we are not connected, try to
// parse connection less message (we have this case when we,
// e.g., open the connection setup dialog since then we are not
// yet connected but talk to the central server with the
// connection less protocol)
if ( ConnLessProtocol.ParseConnectionLessMessage ( vecbyRecBuf,
iNumBytesRead,
RecHostAddr ) )
{
// message coult not be parsed, check if the packet comes
// from the server we just connected -> if yes, send
// disconnect message since the server may not know that we
// are not connected anymore
if ( Channel. GetAddress() == RecHostAddr )
{
ConnLessProtocol.CreateCLDisconnection ( RecHostAddr );
}
}
}
void CClient::OnDetectedCLMessage ( CVector<uint8_t> vecbyData,
int iNumBytes )
{

View file

@ -347,6 +347,9 @@ void SetAudoCompressiontype ( const EAudComprType eNAudCompressionType );
public slots:
void OnSendProtMessage ( CVector<uint8_t> vecMessage );
void OnInvalidPacketReceived ( CVector<uint8_t> vecbyRecBuf,
int iNumBytesRead,
CHostAddress RecHostAddr );
void OnDetectedCLMessage ( CVector<uint8_t> vecbyData, int iNumBytes );
void OnReqJittBufSize() { CreateServerJitterBufferMessage(); }
void OnJittBufSizeChanged ( int iNewJitBufSize );

View file

@ -121,57 +121,45 @@ void CSocket::OnDataReceived()
if ( bIsClient )
{
// client:
// check if packet comes from the server we want to connect and that
// the channel is enabled
if ( !( pChannel->GetAddress() == RecHostAddr ) ||
!pChannel->IsEnabled() )
if ( ( pChannel->GetAddress() == RecHostAddr ) &&
pChannel->IsEnabled() )
{
// this is an unknown address or we are not connected, try to
// parse connection less message (we have this case when we,
// e.g., open the connection setup dialog since then we are not
// yet connected but talk to the central server with the
// connection less protocol)
if ( pConnLessProtocol->ParseConnectionLessMessage ( vecbyRecBuf,
iNumBytesRead,
RecHostAddr ) )
// this network packet is valid, put it in the channel
switch ( pChannel->PutData ( vecbyRecBuf, iNumBytesRead ) )
{
// message coult not be parsed, check if the packet comes
// from the server we just connected -> if yes, send
// disconnect message since the server may not know that we
// are not connected anymore
if ( pChannel->GetAddress() == RecHostAddr )
{
pConnLessProtocol->CreateCLDisconnection ( RecHostAddr );
}
case PS_AUDIO_OK:
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_GREEN );
break;
case PS_AUDIO_ERR:
case PS_GEN_ERROR:
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_RED );
break;
case PS_PROT_ERR:
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_YELLOW );
break;
default:
// other put data states need not to be considered here
break;
}
// do not perform any other action on this received packet
return;
}
switch ( pChannel->PutData ( vecbyRecBuf, iNumBytesRead ) )
else
{
case PS_AUDIO_OK:
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_GREEN );
break;
case PS_AUDIO_ERR:
case PS_GEN_ERROR:
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_RED );
break;
case PS_PROT_ERR:
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_YELLOW );
break;
default:
// other put data states need not to be considered here
break;
// inform about received invalid packet by fireing an event
emit InvalidPacketReceived ( vecbyRecBuf,
iNumBytesRead,
RecHostAddr );
}
}
else
{
// server:
if ( pServer->PutData ( vecbyRecBuf, iNumBytesRead, RecHostAddr ) )
{
// this was an audio packet, start server

View file

@ -29,6 +29,7 @@
#include <QMessageBox>
#include <QUdpSocket>
#include <QSocketNotifier>
#include <QThread>
#include <QMutex>
#include <vector>
#include "global.h"
@ -48,6 +49,7 @@ class CServer; // forward declaration of CServer
/* Classes ********************************************************************/
/* Base socket class ---------------------------------------------------------*/
class CSocket : public QObject
{
Q_OBJECT
@ -83,6 +85,50 @@ protected:
public slots:
void OnDataReceived();
signals:
void InvalidPacketReceived ( CVector<uint8_t> vecbyRecBuf,
int iNumBytesRead,
CHostAddress RecHostAddr );
};
/* Socket which runs in a separate high priority thread ----------------------*/
/*
// TEST
// http://qt-project.org/forums/viewthread/14393
// http://qt-project.org/doc/qt-5.0/qtcore/qthread.html#Priority-enum
// http://qt-project.org/wiki/Threads_Events_QObjects
class CHighPrioSocket
{
public:
CHighPrioSocket ( CChannel* pNewChannel,
CProtocol* pNewCLProtocol,
const quint16 iPortNumber )
{
// TEST
worker = new CSocket ( pNewChannel, pNewCLProtocol, iPortNumber );
worker->moveToThread(&workerThread);
workerThread.start(QThread::TimeCriticalPriority);
}
void SendPacket ( const CVector<uint8_t>& vecbySendBuf,
const CHostAddress& HostAddr )
{
worker->SendPacket ( vecbySendBuf, HostAddr );
}
protected:
// TEST
QThread workerThread;
CSocket* worker;
};
*/
#endif /* !defined ( SOCKET_HOIHGE76GEKJH98_3_4344_BB23945IUHF1912__INCLUDED_ ) */