2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "pingtimer.h"
23 #include "treeserver.h"
24 #include "commandbuilder.h"
26 PingTimer::PingTimer(TreeServer* ts)
27 : Timer(Utils->PingFreq)
33 PingTimer::State PingTimer::TickInternal()
35 // Timer expired, take next action based on what happened last time
36 if (state == PS_SENDPING)
38 // Last ping was answered, send next ping
39 server->GetSocket()->WriteLine(CmdBuilder("PING").push(server->GetID()));
40 LastPingMsec = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
41 // Warn next unless warnings are disabled. If they are, jump straight to timeout.
42 if (Utils->PingWarnTime)
47 else if (state == PS_WARN)
49 // No pong arrived in PingWarnTime seconds, send a warning to opers
50 ServerInstance->SNO->WriteToSnoMask('l', "Server \002%s\002 has not responded to PING for %d seconds, high latency.", server->GetName().c_str(), GetInterval());
55 // They didn't answer the last ping, if they are locally connected, get rid of them
56 if (server->IsLocal())
58 TreeSocket* sock = server->GetSocket();
59 sock->SendError("Ping timeout");
63 // If the server is non-locally connected, don't do anything until we get a PONG.
64 // This is to avoid pinging the server and warning opers more than once.
65 // If they do answer eventually, we will move to the PS_SENDPING state and ping them again.
70 void PingTimer::SetState(State newstate)
74 // Set when should the next Tick() happen based on the state
75 if (state == PS_SENDPING)
76 SetInterval(Utils->PingFreq);
77 else if (state == PS_WARN)
78 SetInterval(Utils->PingWarnTime);
79 else if (state == PS_TIMEOUT)
80 SetInterval(Utils->PingFreq - Utils->PingWarnTime);
82 // If state == PS_IDLE, do not set the timer, see above why
85 bool PingTimer::Tick(time_t currtime)
90 SetState(TickInternal());
94 void PingTimer::OnPong()
97 long ts = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
98 server->rtt = ts - LastPingMsec;
100 // Change state to send ping next, also reschedules the timer appropriately
101 SetState(PS_SENDPING);