From efb83f15acc12e66d2d59a79d38a5ec5061283d2 Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Sat, 4 Nov 2006 13:09:42 +0000 Subject: [PATCH] server sends connected client list on a new connection --- src/channel.cpp | 51 ++++++++++++++++++++++++++++++++++-- src/channel.h | 14 +++++++--- src/protocol.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++--- src/protocol.h | 1 + src/server.cpp | 5 +++- 5 files changed, 129 insertions(+), 10 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index 5a58ae18..b542fde6 100755 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -55,6 +55,38 @@ CChannelSet::CChannelSet() // QObject::connect(&vecChannels[8],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh8())); // QObject::connect(&vecChannels[9],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh9())); } + +void CChannelSet::CreateAndSendChanListForAllConClients() +{ + int i; + CVector veciIpAddrs ( 0 ); + CVector vecstrNames ( 0 ); + + // the channel ID is defined as the order of the channels in the channel + // set where we do not care about not-connected channels + int iCurChanID = 0; + + // look for free channels + for ( i = 0; i < MAX_NUM_CHANNELS; i++ ) + { + if ( vecChannels[i].IsConnected() ) + { + // append IP address and channel name to storing vectors + veciIpAddrs.Add ( vecChannels[i].GetAddress().InetAddr.ip4Addr() ); + vecstrNames.Add ( vecChannels[i].GetName() ); + } + } + + // now send connected channels list to all connected clients + for ( i = 0; i < MAX_NUM_CHANNELS; i++ ) + { + if ( vecChannels[i].IsConnected() ) + { + // send message + vecChannels[i].CreateConClientListMes ( veciIpAddrs, vecstrNames ); + } + } +} int CChannelSet::GetFreeChan() { @@ -97,6 +129,7 @@ bool CChannelSet::PutData ( const CVector& vecbyRecBuf, const CHostAddress& HostAdr ) { bool bRet = false; + bool bCreateChanList = false; Mutex.lock(); { @@ -114,6 +147,12 @@ bool CChannelSet::PutData ( const CVector& vecbyRecBuf, if ( iCurChanID != INVALID_CHANNEL_ID ) { vecChannels[iCurChanID].SetAddress ( HostAdr ); + + // a new client connected to the server, set flag to create and + // send all clients the updated channel list, we cannot create + // the message here since the received data has to be put to the + // channel first so that this channel is marked as connected + bCreateChanList = true; } else { @@ -143,8 +182,16 @@ bool CChannelSet::PutData ( const CVector& vecbyRecBuf, PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_YELLOW, iCurChanID ); break; } - } - } + } + + + // after data is put in channel buffer, create channel list message if + // requested + if ( bCreateChanList ) + { + CreateAndSendChanListForAllConClients(); + } + } Mutex.unlock(); return bRet; diff --git a/src/channel.h b/src/channel.h index 80f2c694..bef76025 100755 --- a/src/channel.h +++ b/src/channel.h @@ -112,6 +112,11 @@ public: } } + void CreateConClientListMes ( const CVector& veciIpAddrs, + const CVector& vecstrNames ) + { + Protocol.CreateConClientListMes ( veciIpAddrs, vecstrNames ); + } protected: void SetNetwInBlSiFact ( const int iNewBlockSizeFactor ); @@ -134,8 +139,9 @@ protected: // channel name std::string sName; - // mixer and effect settings - double dGain; +// TODO we need mixer and effect settings for all the other channels, too!!! +// mixer and effect settings +double dGain; // network jitter-buffer CNetBuf SockBuf; @@ -205,7 +211,9 @@ public: CHostAddress GetAddress ( const int iChanNum ) { return vecChannels[iChanNum].GetAddress(); } -protected: +protected: + void CreateAndSendChanListForAllConClients(); + /* do not use the vector class since CChannel does not have appropriate copy constructor/operator */ CChannel vecChannels[MAX_NUM_CHANNELS]; diff --git a/src/protocol.cpp b/src/protocol.cpp index 046a7f4f..dbfc281b 100755 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -229,17 +229,18 @@ bool CProtocol::ParseMessage ( const CVector& vecbyData, /* return code: true -> ok; false -> error */ - bool bRet; - int iRecCounter, iRecID, iData; + bool bRet, bSendNextMess; + int iRecCounter, iRecID, iData, iDataLen, iStringLen, iCurID; unsigned int iPos; CVector vecData; - bool bSendNextMess; + std::string strCurString; + double dNewGain; // convert unsigned char in uint8_t, TODO convert all buffers in uint8_t CVector vecbyDataConv ( iNumBytes ); for ( int i = 0; i < iNumBytes; i++ ) { -vecbyDataConv[i] = static_cast ( vecbyData[i] ); + vecbyDataConv[i] = static_cast ( vecbyData[i] ); } @@ -332,6 +333,65 @@ vecbyDataConv[i] = static_cast ( vecbyData[i] ); // send acknowledge message CreateAndSendAcknMess ( iRecID, iRecCounter ); + break; + + case PROTMESSID_CHANNEL_GAIN: + + // extract data from stream and emit signal for received value + iPos = 0; + + // channel ID + iCurID = static_cast ( GetValFromStream ( vecData, iPos, 2 ) ); + + // actual gain, we convert from integer to double with range 0..1 + iData = static_cast ( GetValFromStream ( vecData, iPos, 2 ) ); + dNewGain = (double) iData / ( 1 << 16 ); + + // invoke message action + emit ChangeChanGain ( iCurID, dNewGain ); + + // send acknowledge message + CreateAndSendAcknMess ( iRecID, iRecCounter ); + + break; + + case PROTMESSID_CONN_CLIENTS_LIST: + + // extract data from stream and emit signal for received value + iPos = 0; + iDataLen = vecData.Size(); + + while ( iPos < iDataLen ) + { + + // IP address (4 bytes) + iData = static_cast ( GetValFromStream ( vecData, iPos, 4 ) ); + +// TODO do something with the received IP address "iData" + +// TEST +QHostAddress addrTest ( iData ); +printf ( "%s ", addrTest.toString().latin1() ); + + // number of bytes for name string (2 bytes) + iStringLen = static_cast ( GetValFromStream ( vecData, iPos, 2 ) ); + + // name string (n bytes) + strCurString = ""; + for ( int j = 0; j < iStringLen; j++ ) + { + // byte-by-byte copying of the string data + iData = static_cast ( GetValFromStream ( vecData, iPos, 1 ) ); + strCurString += std::string ( (char*) &iData ); + } + +// TODO do something with the received name string "strCurString" + + } + + // send acknowledge message + CreateAndSendAcknMess ( iRecID, iRecCounter ); + break; } } diff --git a/src/protocol.h b/src/protocol.h index c1dc07b1..02c3bf4f 100755 --- a/src/protocol.h +++ b/src/protocol.h @@ -146,6 +146,7 @@ signals: // receiving void ChangeJittBufSize ( int iNewJitBufSize ); void ChangeNetwBlSiFact ( int iNewNetwBlSiFact ); + void ChangeChanGain ( int iChanID, double dNewGain ); void ReqJittBufSize(); }; diff --git a/src/server.cpp b/src/server.cpp index be1b264a..d24fbe68 100755 --- a/src/server.cpp +++ b/src/server.cpp @@ -96,7 +96,10 @@ void CServer::OnTimer() /* Check if at least one client is connected. If not, stop server until one client is connected */ if ( iNumClients != 0 ) - { + { + +// TODO generate a sparate mix for each channel + /* actual processing of audio data -> mix */ vecsSendData = ProcessData ( vecvecdData, vecdGains );