2006-12-18 15:39:33 +01:00
|
|
|
/******************************************************************************\
|
2008-01-02 23:16:38 +01:00
|
|
|
* Copyright (c) 2004-2008
|
2006-12-18 15:39:33 +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
|
|
|
|
*
|
|
|
|
\******************************************************************************/
|
|
|
|
|
|
|
|
#include "socket.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Implementation *************************************************************/
|
2008-07-22 17:17:19 +02:00
|
|
|
void CSocket::Init ( const quint16 iPortNumber )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-12-31 14:09:12 +01:00
|
|
|
// allocate memory for network receive and send buffer in samples
|
2006-12-18 15:39:33 +01:00
|
|
|
vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF );
|
|
|
|
|
2007-12-31 14:09:12 +01:00
|
|
|
// initialize the listening socket
|
2006-12-18 15:39:33 +01:00
|
|
|
bool bSuccess = SocketDevice.bind (
|
2008-07-22 17:17:19 +02:00
|
|
|
QHostAddress ( QHostAddress::Any ), iPortNumber );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2008-07-22 17:17:19 +02:00
|
|
|
// if no success, try if server is on same machine (only for client)
|
|
|
|
if ( ( !bSuccess ) && bIsClient )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-07-22 17:17:19 +02:00
|
|
|
// if server and client is on same machine, decrease port number by
|
|
|
|
// one by definition
|
|
|
|
bSuccess = SocketDevice.bind (
|
|
|
|
QHostAddress( QHostAddress::Any ), iPortNumber - 1 );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !bSuccess )
|
|
|
|
{
|
2008-07-22 17:17:19 +02:00
|
|
|
// we cannot bind socket, throw error
|
|
|
|
throw CGenErr ( "Cannot bind the socket (maybe "
|
|
|
|
"the software is already running).", "Network Error" );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-12-31 14:09:12 +01:00
|
|
|
// connect the "activated" signal
|
2008-01-17 19:56:43 +01:00
|
|
|
QObject::connect ( &SocketDevice, SIGNAL ( readyRead() ),
|
2007-09-08 12:45:14 +02:00
|
|
|
this, SLOT ( OnDataReceived() ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSocket::SendPacket ( const CVector<unsigned char>& vecbySendBuf,
|
|
|
|
const CHostAddress& HostAddr )
|
2006-01-28 12:29:22 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
const int iVecSizeOut = vecbySendBuf.Size();
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( iVecSizeOut != 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-01-02 23:16:38 +01:00
|
|
|
// send packet through network (we have to convert the constant unsigned
|
|
|
|
// char vector in "const char*", for this we first convert the const
|
|
|
|
// unsigned char vector in a read/write unsigned char vector and then
|
|
|
|
// do the cast to const char*)
|
2008-01-17 19:56:43 +01:00
|
|
|
SocketDevice.writeDatagram (
|
2006-12-18 15:39:33 +01:00
|
|
|
(const char*) &( (CVector<unsigned char>) vecbySendBuf )[0],
|
2006-11-25 15:46:57 +01:00
|
|
|
iVecSizeOut, HostAddr.InetAddr, HostAddr.iPort );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
void CSocket::OnDataReceived()
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-01-27 11:05:15 +01:00
|
|
|
while ( SocketDevice.hasPendingDatagrams() )
|
|
|
|
{
|
|
|
|
QHostAddress SenderAddress;
|
|
|
|
quint16 SenderPort;
|
|
|
|
|
|
|
|
// read block from network interface and query address of sender
|
2008-01-27 12:51:59 +01:00
|
|
|
int iNumBytesRead = SocketDevice.readDatagram ( (char*) &vecbyRecBuf[0],
|
2008-01-27 11:05:15 +01:00
|
|
|
MAX_SIZE_BYTES_NETW_BUF, &SenderAddress, &SenderPort );
|
|
|
|
|
|
|
|
// check if an error occurred
|
|
|
|
if ( iNumBytesRead < 0 )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-27 12:51:59 +01:00
|
|
|
// convert address of client
|
|
|
|
const CHostAddress RecHostAddr ( SenderAddress, SenderPort );
|
|
|
|
|
|
|
|
if ( bIsClient )
|
2008-01-27 11:05:15 +01:00
|
|
|
{
|
|
|
|
// client
|
|
|
|
// check if packet comes from the server we want to connect
|
|
|
|
if ( ! ( pChannel->GetAddress() == RecHostAddr ) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( pChannel->PutData ( vecbyRecBuf, iNumBytesRead ) )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// server
|
|
|
|
if ( pChannelSet->PutData ( vecbyRecBuf, iNumBytesRead, RecHostAddr ) )
|
|
|
|
{
|
|
|
|
// this was an audio packet, start server
|
|
|
|
// tell the server object to wake up if it
|
|
|
|
// is in sleep mode (Qt will delete the event object when done)
|
|
|
|
QCoreApplication::postEvent ( pServer,
|
|
|
|
new CLlconEvent ( MS_PACKET_RECEIVED, 0, 0 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|