2013-01-23 11:41:13 +01:00
|
|
|
/******************************************************************************\
|
|
|
|
* Copyright (c) 2004-2013
|
|
|
|
*
|
|
|
|
* 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 "channel.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
// The header file server.h requires to include this header file so we get a
|
|
|
|
// cyclic dependency. To solve this issue, a prototype of the server class is
|
|
|
|
// defined here.
|
|
|
|
class CServer; // forward declaration of CServer
|
|
|
|
|
|
|
|
|
|
|
|
/* 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 )
|
2013-05-10 21:46:59 +02:00
|
|
|
: pChannel( pNewChannel ), bIsClient ( true ) { Init ( iPortNumber ); }
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
CSocket ( CServer* pNServP,
|
|
|
|
const quint16 iPortNumber )
|
|
|
|
: pServer ( pNServP ), bIsClient ( false ) { Init ( iPortNumber ); }
|
|
|
|
|
|
|
|
void SendPacket ( const CVector<uint8_t>& vecbySendBuf,
|
|
|
|
const CHostAddress& HostAddr );
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void Init ( const quint16 iPortNumber = LLCON_DEFAULT_PORT_NUMBER );
|
|
|
|
|
|
|
|
QUdpSocket SocketDevice;
|
|
|
|
QMutex Mutex;
|
|
|
|
|
|
|
|
CVector<uint8_t> vecbyRecBuf;
|
|
|
|
CHostAddress RecHostAddr;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void OnDataReceived();
|
2013-05-10 21:34:55 +02:00
|
|
|
|
|
|
|
signals:
|
|
|
|
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,
|
|
|
|
const quint16 iPortNumber )
|
|
|
|
{
|
|
|
|
|
|
|
|
// TEST we have to register some classes to the Qt signal/slot mechanism
|
|
|
|
// since now we actually have thread crossings with the new code
|
|
|
|
qRegisterMetaType<CVector<uint8_t> > ( "CVector<uint8_t>" );
|
|
|
|
qRegisterMetaType<CHostAddress> ( "CHostAddress" );
|
2013-05-10 21:34:55 +02:00
|
|
|
|
|
|
|
|
2013-06-03 18:07:17 +02:00
|
|
|
// TEST actual creation of the new thread, see internet references below:
|
2013-05-10 21:34:55 +02:00
|
|
|
// 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
|
|
|
|
|
2013-06-03 18:07:17 +02:00
|
|
|
pSocket = new CSocket ( pNewChannel, iPortNumber );
|
|
|
|
pSocket->moveToThread ( &NetworkWorkerThread );
|
|
|
|
NetworkWorkerThread.start ( QThread::TimeCriticalPriority );
|
2013-05-10 21:34:55 +02:00
|
|
|
|
|
|
|
|
2013-06-03 18:07:17 +02:00
|
|
|
QObject::connect ( pSocket,
|
|
|
|
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 )
|
|
|
|
{
|
2013-06-03 18:07:17 +02:00
|
|
|
pSocket->SendPacket ( vecbySendBuf, HostAddr );
|
2013-05-10 21:34:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2013-06-03 18:07:17 +02:00
|
|
|
QThread NetworkWorkerThread;
|
|
|
|
CSocket* pSocket;
|
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_ ) */
|