2011-04-23 22:44:56 +02:00
|
|
|
/******************************************************************************\
|
2020-01-01 15:41:43 +01:00
|
|
|
* Copyright (c) 2004-2020
|
2011-04-23 22:44:56 +02: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
|
|
|
|
*
|
|
|
|
\******************************************************************************/
|
|
|
|
|
|
|
|
#include "socket.h"
|
|
|
|
#include "server.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Implementation *************************************************************/
|
|
|
|
void CSocket::Init ( const quint16 iPortNumber )
|
|
|
|
{
|
2014-02-16 09:12:07 +01:00
|
|
|
#ifdef _WIN32
|
2014-02-11 18:35:39 +01:00
|
|
|
// for the Windows socket usage we have to start it up first
|
2014-02-16 09:12:07 +01:00
|
|
|
|
|
|
|
// TODO check for error and exit application on error
|
|
|
|
|
2014-02-11 18:35:39 +01:00
|
|
|
WSADATA wsa;
|
2014-02-16 09:12:07 +01:00
|
|
|
WSAStartup ( MAKEWORD(1, 0), &wsa );
|
|
|
|
#endif
|
2014-02-11 18:35:39 +01:00
|
|
|
|
|
|
|
// create the UDP socket
|
|
|
|
UdpSocket = socket ( AF_INET, SOCK_DGRAM, 0 );
|
|
|
|
|
2011-04-23 22:44:56 +02:00
|
|
|
// allocate memory for network receive and send buffer in samples
|
|
|
|
vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF );
|
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
// preinitialize socket in address (only the port number is missing)
|
|
|
|
sockaddr_in UdpSocketInAddr;
|
|
|
|
UdpSocketInAddr.sin_family = AF_INET;
|
|
|
|
UdpSocketInAddr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
2011-04-23 22:44:56 +02:00
|
|
|
// initialize the listening socket
|
|
|
|
bool bSuccess;
|
2013-05-10 09:57:21 +02:00
|
|
|
|
2011-04-23 22:44:56 +02:00
|
|
|
if ( bIsClient )
|
|
|
|
{
|
|
|
|
// Per definition use the port number plus ten for the client to make
|
|
|
|
// it possible to run server and client on the same computer. If the
|
|
|
|
// port is not available, try "NUM_SOCKET_PORTS_TO_TRY" times with
|
|
|
|
// incremented port numbers
|
|
|
|
quint16 iClientPortIncrement = 10; // start value: port nubmer plus ten
|
|
|
|
bSuccess = false; // initialization for while loop
|
2014-01-23 21:33:55 +01:00
|
|
|
|
2014-02-11 18:35:39 +01:00
|
|
|
while ( !bSuccess &&
|
|
|
|
( iClientPortIncrement <= NUM_SOCKET_PORTS_TO_TRY ) )
|
|
|
|
{
|
|
|
|
UdpSocketInAddr.sin_port = htons ( iPortNumber + iClientPortIncrement );
|
|
|
|
|
2018-02-25 09:50:58 +01:00
|
|
|
bSuccess = ( ::bind ( UdpSocket ,
|
|
|
|
(sockaddr*) &UdpSocketInAddr,
|
|
|
|
sizeof ( sockaddr_in ) ) == 0 );
|
2014-02-11 18:35:39 +01:00
|
|
|
|
|
|
|
iClientPortIncrement++;
|
|
|
|
}
|
2011-04-23 22:44:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// for the server, only try the given port number and do not try out
|
|
|
|
// other port numbers to bind since it is imporatant that the server
|
|
|
|
// gets the desired port number
|
2014-02-16 09:12:07 +01:00
|
|
|
UdpSocketInAddr.sin_port = htons ( iPortNumber );
|
|
|
|
|
2018-02-25 09:50:58 +01:00
|
|
|
bSuccess = ( ::bind ( UdpSocket ,
|
|
|
|
(sockaddr*) &UdpSocketInAddr,
|
|
|
|
sizeof ( sockaddr_in ) ) == 0 );
|
2011-04-23 22:44:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !bSuccess )
|
|
|
|
{
|
|
|
|
// we cannot bind socket, throw error
|
|
|
|
throw CGenErr ( "Cannot bind the socket (maybe "
|
|
|
|
"the software is already running).", "Network Error" );
|
|
|
|
}
|
|
|
|
|
2014-02-11 18:35:39 +01:00
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
// Connections -------------------------------------------------------------
|
|
|
|
// it is important to do the following connections in this class since we
|
|
|
|
// have a thread transition
|
2014-02-11 18:35:39 +01:00
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
// we have different connections for client and server
|
|
|
|
if ( bIsClient )
|
|
|
|
{
|
|
|
|
// client connections:
|
2014-02-11 18:35:39 +01:00
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
QObject::connect ( this,
|
|
|
|
SIGNAL ( ProtcolMessageReceived ( int, int, CVector<uint8_t>, CHostAddress ) ),
|
|
|
|
pChannel, SLOT ( OnProtcolMessageReceived ( int, int, CVector<uint8_t>, CHostAddress ) ) );
|
2014-02-11 18:35:39 +01:00
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
QObject::connect ( this,
|
|
|
|
SIGNAL ( ProtcolCLMessageReceived ( int, CVector<uint8_t>, CHostAddress ) ),
|
|
|
|
pChannel, SLOT ( OnProtcolCLMessageReceived ( int, CVector<uint8_t>, CHostAddress ) ) );
|
2014-02-11 18:35:39 +01:00
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
QObject::connect ( this,
|
|
|
|
SIGNAL ( NewConnection() ),
|
|
|
|
pChannel, SLOT ( OnNewConnection() ) );
|
2013-08-18 17:22:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-16 09:12:07 +01:00
|
|
|
// server connections:
|
|
|
|
|
|
|
|
QObject::connect ( this,
|
|
|
|
SIGNAL ( ProtcolMessageReceived ( int, int, CVector<uint8_t>, CHostAddress ) ),
|
|
|
|
pServer, SLOT ( OnProtcolMessageReceived ( int, int, CVector<uint8_t>, CHostAddress ) ) );
|
|
|
|
|
|
|
|
QObject::connect ( this,
|
|
|
|
SIGNAL ( ProtcolCLMessageReceived ( int, CVector<uint8_t>, CHostAddress ) ),
|
|
|
|
pServer, SLOT ( OnProtcolCLMessageReceived ( int, CVector<uint8_t>, CHostAddress ) ) );
|
|
|
|
|
|
|
|
QObject::connect ( this,
|
|
|
|
SIGNAL ( NewConnection ( int, CHostAddress ) ),
|
|
|
|
pServer, SLOT ( OnNewConnection ( int, CHostAddress ) ) );
|
|
|
|
|
|
|
|
QObject::connect ( this,
|
|
|
|
SIGNAL ( ServerFull ( CHostAddress ) ),
|
|
|
|
pServer, SLOT ( OnServerFull ( CHostAddress ) ) );
|
2013-08-18 17:22:18 +02:00
|
|
|
}
|
2011-04-23 22:44:56 +02:00
|
|
|
}
|
|
|
|
|
2014-02-13 19:38:25 +01:00
|
|
|
void CSocket::Close()
|
|
|
|
{
|
2014-02-17 19:34:39 +01:00
|
|
|
#ifdef _WIN32
|
2014-02-13 19:38:25 +01:00
|
|
|
// closesocket will cause recvfrom to return with an error because the
|
|
|
|
// socket is closed -> then the thread can safely be shut down
|
2014-02-17 19:34:39 +01:00
|
|
|
closesocket ( UdpSocket );
|
2014-02-18 20:36:09 +01:00
|
|
|
#elif defined ( __APPLE__ ) || defined ( __MACOSX )
|
|
|
|
// on Mac the general close has the same effect as closesocket on Windows
|
|
|
|
close ( UdpSocket );
|
2014-02-16 09:12:07 +01:00
|
|
|
#else
|
2014-02-18 20:36:09 +01:00
|
|
|
// on Linux the shutdown call cancels the recvfrom
|
2014-02-17 19:24:32 +01:00
|
|
|
shutdown ( UdpSocket, SHUT_RDWR );
|
2014-02-13 19:38:25 +01:00
|
|
|
#endif
|
2014-02-16 09:12:07 +01:00
|
|
|
}
|
2014-02-13 19:38:25 +01:00
|
|
|
|
2014-02-11 18:35:39 +01:00
|
|
|
CSocket::~CSocket()
|
|
|
|
{
|
2014-02-17 19:24:32 +01:00
|
|
|
// cleanup the socket (on Windows the WSA cleanup must also be called)
|
2014-02-16 09:12:07 +01:00
|
|
|
#ifdef _WIN32
|
2014-02-17 19:24:32 +01:00
|
|
|
closesocket ( UdpSocket );
|
2014-02-11 18:35:39 +01:00
|
|
|
WSACleanup();
|
2014-02-17 19:24:32 +01:00
|
|
|
#else
|
|
|
|
close ( UdpSocket );
|
2014-02-11 18:35:39 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-04-23 22:44:56 +02:00
|
|
|
void CSocket::SendPacket ( const CVector<uint8_t>& vecbySendBuf,
|
|
|
|
const CHostAddress& HostAddr )
|
|
|
|
{
|
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
|
|
|
const int iVecSizeOut = vecbySendBuf.Size();
|
|
|
|
|
2020-04-03 21:45:36 +02:00
|
|
|
if ( iVecSizeOut > 0 )
|
2011-04-23 22:44:56 +02:00
|
|
|
{
|
|
|
|
// send packet through network (we have to convert the constant unsigned
|
|
|
|
// char vector in "const char*", for this we first convert the const
|
|
|
|
// uint8_t vector in a read/write uint8_t vector and then do the cast to
|
|
|
|
// const char*)
|
2014-02-16 09:12:07 +01:00
|
|
|
sockaddr_in UdpSocketOutAddr;
|
|
|
|
|
|
|
|
UdpSocketOutAddr.sin_family = AF_INET;
|
|
|
|
UdpSocketOutAddr.sin_port = htons ( HostAddr.iPort );
|
|
|
|
UdpSocketOutAddr.sin_addr.s_addr = htonl ( HostAddr.InetAddr.toIPv4Address() );
|
|
|
|
|
|
|
|
sendto ( UdpSocket,
|
|
|
|
(const char*) &( (CVector<uint8_t>) vecbySendBuf )[0],
|
|
|
|
iVecSizeOut,
|
|
|
|
0,
|
|
|
|
(sockaddr*) &UdpSocketOutAddr,
|
|
|
|
sizeof ( sockaddr_in ) );
|
2011-04-23 22:44:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 09:54:49 +01:00
|
|
|
bool CSocket::GetAndResetbJitterBufferOKFlag()
|
|
|
|
{
|
|
|
|
// check jitter buffer status
|
|
|
|
if ( !bJitterBufferOK )
|
|
|
|
{
|
|
|
|
// reset flag and return "not OK" status
|
|
|
|
bJitterBufferOK = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the buffer was OK, we do not have to reset anything and just return the
|
|
|
|
// OK status
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-23 22:44:56 +02:00
|
|
|
void CSocket::OnDataReceived()
|
|
|
|
{
|
2014-02-16 09:12:07 +01:00
|
|
|
/*
|
|
|
|
The strategy of this function is that only the "put audio" function is
|
|
|
|
called directly (i.e. the high thread priority is used) and all other less
|
|
|
|
important things like protocol parsing and acting on protocol messages is
|
|
|
|
done in the low priority thread. To get a thread transition, we have to
|
|
|
|
use the signal/slot mechanism (i.e. we use messages for that).
|
|
|
|
*/
|
|
|
|
|
|
|
|
// read block from network interface and query address of sender
|
|
|
|
sockaddr_in SenderAddr;
|
2014-02-11 21:39:07 +01:00
|
|
|
#ifdef _WIN32
|
2014-02-16 09:12:07 +01:00
|
|
|
int SenderAddrSize = sizeof ( sockaddr_in );
|
2014-02-11 21:39:07 +01:00
|
|
|
#else
|
2014-02-16 09:12:07 +01:00
|
|
|
socklen_t SenderAddrSize = sizeof ( sockaddr_in );
|
2014-02-11 21:39:07 +01:00
|
|
|
#endif
|
2014-02-11 18:35:39 +01:00
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
const long iNumBytesRead = recvfrom ( UdpSocket,
|
|
|
|
(char*) &vecbyRecBuf[0],
|
|
|
|
MAX_SIZE_BYTES_NETW_BUF,
|
|
|
|
0,
|
|
|
|
(sockaddr*) &SenderAddr,
|
|
|
|
&SenderAddrSize );
|
|
|
|
|
|
|
|
// check if an error occurred or no data could be read
|
|
|
|
if ( iNumBytesRead <= 0 )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert address of client
|
|
|
|
RecHostAddr.InetAddr.setAddress ( ntohl ( SenderAddr.sin_addr.s_addr ) );
|
|
|
|
RecHostAddr.iPort = ntohs ( SenderAddr.sin_port );
|
|
|
|
|
2011-04-23 22:44:56 +02:00
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
// check if this is a protocol message
|
|
|
|
int iRecCounter;
|
|
|
|
int iRecID;
|
|
|
|
CVector<uint8_t> vecbyMesBodyData;
|
|
|
|
|
|
|
|
if ( !CProtocol::ParseMessageFrame ( vecbyRecBuf,
|
|
|
|
iNumBytesRead,
|
|
|
|
vecbyMesBodyData,
|
|
|
|
iRecCounter,
|
|
|
|
iRecID ) )
|
|
|
|
{
|
|
|
|
// this is a protocol message, check the type of the message
|
|
|
|
if ( CProtocol::IsConnectionLessMessageID ( iRecID ) )
|
2011-04-23 22:44:56 +02:00
|
|
|
{
|
2014-02-16 09:12:07 +01:00
|
|
|
|
|
|
|
// TODO a copy of the vector is used -> avoid malloc in real-time routine
|
|
|
|
|
|
|
|
emit ProtcolCLMessageReceived ( iRecID, vecbyMesBodyData, RecHostAddr );
|
2011-04-23 22:44:56 +02:00
|
|
|
}
|
2014-02-16 09:12:07 +01:00
|
|
|
else
|
|
|
|
{
|
2011-04-23 22:44:56 +02:00
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
// TODO a copy of the vector is used -> avoid malloc in real-time routine
|
2011-04-23 22:44:56 +02:00
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
emit ProtcolMessageReceived ( iRecCounter, iRecID, vecbyMesBodyData, RecHostAddr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// this is most probably a regular audio packet
|
2011-04-23 22:44:56 +02:00
|
|
|
if ( bIsClient )
|
|
|
|
{
|
|
|
|
// client:
|
2013-05-10 21:34:55 +02:00
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
switch ( pChannel->PutAudioData ( vecbyRecBuf, iNumBytesRead, RecHostAddr ) )
|
2011-04-23 22:44:56 +02:00
|
|
|
{
|
2014-02-16 09:12:07 +01:00
|
|
|
case PS_AUDIO_ERR:
|
|
|
|
case PS_GEN_ERROR:
|
|
|
|
bJitterBufferOK = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_NEW_CONNECTION:
|
|
|
|
// inform other objects that new connection was established
|
|
|
|
emit NewConnection();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_AUDIO_INVALID:
|
2013-05-10 21:34:55 +02:00
|
|
|
// inform about received invalid packet by fireing an event
|
2014-02-16 09:12:07 +01:00
|
|
|
emit InvalidPacketReceived ( RecHostAddr );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
break;
|
2011-04-23 22:44:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// server:
|
2013-05-10 21:34:55 +02:00
|
|
|
|
2014-02-16 09:12:07 +01:00
|
|
|
int iCurChanID;
|
|
|
|
|
|
|
|
if ( pServer->PutAudioData ( vecbyRecBuf, iNumBytesRead, RecHostAddr, iCurChanID ) )
|
2011-04-23 22:44:56 +02:00
|
|
|
{
|
2014-02-16 09:12:07 +01:00
|
|
|
// we have a new connection, emit a signal
|
|
|
|
emit NewConnection ( iCurChanID, RecHostAddr );
|
|
|
|
|
2014-01-06 14:04:26 +01:00
|
|
|
// this was an audio packet, start server if it is in sleep mode
|
|
|
|
if ( !pServer->IsRunning() )
|
|
|
|
{
|
|
|
|
// (note that Qt will delete the event object when done)
|
|
|
|
QCoreApplication::postEvent ( pServer,
|
|
|
|
new CCustomEvent ( MS_PACKET_RECEIVED, 0, 0 ) );
|
|
|
|
}
|
2011-04-23 22:44:56 +02:00
|
|
|
}
|
2014-02-16 09:12:07 +01:00
|
|
|
|
|
|
|
// check if no channel is available
|
|
|
|
if ( iCurChanID == INVALID_CHANNEL_ID )
|
|
|
|
{
|
|
|
|
// fire message for the state that no free channel is available
|
|
|
|
emit ServerFull ( RecHostAddr );
|
|
|
|
}
|
2011-04-23 22:44:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|