jamulus/src/socket.cpp

130 lines
3.6 KiB
C++
Raw Normal View History

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 *************************************************************/
void CSocket::Init ()
2006-01-28 12:29:22 +01:00
{
/* allocate memory for network receive and send buffer in samples */
vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF );
2006-01-28 12:29:22 +01:00
/* initialize the listening socket */
bool bSuccess = SocketDevice.bind (
QHostAddress ( (Q_UINT32) 0 ) /* INADDR_ANY */, LLCON_PORT_NUMBER );
2006-01-28 12:29:22 +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) */
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 */
bSuccess = SocketDevice.bind (
QHostAddress( (Q_UINT32) 0 ) /* INADDR_ANY */,
LLCON_PORT_NUMBER - 1 );
2006-01-28 12:29:22 +01:00
}
}
if ( !bSuccess )
2006-01-28 12:29:22 +01:00
{
/* show error message */
QMessageBox::critical ( 0, "Network Error", "Cannot bind the socket.",
QMessageBox::Ok, QMessageBox::NoButton );
2006-01-28 12:29:22 +01:00
/* exit application */
exit ( 1 );
2006-01-28 12:29:22 +01:00
}
QSocketNotifier* pSocketNotivRead =
new QSocketNotifier ( SocketDevice.socket (), QSocketNotifier::Read );
2006-01-28 12:29:22 +01:00
/* connect the "activated" signal */
QObject::connect ( pSocketNotivRead, SIGNAL ( activated ( int ) ),
this, SLOT ( OnDataReceived () ) );
2006-01-28 12:29:22 +01:00
}
void CSocket::SendPacket( const CVector<unsigned char>& vecbySendBuf,
const CHostAddress& HostAddr,
const int iTimeStampIdx )
2006-01-28 12:29:22 +01:00
{
const int iVecSizeOut = vecbySendBuf.Size ();
2006-01-28 12:29:22 +01:00
if ( iVecSizeOut != 0 )
{
/* send packet through network */
SocketDevice.writeBlock (
(const char*) &((CVector<unsigned char>) vecbySendBuf)[0],
iVecSizeOut, HostAddr.InetAddr, HostAddr.iPort );
2006-01-28 12:29:22 +01:00
}
/* sent time stamp if required */
if ( iTimeStampIdx != INVALID_TIME_STAMP_IDX )
2006-01-28 12:29:22 +01:00
{
/* Always one byte long */
SocketDevice.writeBlock ( (const char*) &iTimeStampIdx, 1,
HostAddr.InetAddr, HostAddr.iPort );
2006-01-28 12:29:22 +01:00
}
}
void CSocket::OnDataReceived ()
2006-01-28 12:29:22 +01:00
{
/* read block from network interface */
const int iNumBytesRead = SocketDevice.readBlock( (char*) &vecbyRecBuf[0],
2006-01-28 12:29:22 +01:00
MAX_SIZE_BYTES_NETW_BUF);
/* check if an error occurred */
if ( iNumBytesRead < 0 )
{
return;
}
2006-01-28 12:29:22 +01:00
/* get host address of client */
CHostAddress RecHostAddr ( SocketDevice.peerAddress (),
SocketDevice.peerPort () );
2006-01-28 12:29:22 +01:00
if ( bIsClient )
2006-01-28 12:29:22 +01:00
{
/* client */
/* check if packet comes from the server we want to connect */
if ( ! ( pChannel->GetAddress () == RecHostAddr ) )
2006-01-28 12:29:22 +01:00
return;
if ( pChannel->PutData( vecbyRecBuf, iNumBytesRead ) )
{
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_GREEN );
}
else
{
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_RED );
}
2006-01-28 12:29:22 +01:00
}
else
{
/* server */
pChannelSet->PutData ( vecbyRecBuf, iNumBytesRead, RecHostAddr );
2006-01-28 12:29:22 +01:00
}
}