From 43ab3cbd102b365439b271f23bfa45da3cc03ab8 Mon Sep 17 00:00:00 2001 From: jesopo Date: Wed, 29 Aug 2018 14:33:27 +0100 Subject: Factor ping interval and ping timeout in to the main loop's minimum interval --- IRCServer.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'IRCServer.py') diff --git a/IRCServer.py b/IRCServer.py index 46dedbb7..8d4e4b64 100644 --- a/IRCServer.py +++ b/IRCServer.py @@ -1,9 +1,12 @@ import collections, socket, ssl, sys, time import IRCChannel, IRCUser -OUR_TLS_PROTOCOL = ssl.PROTOCOL_SSLv23 THROTTLE_LINES = 4 THROTTLE_SECONDS = 1 +READ_TIMEOUT_SECONDS = 120 +PING_INTERVAL_SECONDS = 30 + +OUR_TLS_PROTOCOL = ssl.PROTOCOL_SSLv23 if hasattr(ssl, "PROTOCOL_TLS"): OUR_TLS_PROTOCOL = ssl.PROTOCOL_TLS @@ -38,7 +41,7 @@ class Server(object): self.channel_modes = [] self.channel_types = [] - self.last_read = None + self.last_read = time.monotonic() self.last_send = None self.attempted_join = {} @@ -204,6 +207,19 @@ class Server(object): self.last_read = time.monotonic() self.ping_sent = False return decoded_lines + + def until_next_ping(self): + return max(0, (self.last_read+PING_INTERVAL_SECONDS + )-time.monotonic()) + def ping_due(self): + return self.until_next_ping() == 0 + + def until_read_timeout(self): + return max(0, (self.last_read+READ_TIMEOUT_SECONDS + )-time.monotonic()) + def read_timed_out(self): + return self.until_read_timeout == 0 + def send(self, data): encoded = data.split("\n")[0].strip("\r").encode("utf8") if len(encoded) > 450: -- cgit v1.3.1-10-gc9f91