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
5469void 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+
163185qint64 CTcpConnection::write ( const char * data, qint64 maxSize )
164186{
165187 if ( !pTcpSocket )
0 commit comments