Skip to content

Commit 08abf30

Browse files
committed
Rework TCP session-mode connection
1 parent b71df91 commit 08abf30

6 files changed

Lines changed: 36 additions & 20 deletions

File tree

src/client.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,12 @@ void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecM
285285
InetAddr,
286286
nullptr,
287287
&Channel,
288-
eProtoMode == PROTO_TCP_ONCE ); // client connection, will self-delete on disconnect
288+
eProtoMode == PROTO_TCP_LONG ); // client connection, will self-delete on disconnect
289+
290+
if ( eProtoMode == PROTO_TCP_LONG )
291+
{
292+
Channel.SetTcpConnection ( pTcpConnection ); // link session connection with channel
293+
}
289294

290295
pTcpConnection->write ( (const char*) &( (CVector<uint8_t>) vecMessage )[0], vecMessage.Size() );
291296

@@ -1065,16 +1070,17 @@ void CClient::OnCLTcpSupported ( CHostAddress InetAddr, int iID )
10651070
}
10661071
}
10671072

1068-
void CClient::OnCLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo )
1073+
void CClient::OnCLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo, CTcpConnection* pTcpConnection )
10691074
{
10701075
// test if we are receiving for the connect dialog or a connected session
1071-
qDebug() << Q_FUNC_INFO << "Channel.IsConnected() =" << Channel.IsConnected();
1072-
if ( Channel.IsConnected() )
1076+
if ( pTcpConnection && pTcpConnection->IsSession() )
10731077
{
1078+
qDebug() << "- sending client list to client dialog";
10741079
OnConClientListMesReceived ( vecChanInfo ); // connected session
10751080
}
10761081
else
10771082
{
1083+
qDebug() << "- sending client list to connect dialog";
10781084
emit CLConnClientsListMesReceived ( InetAddr, vecChanInfo ); // connect dialog
10791085
}
10801086
}
@@ -1108,6 +1114,14 @@ void CClient::Stop()
11081114
// stop audio interface
11091115
Sound.Stop();
11101116

1117+
// close any session TCP connection
1118+
CTcpConnection* pTcpConnection = Channel.GetTcpConnection();
1119+
if ( pTcpConnection )
1120+
{
1121+
Channel.SetTcpConnection ( nullptr );
1122+
pTcpConnection->disconnectFromHost();
1123+
}
1124+
11111125
// disable channel
11121126
Channel.SetEnable ( false );
11131127

src/client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ protected slots:
467467
void OnMuteStateHasChangedReceived ( int iServerChanID, bool bIsMuted );
468468
void OnCLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );
469469
void OnConClientListMesReceived ( CVector<CChannelInfo> vecChanInfo );
470-
void OnCLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo );
470+
void OnCLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo, CTcpConnection* pTcpConnection );
471471

472472
signals:
473473
void ConClientListMesReceived ( CVector<CChannelInfo> vecChanInfo );

src/protocol.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ void CProtocol::ParseConnectionLessMessageBody ( const CVector<uint8_t>& vecbyMe
936936
break;
937937

938938
case PROTMESSID_CLM_CONN_CLIENTS_LIST:
939-
EvaluateCLConnClientsListMes ( InetAddr, vecbyMesBodyData );
939+
EvaluateCLConnClientsListMes ( InetAddr, vecbyMesBodyData, pTcpConnection );
940940
break;
941941

942942
case PROTMESSID_CLM_REQ_CONN_CLIENTS_LIST:
@@ -2456,7 +2456,7 @@ void CProtocol::CreateCLConnClientsListMes ( const CHostAddress& InetAddr, const
24562456
CreateAndImmSendConLessMessage ( PROTMESSID_CLM_CONN_CLIENTS_LIST, vecData, InetAddr, pTcpConnection );
24572457
}
24582458

2459-
bool CProtocol::EvaluateCLConnClientsListMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData )
2459+
bool CProtocol::EvaluateCLConnClientsListMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData, CTcpConnection* pTcpConnection )
24602460
{
24612461
int iPos = 0; // init position pointer
24622462
const int iDataLen = vecData.Size();
@@ -2510,7 +2510,7 @@ bool CProtocol::EvaluateCLConnClientsListMes ( const CHostAddress& InetAddr, con
25102510
}
25112511

25122512
// invoke message action
2513-
emit CLConnClientsListMesReceived ( InetAddr, vecChanInfo );
2513+
emit CLConnClientsListMesReceived ( InetAddr, vecChanInfo, pTcpConnection );
25142514

25152515
return false; // no error
25162516
}

src/protocol.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class CProtocol : public QObject
297297
bool EvaluateCLDisconnectionMes ( const CHostAddress& InetAddr );
298298
bool EvaluateCLVersionAndOSMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
299299
bool EvaluateCLReqVersionAndOSMes ( const CHostAddress& InetAddr );
300-
bool EvaluateCLConnClientsListMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
300+
bool EvaluateCLConnClientsListMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData, CTcpConnection* pTcpConnection );
301301
bool EvaluateCLReqConnClientsListMes ( const CHostAddress& InetAddr, CTcpConnection* pTcpConnection );
302302
bool EvaluateCLChannelLevelListMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
303303
bool EvaluateCLRegisterServerResp ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
@@ -365,7 +365,7 @@ public slots:
365365
void CLDisconnection ( CHostAddress InetAddr );
366366
void CLVersionAndOSReceived ( CHostAddress InetAddr, COSUtil::EOpSystemType eOSType, QString strVersion );
367367
void CLReqVersionAndOS ( CHostAddress InetAddr );
368-
void CLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo );
368+
void CLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo, CTcpConnection* pTcpConnection );
369369
void CLReqConnClientsList ( CHostAddress InetAddr, CTcpConnection* pTcpConnection );
370370
void CLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );
371371
void CLRegisterServerResp ( CHostAddress InetAddr, ESvrRegResult eStatus );

src/tcpconnection.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@
2626
#include "server.h"
2727
#include "channel.h"
2828

29-
CTcpConnection::CTcpConnection ( QTcpSocket* pTcpSocket,
30-
const CHostAddress& tcpAddress,
31-
CServer* pServer,
32-
CChannel* pChannel,
33-
bool bDisconAfterRecv ) :
29+
CTcpConnection::CTcpConnection ( QTcpSocket* pTcpSocket, const CHostAddress& tcpAddress, CServer* pServer, CChannel* pChannel, bool bIsSession ) :
3430
pTcpSocket ( pTcpSocket ),
3531
tcpAddress ( tcpAddress ),
3632
pServer ( pServer ),
3733
pChannel ( pChannel ),
38-
bDisconAfterRecv ( bDisconAfterRecv )
34+
bIsSession ( bIsSession )
3935
{
4036
vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF );
4137
iPos = 0;
@@ -59,6 +55,10 @@ void CTcpConnection::OnDisconnected()
5955
{
6056
qDebug() << "- Jamulus-TCP: disconnected from:" << tcpAddress.toString();
6157
pTcpSocket->deleteLater();
58+
if ( pChannel && pChannel->GetTcpConnection() == this )
59+
{
60+
pChannel->SetTcpConnection ( nullptr ); // unlink from channel
61+
}
6262
deleteLater(); // delete this object in the next event loop
6363
}
6464

@@ -133,8 +133,8 @@ void CTcpConnection::OnReadyRead()
133133
emit ProtocolCLMessageReceived ( iRecID, vecbyMesBodyData, tcpAddress, this );
134134
//### TODO: END ###//
135135

136-
// disconnect if it's not a persistent connection
137-
if ( bDisconAfterRecv )
136+
// disconnect if it's not a client session connection
137+
if ( !pServer && !bIsSession )
138138
{
139139
pTcpSocket->disconnectFromHost();
140140
}

src/tcpconnection.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CTcpConnection : public QObject
4646
Q_OBJECT
4747

4848
public:
49-
CTcpConnection ( QTcpSocket* pTcpSocket, const CHostAddress& tcpAddress, CServer* pServer, CChannel* pChannel, bool bDisconAfterRecv );
49+
CTcpConnection ( QTcpSocket* pTcpSocket, const CHostAddress& tcpAddress, CServer* pServer, CChannel* pChannel, bool bIsSession );
5050
~CTcpConnection() {}
5151

5252
void SetChannel ( CChannel* pChan ) { pChannel = pChan; }
@@ -55,6 +55,8 @@ class CTcpConnection : public QObject
5555
qint64 write ( const char* data, qint64 maxSize );
5656
void disconnectFromHost();
5757

58+
bool IsSession() { return bIsSession; }
59+
5860
private:
5961
QTcpSocket* pTcpSocket;
6062
CHostAddress tcpAddress;
@@ -63,7 +65,7 @@ class CTcpConnection : public QObject
6365
CServer* pServer;
6466
CChannel* pChannel;
6567

66-
const bool bDisconAfterRecv;
68+
const bool bIsSession;
6769

6870
int iPos;
6971
int iPayloadRemain;

0 commit comments

Comments
 (0)