Properly handle broken connections

master
Sven Slootweg 13 years ago
parent 6f894818ed
commit d47f592d60

@ -74,15 +74,19 @@ 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()]
if data: try:
cur_client.process_data(data) data = sock.recv(1024)
except socket.error, (value, message):
cur_client.abort(message)
else: else:
cur_client.end() if data:
self.select_inputs = remove_from_list(self.select_inputs, sock) cur_client.process_data(data)
print "NOTICE: Client disconnected" else:
cur_client.end()
self.select_inputs = remove_from_list(self.select_inputs, sock)
print "NOTICE: Client disconnected"
except ssl.SSLError, err: except ssl.SSLError, err:
if err.args[0] == ssl.SSL_ERROR_WANT_READ: if err.args[0] == ssl.SSL_ERROR_WANT_READ:
select.select([sock], [], []) select.select([sock], [], [])
@ -106,20 +110,23 @@ class client:
self.user = user(self, server) self.user = user(self, server)
self.listener = listener self.listener = listener
def abort(self, reason):
# TODO: Log quit reason
try:
self.stream.shutdown(2)
self.close()
except socket.error:
pass
self.end()
self.listener.select_inputs = remove_from_list(self.listener.select_inputs, self.stream)
print "NOTICE: Client disconnected, possibly due to socket error: %s" % reason
def send_chunk(self, chunk): def send_chunk(self, chunk):
try: try:
self.stream.send(chunk + EOC) self.stream.send(chunk + EOC)
except socket.error: except socket.error, (value, message):
# TODO: Log quit reason self.abort(message)
try:
self.stream.shutdown(2)
self.close()
except socket.error:
pass
self.end()
self.listener.select_inputs = remove_from_list(self.listener.select_inputs, self.stream)
print "NOTICE: Client disconnected, possibly due to socket error"
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