Commit 57af2bf1 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Improved CLOSE handshake implementation, it should work now...

parent f44555a8
...@@ -21,9 +21,7 @@ class Connection(object): ...@@ -21,9 +21,7 @@ class Connection(object):
""" """
self.sock = sock self.sock = sock
self.close_frame_sent = False
self.close_frame_received = False self.close_frame_received = False
self.ping_sent = False self.ping_sent = False
self.ping_payload = None self.ping_payload = None
...@@ -72,18 +70,10 @@ class Connection(object): ...@@ -72,18 +70,10 @@ class Connection(object):
Handle a control frame as defined by RFC 6455. Handle a control frame as defined by RFC 6455.
""" """
if frame.opcode == OPCODE_CLOSE: if frame.opcode == OPCODE_CLOSE:
# Handle a close message by sending a response close message if no # Close the connection from this end as well
# CLOSE frame was sent before, and closing the connection. The
# onclose() handler is called afterwards.
self.close_frame_received = True self.close_frame_received = True
code, reason = frame.unpack_close() code, reason = frame.unpack_close()
if not self.close_frame_sent:
payload = '' if code is None else struct.pack('!H', code)
self.sock.send(ControlFrame(OPCODE_CLOSE, payload))
self.sock.close()
# No more receiving data after a close message # No more receiving data after a close message
raise SocketClosed(code, reason) raise SocketClosed(code, reason)
...@@ -114,19 +104,12 @@ class Connection(object): ...@@ -114,19 +104,12 @@ class Connection(object):
try: try:
self.onmessage(self.receive()) self.onmessage(self.receive())
except SocketClosed as e: except SocketClosed as e:
self.onclose(e.code, e.reason) self.close()
#self.onclose(e.code, e.reason)
break break
except Exception as e: except Exception as e:
self.onerror(e) self.onerror(e)
def send_close(self, code, reason):
"""
Send a CLOSE control frame.
"""
payload = '' if code is None else struct.pack('!H', code) + reason
self.sock.send(ControlFrame(OPCODE_CLOSE, payload))
self.close_frame_sent = True
def send_ping(self, payload=''): def send_ping(self, payload=''):
""" """
Send a PING control frame with an optional payload. Send a PING control frame with an optional payload.
...@@ -136,27 +119,18 @@ class Connection(object): ...@@ -136,27 +119,18 @@ class Connection(object):
self.ping_sent = True self.ping_sent = True
self.onping(payload) self.onping(payload)
def handle_close(self, code=None, reason=''):
"""
Handle a close message by sending a response close message if no CLOSE
frame was sent before, and closing the connection. The onclose()
handler is called afterwards.
"""
if not self.close_frame_sent:
payload = '' if code is None else struct.pack('!H', code)
self.sock.send(ControlFrame(OPCODE_CLOSE, payload))
self.sock.close()
self.onclose(code, reason)
def close(self, code=None, reason=''): def close(self, code=None, reason=''):
""" """
Close the socket by sending a CLOSE frame and waiting for a response Close the socket by sending a CLOSE frame and waiting for a response
close message. The onclose() handler is called after the CLOSE frame close message, unless such a message has already been received earlier
has been sent, but before the response has been received. (prior to calling this function, for example). The onclose() handler is
called after the response has been received.
""" """
self.send_close(code, reason) # Send CLOSE frame
payload = '' if code is None else struct.pack('!H', code) + reason
self.sock.send(ControlFrame(OPCODE_CLOSE, payload))
# Receive CLOSE frame
if not self.close_frame_received: if not self.close_frame_received:
frame = self.sock.recv() frame = self.sock.recv()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment