From a770b75a0642223d5e3d6fa1328bad933bfbcc15 Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Sun, 22 May 2011 09:47:09 +0000 Subject: [PATCH] added a connection less message for disconnect -> does not work yet since connection less messages are not evaluated for connected channels --- src/channel.cpp | 2 +- src/channel.h | 3 ++- src/client.cpp | 6 +++--- src/protocol.cpp | 31 ++++++++++++++++++++++++++++--- src/protocol.h | 14 ++++++++++---- src/server.cpp | 31 +++++++++++++++++++++++++++++++ src/server.h | 3 +++ src/socket.cpp | 18 +++++++++++++----- src/testbench.h | 6 +++--- 9 files changed, 94 insertions(+), 20 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index a6510d0e..c722a03f 100755 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -331,7 +331,7 @@ void CChannel::CreateNetTranspPropsMessFromCurrentSettings() Protocol.CreateNetwTranspPropsMes ( NetworkTransportProps ); } -void CChannel::OnDisconnection() +void CChannel::Disconnect() { // set time out counter to a small value > 0 so that the next time a // received audio block is queried, the disconnection is performed diff --git a/src/channel.h b/src/channel.h index cf8c14ff..39baef97 100755 --- a/src/channel.h +++ b/src/channel.h @@ -70,6 +70,7 @@ public: void ResetTimeOutCounter() { iConTimeOut = iConTimeOutStartVal; } bool IsConnected() const { return iConTimeOut > 0; } + void Disconnect(); void SetEnable ( const bool bNEnStat ); bool IsEnabled() { return bIsEnabled; } @@ -173,7 +174,7 @@ public slots: void OnChangeChanName ( QString strName ); void OnNetTranspPropsReceived ( CNetworkTransportProps NetworkTransportProps ); void OnReqNetTranspProps(); - void OnDisconnection(); + void OnDisconnection() { Disconnect(); } signals: void MessReadyForSending ( CVector vecMessage ); diff --git a/src/client.cpp b/src/client.cpp index b53d5233..134b2086 100755 --- a/src/client.cpp +++ b/src/client.cpp @@ -425,10 +425,10 @@ void CClient::Stop() // network queue causing the channel to be reconnected right after having // received the disconnect message (seems not to gain much, disconnect is // still not working reliably) - QTime dieTime = QTime::currentTime().addMSecs ( 100 ); - while ( QTime::currentTime() < dieTime ) + QTime DieTime = QTime::currentTime().addMSecs ( 100 ); + while ( QTime::currentTime() < DieTime ) { - QCoreApplication::processEvents ( QEventLoop::AllEvents, 100 ); + QCoreApplication::processEvents ( QEventLoop::AllEvents, 100 ); } // Send disconnect message to server (Since we disable our protocol diff --git a/src/protocol.cpp b/src/protocol.cpp index 80c0587f..c5a619d5 100755 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -33,8 +33,8 @@ MAIN FRAME -MESSAGES --------- +MESSAGES (with connection) +-------------------------- - PROTMESSID_ACKN: Acknowledgement message @@ -202,7 +202,6 @@ CONNECTION LESS MESSAGES the QLocale class - - PROTMESSID_CLM_UNREGISTER_SERVER: Unregister a server note: does not have any data -> n = 0 @@ -232,6 +231,11 @@ CONNECTION LESS MESSAGES +--------------------+--------------+ +- PROTMESSID_CLM_DISCONNECTION: Disconnect message + + note: does not have any data -> n = 0 + + ****************************************************************************** * * This program is free software; you can redistribute it and/or modify it under @@ -616,6 +620,10 @@ if ( rand() < ( RAND_MAX / 2 ) ) return false; case PROTMESSID_CLM_UNREGISTER_SERVER: bRet = EvaluateCLUnregisterServerMes ( InetAddr ); break; + + case PROTMESSID_CLM_DISCONNECTION: + bRet = EvaluateCLDisconnectionMes ( InetAddr ); + break; } } else @@ -1568,11 +1576,28 @@ bool CProtocol::EvaluateCLSendEmptyMesMes ( const CVector& vecData ) void CProtocol::CreateCLEmptyMes ( const CHostAddress& InetAddr ) { + // special message: for this message there exist no Evaluate + // function CreateAndImmSendConLessMessage ( PROTMESSID_CLM_EMPTY_MESSAGE, CVector ( 0 ), InetAddr ); } +void CProtocol::CreateCLDisconnection ( const CHostAddress& InetAddr ) +{ + CreateAndImmSendConLessMessage ( PROTMESSID_CLM_DISCONNECTION, + CVector ( 0 ), + InetAddr ); +} + +bool CProtocol::EvaluateCLDisconnectionMes ( const CHostAddress& InetAddr ) +{ + // invoke message action + emit CLDisconnection ( InetAddr ); + + return false; // no error +} + /******************************************************************************\ * Message generation and parsing * diff --git a/src/protocol.h b/src/protocol.h index cd526a52..3921a3cb 100755 --- a/src/protocol.h +++ b/src/protocol.h @@ -64,6 +64,7 @@ #define PROTMESSID_CLM_REQ_SERVER_LIST 1007 // request server list #define PROTMESSID_CLM_SEND_EMPTY_MESSAGE 1008 // an empty message shall be send #define PROTMESSID_CLM_EMPTY_MESSAGE 1009 // empty message +#define PROTMESSID_CLM_DISCONNECTION 1010 // disconnection // lengths of message as defined in protocol.cpp file @@ -110,10 +111,7 @@ public: void CreateCLSendEmptyMesMes ( const CHostAddress& InetAddr, const CHostAddress& TargetInetAddr ); void CreateCLEmptyMes ( const CHostAddress& InetAddr ); - - void CreateAndImmSendDisconnectionMes(); - void CreateAndImmSendAcknMess ( const int& iID, - const int& iCnt ); + void CreateCLDisconnection ( const CHostAddress& InetAddr ); bool ParseMessage ( const CVector& vecbyData, const int iNumBytes ); @@ -125,6 +123,12 @@ public: bool IsProtocolMessage ( const CVector& vecbyData, const int iNumBytes ); + void CreateAndImmSendDisconnectionMes(); + + // this function is public because we need it in the test bench + void CreateAndImmSendAcknMess ( const int& iID, + const int& iCnt ); + protected: class CSendMessage { @@ -219,6 +223,7 @@ protected: const CVector& vecData ); bool EvaluateCLReqServerListMes ( const CHostAddress& InetAddr ); bool EvaluateCLSendEmptyMesMes ( const CVector& vecData ); + bool EvaluateCLDisconnectionMes ( const CHostAddress& InetAddr ); int iOldRecID; int iOldRecCnt; @@ -266,6 +271,7 @@ signals: CVector vecServerInfo ); void CLReqServerList ( CHostAddress InetAddr ); void CLSendEmptyMes ( CHostAddress TargetInetAddr ); + void CLDisconnection ( CHostAddress InetAddr ); }; #endif /* !defined ( PROTOCOL_H__3B123453_4344_BB2392354455IUHF1912__INCLUDED_ ) */ diff --git a/src/server.cpp b/src/server.cpp index 7a3a5155..4dfc1a21 100755 --- a/src/server.cpp +++ b/src/server.cpp @@ -304,6 +304,10 @@ CServer::CServer ( const int iNewNumChan, SIGNAL ( CLSendEmptyMes ( CHostAddress ) ), this, SLOT ( OnCLSendEmptyMes ( CHostAddress ) ) ); + QObject::connect ( &ConnLessProtocol, + SIGNAL ( CLDisconnection ( CHostAddress ) ), + this, SLOT ( OnCLDisconnection ( CHostAddress ) ) ); + // CODE TAG: MAX_NUM_CHANNELS_TAG // make sure we have MAX_NUM_CHANNELS connections!!! @@ -407,6 +411,18 @@ void CServer::OnSendCLProtMessage ( CHostAddress InetAddr, Socket.SendPacket ( vecMessage, InetAddr ); } +void CServer::OnCLDisconnection ( CHostAddress InetAddr ) +{ + // check if the given address is actually a client which is connected to + // this server, if yes, disconnect it + const int iCurChanID = FindChannel ( InetAddr ); + + if ( iCurChanID != INVALID_CHANNEL_ID ) + { + vecChannels[iCurChanID].Disconnect(); + } +} + void CServer::Start() { // only start if not already running @@ -880,6 +896,21 @@ int CServer::GetFreeChan() return INVALID_CHANNEL_ID; } +int CServer::FindChannel ( const CHostAddress& InetAddr ) +{ + // look for a channel with the given internet address + for ( int i = 0; i < iNumChannels; i++ ) + { + if ( vecChannels[i].GetAddress() == InetAddr ) + { + return i; + } + } + + // no free channel found, return invalid ID + return INVALID_CHANNEL_ID; +} + int CServer::GetNumberOfConnectedClients() { int iNumConnClients = 0; diff --git a/src/server.h b/src/server.h index 1e318b97..fab4012a 100755 --- a/src/server.h +++ b/src/server.h @@ -183,6 +183,7 @@ protected: int CheckAddr ( const CHostAddress& Addr ); int GetFreeChan(); + int FindChannel ( const CHostAddress& InetAddr ); int GetNumberOfConnectedClients(); CVector CreateChannelList(); void CreateAndSendChanListForAllConChannels(); @@ -276,6 +277,8 @@ public slots: ServerListManager.CentralServerUnregisterServer ( InetAddr ); } + void OnCLDisconnection ( CHostAddress InetAddr ); + // CODE TAG: MAX_NUM_CHANNELS_TAG // make sure we have MAX_NUM_CHANNELS connections!!! diff --git a/src/socket.cpp b/src/socket.cpp index 0099c16f..ec3c073f 100755 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -137,11 +137,19 @@ void CSocket::OnDataReceived() // are not connected anymore if ( pChannel->GetAddress() == RecHostAddr ) { - // the channel has to be enabled for sending a message, - // disable the channel again afterwards - pChannel->SetEnable ( true ); - pChannel->CreateAndImmSendDisconnectionMes(); - pChannel->SetEnable ( false ); + + +// TEST old code -> to be removed because this is not working!!! +pChannel->SetEnable ( true ); +pChannel->CreateAndImmSendDisconnectionMes(); +pChannel->SetEnable ( false ); + + +// TODO this does not work because for a connected channel at the server, no +// connection less protocol messages are accepted + +// pConnLessProtocol->CreateCLDisconnection ( RecHostAddr ); + } } diff --git a/src/testbench.h b/src/testbench.h index d8b03193..9e612dc4 100755 --- a/src/testbench.h +++ b/src/testbench.h @@ -230,12 +230,12 @@ public slots: break; case 20: - Protocol.CreateAndImmSendAcknMess ( GenRandomIntInRange ( -10, 100 ), - GenRandomIntInRange ( -100, 100 ) ); + Protocol.CreateCLDisconnection ( CurHostAddress ); break; case 21: - Protocol.CreateAndImmSendDisconnectionMes(); + Protocol.CreateAndImmSendAcknMess ( GenRandomIntInRange ( -10, 100 ), + GenRandomIntInRange ( -100, 100 ) ); break; case 22: