Skip to content

Commit a666f56

Browse files
committed
Implement keepalive over session long TCP connection
1 parent bfee34b commit a666f56

5 files changed

Lines changed: 46 additions & 4 deletions

File tree

src/client.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecM
255255
{
256256
if ( pTcpConnection )
257257
{
258-
qWarning() << "Client send cannot use TCP server";
258+
// already have TCP connection - just send and return
259+
pTcpConnection->write ( (const char*) &( (CVector<uint8_t>) vecMessage )[0], vecMessage.Size() );
259260
return;
260261
}
261262

@@ -284,6 +285,7 @@ void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecM
284285
CTcpConnection* pTcpConnection = new CTcpConnection ( pSocket,
285286
InetAddr,
286287
nullptr,
288+
this,
287289
&Channel,
288290
eProtoMode == PROTO_TCP_LONG ); // client connection, will self-delete on disconnect
289291

src/client.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,12 @@ protected slots:
469469
void OnConClientListMesReceived ( CVector<CChannelInfo> vecChanInfo );
470470
void OnCLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo, CTcpConnection* pTcpConnection );
471471

472+
public slots:
473+
void OnCLSendEmptyMes ( CHostAddress InetAddr, CTcpConnection* pTcpConnection )
474+
{
475+
ConnLessProtocol.CreateCLEmptyMes ( InetAddr, pTcpConnection );
476+
}
477+
472478
signals:
473479
void ConClientListMesReceived ( CVector<CChannelInfo> vecChanInfo );
474480
void ChatTextReceived ( QString strChatText );

src/tcpconnection.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@
2424

2525
#include "protocol.h"
2626
#include "server.h"
27+
#include "client.h"
2728
#include "channel.h"
2829

29-
CTcpConnection::CTcpConnection ( QTcpSocket* pTcpSocket, const CHostAddress& tcpAddress, CServer* pServer, CChannel* pChannel, bool bIsSession ) :
30+
CTcpConnection::CTcpConnection ( QTcpSocket* pTcpSocket,
31+
const CHostAddress& tcpAddress,
32+
CServer* pServer,
33+
CClient* pClient,
34+
CChannel* pChannel,
35+
bool bIsSession ) :
3036
pTcpSocket ( pTcpSocket ),
3137
tcpAddress ( tcpAddress ),
3238
pServer ( pServer ),
39+
pClient ( pClient ),
3340
pChannel ( pChannel ),
3441
bIsSession ( bIsSession )
3542
{
@@ -49,11 +56,20 @@ CTcpConnection::CTcpConnection ( QTcpSocket* pTcpSocket, const CHostAddress& tcp
4956
{
5057
connect ( this, &CTcpConnection::ProtocolCLMessageReceived, pChannel, &CChannel::OnProtocolCLMessageReceived );
5158
}
59+
60+
if ( pClient && bIsSession )
61+
{
62+
// set up keepalive CLM_EMPTY_MESSAGE over TCP session connection
63+
connect ( this, &CTcpConnection::CLSendEmptyMes, pClient, &CClient::OnCLSendEmptyMes );
64+
connect ( &TimerKeepalive, &QTimer::timeout, this, &CTcpConnection::OnTimerKeepalive );
65+
TimerKeepalive.start ( TCP_KEEPALIVE_INTERVAL_MS );
66+
}
5267
}
5368

5469
void CTcpConnection::OnDisconnected()
5570
{
5671
qDebug() << "- Jamulus-TCP: disconnected from:" << tcpAddress.toString();
72+
TimerKeepalive.stop();
5773
pTcpSocket->deleteLater();
5874
if ( pChannel && pChannel->GetTcpConnection() == this )
5975
{
@@ -160,6 +176,12 @@ void CTcpConnection::OnReadyRead()
160176
qDebug() << "- end of readyRead(), bytesAvailable() =" << pTcpSocket->bytesAvailable();
161177
}
162178

179+
void CTcpConnection::OnTimerKeepalive()
180+
{
181+
// qDebug() << "- Keepalive timer" << this << "to TCP" << tcpAddress.toString();
182+
emit CLSendEmptyMes ( tcpAddress, this );
183+
}
184+
163185
qint64 CTcpConnection::write ( const char* data, qint64 maxSize )
164186
{
165187
if ( !pTcpSocket )

src/tcpconnection.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,20 @@
4040
class CServer; // forward declaration of CServer
4141
class CChannel; // forward declaration of CChannel
4242

43+
#define TCP_KEEPALIVE_INTERVAL_MS 15000
44+
4345
/* Classes ********************************************************************/
4446
class CTcpConnection : public QObject
4547
{
4648
Q_OBJECT
4749

4850
public:
49-
CTcpConnection ( QTcpSocket* pTcpSocket, const CHostAddress& tcpAddress, CServer* pServer, CChannel* pChannel, bool bIsSession );
51+
CTcpConnection ( QTcpSocket* pTcpSocket,
52+
const CHostAddress& tcpAddress,
53+
CServer* pServer,
54+
CClient* pClient,
55+
CChannel* pChannel,
56+
bool bIsSession );
5057
~CTcpConnection() {}
5158

5259
void SetChannel ( CChannel* pChan ) { pChannel = pChan; }
@@ -63,6 +70,7 @@ class CTcpConnection : public QObject
6370
CHostAddress udpAddress;
6471

6572
CServer* pServer;
73+
CClient* pClient;
6674
CChannel* pChannel;
6775

6876
const bool bIsSession;
@@ -71,10 +79,14 @@ class CTcpConnection : public QObject
7179
int iPayloadRemain;
7280
CVector<uint8_t> vecbyRecBuf;
7381

82+
QTimer TimerKeepalive;
83+
7484
signals:
7585
void ProtocolCLMessageReceived ( int iRecID, CVector<uint8_t> vecbyMesBodyData, CHostAddress HostAdr, CTcpConnection* pTcpConnection );
86+
void CLSendEmptyMes ( CHostAddress InetAddr, CTcpConnection* pTcpConnection );
7687

7788
private slots:
7889
void OnDisconnected();
7990
void OnReadyRead();
91+
void OnTimerKeepalive();
8092
};

src/tcpserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,5 @@ void CTcpServer::OnNewConnection()
9797

9898
qDebug() << "- Jamulus-TCP: received connection from:" << peerAddress.toString();
9999

100-
new CTcpConnection ( pSocket, peerAddress, pServer, nullptr, false ); // will auto-delete on disconnect
100+
new CTcpConnection ( pSocket, peerAddress, pServer, nullptr, nullptr, false ); // will auto-delete on disconnect
101101
}

0 commit comments

Comments
 (0)