2006-01-28 12:29:22 +01:00
|
|
|
/******************************************************************************\
|
|
|
|
* Copyright (c) 2004-2006
|
|
|
|
*
|
|
|
|
* 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 *************************************************************/
|
2006-02-12 15:26:46 +01:00
|
|
|
void CSocket::Init ()
|
2006-01-28 12:29:22 +01:00
|
|
|
{
|
|
|
|
/* allocate memory for network receive and send buffer in samples */
|
2006-02-12 15:26:46 +01:00
|
|
|
vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
|
|
|
/* initialize the listening socket */
|
2006-02-12 15:26:46 +01:00
|
|
|
bool bSuccess = SocketDevice.bind (
|
|
|
|
QHostAddress ( (Q_UINT32) 0 ) /* INADDR_ANY */, LLCON_PORT_NUMBER );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2006-02-12 15:26:46 +01:00
|
|
|
if ( bIsClient )
|
2006-01-28 12:29:22 +01:00
|
|
|
{
|
|
|
|
/* if no success, try if server is on same machine (only for client) */
|
2006-02-12 15:26:46 +01:00
|
|
|
if ( !bSuccess )
|
2006-01-28 12:29:22 +01:00
|
|
|
{
|
|
|
|
/* if server and client is on same machine, decrease port number by
|
|
|
|
one by definition */
|
2006-02-12 15:26:46 +01:00
|
|
|
bSuccess = SocketDevice.bind (
|
|
|
|
QHostAddress( (Q_UINT32) 0 ) /* INADDR_ANY */,
|
|
|
|
LLCON_PORT_NUMBER - 1 );
|
2006-01-28 12:29:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-12 15:26:46 +01:00
|
|
|
if ( !bSuccess )
|
2006-01-28 12:29:22 +01:00
|
|
|
{
|
|
|
|
/* show error message */
|
2006-02-12 15:26:46 +01:00
|
|
|
QMessageBox::critical ( 0, "Network Error", "Cannot bind the socket.",
|
|
|
|
QMessageBox::Ok, QMessageBox::NoButton );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
|
|
|
/* exit application */
|
2006-02-12 15:26:46 +01:00
|
|
|
exit ( 1 );
|
2006-01-28 12:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSocketNotifier* pSocketNotivRead =
|
2006-02-12 15:26:46 +01:00
|
|
|
new QSocketNotifier ( SocketDevice.socket (), QSocketNotifier::Read );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
|
|
|
/* connect the "activated" signal */
|
2006-02-12 15:26:46 +01:00
|
|
|
QObject::connect ( pSocketNotivRead, SIGNAL ( activated ( int ) ),
|
|
|
|
this, SLOT ( OnDataReceived () ) );
|
2006-01-28 12:29:22 +01:00
|
|
|
}
|
|
|
|
|
2006-02-26 14:27:19 +01:00
|
|
|
void CSocket::SendPacket ( const CVector<unsigned char>& vecbySendBuf,
|
|
|
|
const CHostAddress& HostAddr )
|
2006-01-28 12:29:22 +01:00
|
|
|
{
|
2006-02-12 15:26:46 +01:00
|
|
|
const int iVecSizeOut = vecbySendBuf.Size ();
|
2006-01-28 12:29:22 +01:00
|
|
|
|
|
|
|
if ( iVecSizeOut != 0 )
|
|
|
|
{
|
|
|
|
/* send packet through network */
|
2006-02-12 15:26:46 +01:00
|
|
|
SocketDevice.writeBlock (
|
2006-02-26 11:50:47 +01:00
|
|
|
(const char*) &( (CVector<unsigned char>) vecbySendBuf )[0],
|
2006-02-12 15:26:46 +01:00
|
|
|
iVecSizeOut, HostAddr.InetAddr, HostAddr.iPort );
|
2006-01-28 12:29:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-12 15:26:46 +01:00
|
|
|
void CSocket::OnDataReceived ()
|
2006-01-28 12:29:22 +01:00
|
|
|
{
|
|
|
|
/* read block from network interface */
|
2006-02-12 15:26:46 +01:00
|
|
|
const int iNumBytesRead = SocketDevice.readBlock( (char*) &vecbyRecBuf[0],
|
2006-02-26 11:50:47 +01:00
|
|
|
MAX_SIZE_BYTES_NETW_BUF );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
|
|
|
/* check if an error occurred */
|
2006-02-12 15:26:46 +01:00
|
|
|
if ( iNumBytesRead < 0 )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2006-01-28 12:29:22 +01:00
|
|
|
|
|
|
|
/* get host address of client */
|
2006-02-12 15:26:46 +01:00
|
|
|
CHostAddress RecHostAddr ( SocketDevice.peerAddress (),
|
|
|
|
SocketDevice.peerPort () );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2006-02-12 15:26:46 +01:00
|
|
|
if ( bIsClient )
|
2006-01-28 12:29:22 +01:00
|
|
|
{
|
|
|
|
/* client */
|
|
|
|
/* check if packet comes from the server we want to connect */
|
2006-02-17 20:07:10 +01:00
|
|
|
if ( ! ( pChannel->GetAddress () == RecHostAddr ) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2006-03-06 18:04:07 +01:00
|
|
|
switch ( pChannel->PutData( vecbyRecBuf, iNumBytesRead ) )
|
|
|
|
{
|
|
|
|
case PS_AUDIO_OK:
|
2006-02-12 15:26:46 +01:00
|
|
|
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_GREEN );
|
2006-03-06 18:04:07 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_AUDIO_ERR:
|
|
|
|
case PS_GEN_ERROR:
|
2006-02-12 15:26:46 +01:00
|
|
|
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_RED );
|
2006-03-06 18:04:07 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_PROT_ERR:
|
|
|
|
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_YELLOW );
|
|
|
|
break;
|
|
|
|
}
|
2006-01-28 12:29:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-12 15:26:46 +01:00
|
|
|
/* server */
|
2006-03-07 22:52:21 +01:00
|
|
|
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)
|
|
|
|
QThread::postEvent ( pServer,
|
|
|
|
new CLlconEvent ( MS_PACKET_RECEIVED, 0, 0 ) );
|
|
|
|
}
|
2006-01-28 12:29:22 +01:00
|
|
|
}
|
|
|
|
}
|