2013-01-23 11:41:13 +01:00
|
|
|
/******************************************************************************\
|
2014-01-05 17:52:38 +01:00
|
|
|
* Copyright (c) 2004-2014
|
2013-01-23 11:41:13 +01:00
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Volker Fischer
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
\******************************************************************************/
|
|
|
|
|
|
|
|
#if !defined ( SOCKET_HOIHGE76GEKJH98_3_4344_BB23945IUHF1912__INCLUDED_ )
|
|
|
|
#define SOCKET_HOIHGE76GEKJH98_3_4344_BB23945IUHF1912__INCLUDED_
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QUdpSocket>
|
|
|
|
#include <QSocketNotifier>
|
2013-05-10 21:34:55 +02:00
|
|
|
#include <QThread>
|
2013-01-23 11:41:13 +01:00
|
|
|
#include <QMutex>
|
|
|
|
#include <vector>
|
|
|
|
#include "global.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "util.h"
|
2014-02-11 21:39:07 +01:00
|
|
|
#ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD
|
|
|
|
# ifndef _WIN32
|
|
|
|
# include <netinet/in.h>
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
2013-01-23 11:41:13 +01:00
|
|
|
|
2014-01-06 16:57:40 +01:00
|
|
|
// The header files channel.h and server.h require to include this header file
|
|
|
|
// so we get a cyclic dependency. To solve this issue, a prototype of the
|
|
|
|
// channel class and server class is defined here.
|
|
|
|
class CServer; // forward declaration of CServer
|
|
|
|
class CChannel; // forward declaration of CChannel
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* Definitions ****************************************************************/
|
|
|
|
// number of ports we try to bind until we give up
|
|
|
|
#define NUM_SOCKET_PORTS_TO_TRY 50
|
|
|
|
|
|
|
|
|
|
|
|
/* Classes ********************************************************************/
|
2013-05-10 21:34:55 +02:00
|
|
|
/* Base socket class ---------------------------------------------------------*/
|
2013-01-23 11:41:13 +01:00
|
|
|
class CSocket : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
CSocket ( CChannel* pNewChannel,
|
|
|
|
const quint16 iPortNumber )
|
2014-01-03 09:54:49 +01:00
|
|
|
: pChannel ( pNewChannel ),
|
|
|
|
bIsClient ( true ),
|
|
|
|
bJitterBufferOK ( true ) { Init ( iPortNumber ); }
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
CSocket ( CServer* pNServP,
|
|
|
|
const quint16 iPortNumber )
|
2014-01-03 09:54:49 +01:00
|
|
|
: pServer ( pNServP ),
|
|
|
|
bIsClient ( false ),
|
|
|
|
bJitterBufferOK ( true ) { Init ( iPortNumber ); }
|
2013-01-23 11:41:13 +01:00
|
|
|
|
2014-02-11 18:35:39 +01:00
|
|
|
virtual ~CSocket();
|
|
|
|
|
2013-01-23 11:41:13 +01:00
|
|
|
void SendPacket ( const CVector<uint8_t>& vecbySendBuf,
|
|
|
|
const CHostAddress& HostAddr );
|
|
|
|
|
2014-01-03 09:54:49 +01:00
|
|
|
bool GetAndResetbJitterBufferOKFlag();
|
|
|
|
|
2014-02-11 21:21:32 +01:00
|
|
|
#ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD
|
2014-02-13 19:38:25 +01:00
|
|
|
void Close();
|
|
|
|
|
2014-02-11 21:21:32 +01:00
|
|
|
void EmitDetectedCLMessage ( const CVector<uint8_t>& vecbyMesBodyData,
|
|
|
|
const int iRecID )
|
|
|
|
{
|
|
|
|
emit DetectedCLMessage ( vecbyMesBodyData, iRecID );
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitParseMessageBody ( const CVector<uint8_t>& vecbyMesBodyData,
|
|
|
|
const int iRecCounter,
|
|
|
|
const int iRecID )
|
|
|
|
{
|
|
|
|
emit ParseMessageBody ( vecbyMesBodyData, iRecCounter, iRecID );
|
|
|
|
}
|
2014-02-13 22:03:33 +01:00
|
|
|
|
|
|
|
void EmitNewConnection()
|
|
|
|
{
|
|
|
|
emit NewConnection();
|
|
|
|
}
|
2014-02-11 21:21:32 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-23 11:41:13 +01:00
|
|
|
protected:
|
|
|
|
void Init ( const quint16 iPortNumber = LLCON_DEFAULT_PORT_NUMBER );
|
|
|
|
|
2014-02-11 18:35:39 +01:00
|
|
|
#ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD
|
2014-02-11 21:21:32 +01:00
|
|
|
# ifdef _WIN32
|
2014-02-11 18:35:39 +01:00
|
|
|
SOCKET UdpSocket;
|
2014-02-11 21:21:32 +01:00
|
|
|
# else
|
|
|
|
int UdpSocket;
|
|
|
|
# endif
|
2014-02-11 18:35:39 +01:00
|
|
|
#endif
|
2013-01-23 11:41:13 +01:00
|
|
|
QUdpSocket SocketDevice;
|
2014-02-11 18:35:39 +01:00
|
|
|
|
2013-01-23 11:41:13 +01:00
|
|
|
QMutex Mutex;
|
|
|
|
|
|
|
|
CVector<uint8_t> vecbyRecBuf;
|
|
|
|
CHostAddress RecHostAddr;
|
2014-01-23 21:33:55 +01:00
|
|
|
QHostAddress SenderAddress;
|
|
|
|
quint16 SenderPort;
|
2013-01-23 11:41:13 +01:00
|
|
|
|
2013-05-10 21:46:59 +02:00
|
|
|
CChannel* pChannel; // for client
|
|
|
|
CServer* pServer; // for server
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
bool bIsClient;
|
|
|
|
|
2014-01-03 09:54:49 +01:00
|
|
|
bool bJitterBufferOK;
|
|
|
|
|
2013-01-23 11:41:13 +01:00
|
|
|
public slots:
|
|
|
|
void OnDataReceived();
|
2013-05-10 21:34:55 +02:00
|
|
|
|
|
|
|
signals:
|
2014-02-11 18:35:39 +01:00
|
|
|
#ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD
|
|
|
|
void DetectedCLMessage ( CVector<uint8_t> vecbyMesBodyData,
|
|
|
|
int iRecID );
|
|
|
|
|
|
|
|
void ParseMessageBody ( CVector<uint8_t> vecbyMesBodyData,
|
|
|
|
int iRecCounter,
|
|
|
|
int iRecID );
|
2014-02-13 22:03:33 +01:00
|
|
|
|
|
|
|
void NewConnection();
|
2014-02-11 18:35:39 +01:00
|
|
|
#endif
|
2013-05-10 21:34:55 +02:00
|
|
|
void InvalidPacketReceived ( CVector<uint8_t> vecbyRecBuf,
|
|
|
|
int iNumBytesRead,
|
|
|
|
CHostAddress RecHostAddr );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-06-03 18:07:17 +02:00
|
|
|
#ifdef ENABLE_RECEIVE_SOCKET_IN_SEPARATE_THREAD
|
2013-05-10 21:34:55 +02:00
|
|
|
/* Socket which runs in a separate high priority thread ----------------------*/
|
2013-06-03 18:07:17 +02:00
|
|
|
class CHighPrioSocket : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
CHighPrioSocket ( CChannel* pNewChannel,
|
2014-02-13 19:38:25 +01:00
|
|
|
const quint16 iPortNumber ) :
|
|
|
|
Socket ( pNewChannel, iPortNumber )
|
2013-06-03 18:07:17 +02:00
|
|
|
{
|
2013-08-18 09:42:32 +02:00
|
|
|
// 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
|
2014-02-13 19:38:25 +01:00
|
|
|
Socket.moveToThread ( &NetworkWorkerThread );
|
2014-02-01 16:14:47 +01:00
|
|
|
|
2014-02-13 19:38:25 +01:00
|
|
|
NetworkWorkerThread.SetSocket ( &Socket );
|
2014-02-01 16:14:47 +01:00
|
|
|
|
2013-08-18 09:42:32 +02:00
|
|
|
NetworkWorkerThread.start ( QThread::TimeCriticalPriority );
|
|
|
|
|
|
|
|
// connect the "InvalidPacketReceived" signal
|
2014-02-13 19:38:25 +01:00
|
|
|
QObject::connect ( &Socket,
|
2013-06-03 18:07:17 +02:00
|
|
|
SIGNAL ( InvalidPacketReceived ( CVector<uint8_t>, int, CHostAddress ) ),
|
|
|
|
SIGNAL ( InvalidPacketReceived ( CVector<uint8_t>, int, CHostAddress ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~CHighPrioSocket()
|
|
|
|
{
|
|
|
|
NetworkWorkerThread.exit();
|
2013-05-10 21:34:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SendPacket ( const CVector<uint8_t>& vecbySendBuf,
|
|
|
|
const CHostAddress& HostAddr )
|
|
|
|
{
|
2014-02-13 19:38:25 +01:00
|
|
|
Socket.SendPacket ( vecbySendBuf, HostAddr );
|
2013-05-10 21:34:55 +02:00
|
|
|
}
|
|
|
|
|
2014-01-10 21:00:45 +01:00
|
|
|
bool GetAndResetbJitterBufferOKFlag()
|
|
|
|
{
|
2014-02-13 19:38:25 +01:00
|
|
|
return Socket.GetAndResetbJitterBufferOKFlag();
|
2014-01-10 21:00:45 +01:00
|
|
|
}
|
|
|
|
|
2013-05-10 21:34:55 +02:00
|
|
|
protected:
|
2014-02-01 16:14:47 +01:00
|
|
|
class CSocketThread : public QThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CSocketThread ( CSocket* pNewSocket = NULL, QObject* parent = 0 ) :
|
2014-02-11 18:47:48 +01:00
|
|
|
QThread ( parent ), pSocket ( pNewSocket ), bRun ( true ) {}
|
2014-02-01 16:14:47 +01:00
|
|
|
|
2014-02-13 19:38:25 +01:00
|
|
|
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 );
|
2014-02-01 16:14:47 +01:00
|
|
|
}
|
|
|
|
|
2014-02-13 19:38:25 +01:00
|
|
|
void SetSocket ( CSocket* pNewSocket ) { pSocket = pNewSocket; }
|
|
|
|
|
2014-02-01 16:14:47 +01:00
|
|
|
protected:
|
|
|
|
void run() {
|
2014-02-13 19:38:25 +01:00
|
|
|
// make sure the socket pointer is initialized (should be always the
|
|
|
|
// case)
|
2014-02-01 16:14:47 +01:00
|
|
|
if ( pSocket != NULL )
|
|
|
|
{
|
|
|
|
while ( bRun )
|
|
|
|
{
|
2014-02-13 19:38:25 +01:00
|
|
|
// this function is a blocking function (waiting for network
|
|
|
|
// packets to be received and processed)
|
2014-02-01 16:14:47 +01:00
|
|
|
pSocket->OnDataReceived();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CSocket* pSocket;
|
|
|
|
bool bRun;
|
|
|
|
};
|
|
|
|
|
|
|
|
CSocketThread NetworkWorkerThread;
|
2014-02-13 19:38:25 +01:00
|
|
|
CSocket Socket;
|
2013-05-10 21:34:55 +02:00
|
|
|
|
2013-06-03 18:07:17 +02:00
|
|
|
signals:
|
|
|
|
void InvalidPacketReceived ( CVector<uint8_t> vecbyRecBuf,
|
|
|
|
int iNumBytesRead,
|
|
|
|
CHostAddress RecHostAddr );
|
2013-01-23 11:41:13 +01:00
|
|
|
};
|
2013-06-03 18:07:17 +02:00
|
|
|
#endif
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
#endif /* !defined ( SOCKET_HOIHGE76GEKJH98_3_4344_BB23945IUHF1912__INCLUDED_ ) */
|