From 0728a742d76e1cd8a48a37169736f5fa4ca0f371 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Wed, 3 Dec 2003 06:01:48 +0000 Subject: Add ping timeouts Ping client if it has been quiet for a while and disconnect it if it doesn't pong. --- miniircd | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/miniircd b/miniircd index 6a1dbf0..e941018 100755 --- a/miniircd +++ b/miniircd @@ -23,11 +23,12 @@ version = "0.0.0" import getopt import select +import sets import socket import string import sys import re -import sets +import time class Channel(object): def __init__(self, server, name): @@ -74,6 +75,8 @@ class Client(object): self.__socket = socket self.__readbuffer = "" self.__writebuffer = "" + self.__timestamp = time.time() + self.__sentPing = False self.__nickname = None self.__user = None self.__host, self.__port = socket.getpeername() @@ -88,6 +91,10 @@ class Client(object): return self.__socket socket = property(getSocket) + def getTimestamp(self): + return self.__timestamp + timestamp = property(getTimestamp) + def getChannels(self): return self.__channels channels = property(getChannels) @@ -115,6 +122,14 @@ class Client(object): return self.__realname realname = property(getRealname) + def checkAliveness(self): + now = time.time() + if self.timestamp + 180 < now: + self.disconnect("ping timeout") + if not self.__sentPing and self.timestamp + 90 < now: + self.message("PING :%s" % self.__server.name) + self.__sentPing = True + def writeQueueSize(self): return len(self.__writebuffer) @@ -554,6 +569,8 @@ class Client(object): else: self.__readbuffer += data self.__parseReadBuffer() + self.__timestamp = time.time() + self.__sentPing = False def socketWritableNotification(self): try: @@ -564,8 +581,8 @@ class Client(object): def disconnect(self, quitmsg): self.message("ERROR :%s" % quitmsg) - self.__server.printInfo("Disconnected connection from %s:%s." % ( - self.__host, self.__port)) + self.__server.printInfo("Disconnected connection from %s:%s (%s)." % ( + self.__host, self.__port, quitmsg)) self.socket.close() self.__server.removeClient(self, quitmsg) @@ -679,12 +696,15 @@ class Server(object): serversockets.append(s) del s self.printInfo("Listening on port %d." % port) + + lastAlivenessCheck = time.time() while True: iwtd, owtd, ewtd = select.select( serversockets + [x.socket for x in self.__clients.values()], [x.socket for x in self.__clients.values() if x.writeQueueSize() > 0], - []) + [], + 10) for x in iwtd: if x in self.__clients: self.__clients[x].socketReadableNotification() @@ -695,6 +715,11 @@ class Server(object): addr[0], addr[1])) for x in owtd: self.__clients[x].socketWritableNotification() + now = time.time() + if lastAlivenessCheck + 10 < now: + for client in self.__clients.values(): + client.checkAliveness() + lastAlivenessCheck = now _alpha = "abcdefghijklmnopqrstuvwxyz" _special = "-[]\\`^{}" -- cgit v1.2.3