From ba11b8eecfee1d6e70df72d36e826f2696e66c0d Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Mon, 14 Jul 2014 15:33:36 +0200 Subject: [PATCH] Less magic numbers for timeout settings Currently the connection timeout handling of znc uses three magic numbers, each of which is at least repeated in two unrelated places. This commits defines the numbers in CIRCNetwork and makes the other places just use this number. This also renames PING_TIMEOUT to PING_FREQUENCY because I feel that describes this constant better. I am not really happy about the name NO_TRAFFIC_TIMEOUT that is used for the real timeout, but I couldn't think of a better name. PING_TIMEOUT isn't good because that sounds like the time between sending a PING and the resulting timeout. Signed-off-by: Uli Schlachter --- include/znc/IRCNetwork.h | 9 ++++++++- src/Client.cpp | 2 +- src/IRCNetwork.cpp | 6 +++--- src/IRCSock.cpp | 2 +- src/User.cpp | 4 ++-- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/include/znc/IRCNetwork.h b/include/znc/IRCNetwork.h index 289451ec..80b6c359 100644 --- a/include/znc/IRCNetwork.h +++ b/include/znc/IRCNetwork.h @@ -44,7 +44,14 @@ public: ~CIRCNetwork(); enum { - PING_TIMEOUT = 270 + /** How long must an IRC connection be idle before ZNC sends a ping */ + PING_FREQUENCY = 270, + /** Time between checks if PINGs need to be sent */ + PING_SLACK = 30, + /** Timeout after which IRC connections are closed. Must + * obviously be smaller than PING_FREQUENCY + PING_SLACK. + */ + NO_TRAFFIC_TIMEOUT = 540 }; void Clone(const CIRCNetwork& Network, bool bCloneName = true); diff --git a/src/Client.cpp b/src/Client.cpp index a6ea8b67..44cadf70 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -671,7 +671,7 @@ void CClient::AcceptLogin(CUser& User) { // Set our proper timeout and set back our proper timeout mode // (constructor set a different timeout and mode) - SetTimeout(540, TMO_READ); + SetTimeout(CIRCNetwork::NO_TRAFFIC_TIMEOUT, TMO_READ); SetSockName("USR::" + m_pUser->GetUserName()); SetEncoding(m_pUser->GetClientEncoding()); diff --git a/src/IRCNetwork.cpp b/src/IRCNetwork.cpp index 00c5787a..ec15fcd7 100644 --- a/src/IRCNetwork.cpp +++ b/src/IRCNetwork.cpp @@ -30,7 +30,7 @@ public: CIRCNetworkPingTimer(CIRCNetwork *pNetwork) : CCron() { m_pNetwork = pNetwork; SetName("CIRCNetworkPingTimer::" + m_pNetwork->GetUser()->GetUserName() + "::" + m_pNetwork->GetName()); - Start(30); + Start(CIRCNetwork::PING_SLACK); } virtual ~CIRCNetworkPingTimer() {} @@ -39,7 +39,7 @@ protected: virtual void RunJob() { CIRCSock* pIRCSock = m_pNetwork->GetIRCSock(); - if (pIRCSock && pIRCSock->GetTimeSinceLastDataTransaction() >= CIRCNetwork::PING_TIMEOUT) { + if (pIRCSock && pIRCSock->GetTimeSinceLastDataTransaction() >= CIRCNetwork::PING_FREQUENCY) { pIRCSock->PutIRC("PING :ZNC"); } @@ -47,7 +47,7 @@ protected: for (size_t b = 0; b < vClients.size(); b++) { CClient* pClient = vClients[b]; - if (pClient->GetTimeSinceLastDataTransaction() >= CIRCNetwork::PING_TIMEOUT) { + if (pClient->GetTimeSinceLastDataTransaction() >= CIRCNetwork::PING_FREQUENCY) { pClient->PutClient("PING :ZNC"); } } diff --git a/src/IRCSock.cpp b/src/IRCSock.cpp index 1e0c7931..d7f0a42a 100644 --- a/src/IRCSock.cpp +++ b/src/IRCSock.cpp @@ -174,7 +174,7 @@ void CIRCSock::ReadLine(const CString& sData) { } m_pNetwork->SetIRCServer(sServer); - SetTimeout(540, TMO_READ); // Now that we are connected, let nature take its course + SetTimeout(CIRCNetwork::NO_TRAFFIC_TIMEOUT, TMO_READ); // Now that we are connected, let nature take its course PutIRC("WHO " + sNick); m_bAuthed = true; diff --git a/src/User.cpp b/src/User.cpp index ee672b09..930d211e 100644 --- a/src/User.cpp +++ b/src/User.cpp @@ -29,7 +29,7 @@ public: CUserTimer(CUser* pUser) : CCron() { m_pUser = pUser; SetName("CUserTimer::" + m_pUser->GetUserName()); - Start(30); + Start(CIRCNetwork::PING_SLACK); } virtual ~CUserTimer() {} @@ -40,7 +40,7 @@ protected: for (size_t c = 0; c < vUserClients.size(); ++c) { CClient* pUserClient = vUserClients[c]; - if (pUserClient->GetTimeSinceLastDataTransaction() >= CIRCNetwork::PING_TIMEOUT) { + if (pUserClient->GetTimeSinceLastDataTransaction() >= CIRCNetwork::PING_FREQUENCY) { pUserClient->PutClient("PING :ZNC"); } }