Properly handle broken connections

master
Sven Slootweg 13 years ago
parent 6f894818ed
commit d47f592d60

@ -74,9 +74,13 @@ class listener:
self.client_map[connstream.fileno()] = new_client self.client_map[connstream.fileno()] = new_client
self.client_list.append(new_client) self.client_list.append(new_client)
else: else:
data = sock.recv(1024)
cur_client = self.client_map[sock.fileno()] cur_client = self.client_map[sock.fileno()]
try:
data = sock.recv(1024)
except socket.error, (value, message):
cur_client.abort(message)
else:
if data: if data:
cur_client.process_data(data) cur_client.process_data(data)
else: else:
@ -106,10 +110,7 @@ class client:
self.user = user(self, server) self.user = user(self, server)
self.listener = listener self.listener = listener
def send_chunk(self, chunk): def abort(self, reason):
try:
self.stream.send(chunk + EOC)
except socket.error:
# TODO: Log quit reason # TODO: Log quit reason
try: try:
self.stream.shutdown(2) self.stream.shutdown(2)
@ -119,7 +120,13 @@ class client:
self.end() self.end()
self.listener.select_inputs = remove_from_list(self.listener.select_inputs, self.stream) self.listener.select_inputs = remove_from_list(self.listener.select_inputs, self.stream)
print "NOTICE: Client disconnected, possibly due to socket error" print "NOTICE: Client disconnected, possibly due to socket error: %s" % reason
def send_chunk(self, chunk):
try:
self.stream.send(chunk + EOC)
except socket.error, (value, message):
self.abort(message)
def send_global_notice(self, notice): def send_global_notice(self, notice):
self.send_chunk(":%s NOTICE %s" % (config_ownhost, notice)) self.send_chunk(":%s NOTICE %s" % (config_ownhost, notice))

Loading…
Cancel
Save