Skip to content

Commit 2c09cb1

Browse files
committed
Add an idle timeout on the server side
1 parent 738bb11 commit 2c09cb1

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/tcpconnection.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,18 @@ CTcpConnection::CTcpConnection ( QTcpSocket* pTcpSocket, const CHostAddress& tcp
7373
connect ( pTcpSocket, &QTcpSocket::readyRead, this, &CTcpConnection::OnReadyRead );
7474

7575
connect ( this, &CTcpConnection::ProtocolCLMessageReceived, pServer, &CServer::OnProtocolCLMessageReceived );
76+
77+
// setup an idle timer on the server side only
78+
connect ( &TimerIdleTimeout, &QTimer::timeout, this, &CTcpConnection::OnTimerIdleTimeout );
79+
TimerIdleTimeout.setSingleShot ( true );
80+
TimerIdleTimeout.start ( TCP_IDLE_TIMEOUT_MS );
7681
}
7782

7883
void CTcpConnection::OnDisconnected()
7984
{
8085
qDebug() << "- Jamulus-TCP: disconnected from:" << tcpAddress.toString();
8186
TimerKeepalive.stop();
87+
TimerIdleTimeout.stop();
8288
pTcpSocket->deleteLater();
8389
if ( pChannel && pChannel->GetTcpConnection() == this )
8490
{
@@ -183,6 +189,12 @@ void CTcpConnection::OnReadyRead()
183189
}
184190

185191
qDebug() << "- end of readyRead(), bytesAvailable() =" << pTcpSocket->bytesAvailable();
192+
193+
if ( pServer )
194+
{
195+
// restart server idle timer allowing for keepalive interval
196+
TimerIdleTimeout.start ( TCP_KEEPALIVE_INTERVAL_MS + TCP_IDLE_TIMEOUT_MS );
197+
}
186198
}
187199

188200
void CTcpConnection::OnTimerKeepalive()
@@ -191,6 +203,12 @@ void CTcpConnection::OnTimerKeepalive()
191203
emit CLSendEmptyMes ( tcpAddress, this );
192204
}
193205

206+
void CTcpConnection::OnTimerIdleTimeout()
207+
{
208+
// qDebug() << "- ConnTimeout timer" << this << "from TCP" << tcpAddress.toString();
209+
disconnectFromHost();
210+
}
211+
194212
qint64 CTcpConnection::write ( const char* data, qint64 maxSize )
195213
{
196214
if ( !pTcpSocket )

src/tcpconnection.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CServer; // forward declaration of CServer
4141
class CChannel; // forward declaration of CChannel
4242

4343
#define TCP_CONNECT_TIMEOUT_MS 3000
44+
#define TCP_IDLE_TIMEOUT_MS 5000
4445
#define TCP_KEEPALIVE_INTERVAL_MS 15000
4546

4647
/* Classes ********************************************************************/
@@ -77,6 +78,7 @@ class CTcpConnection : public QObject
7778
CVector<uint8_t> vecbyRecBuf;
7879

7980
QTimer TimerKeepalive;
81+
QTimer TimerIdleTimeout;
8082

8183
signals:
8284
void ProtocolCLMessageReceived ( int iRecID, CVector<uint8_t> vecbyMesBodyData, CHostAddress HostAdr, CTcpConnection* pTcpConnection );
@@ -86,4 +88,5 @@ private slots:
8688
void OnDisconnected();
8789
void OnReadyRead();
8890
void OnTimerKeepalive();
91+
void OnTimerIdleTimeout();
8992
};

0 commit comments

Comments
 (0)