-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathws_connection.py
65 lines (49 loc) · 1.54 KB
/
ws_connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import socket
from websocket import websocket
class ClientClosedError(Exception):
pass
class WebSocketConnection:
def __init__(self, addr, s, close_callback):
self.client_close = False
self._need_check = False
self.address = addr
self.socket = s
self.ws = websocket(s, True)
self.close_callback = close_callback
s.setblocking(False)
s.setsockopt(socket.SOL_SOCKET, 20, self.notify)
def notify(self, s):
self._need_check = True
def read(self):
if self._need_check:
self._check_socket_state()
msg_bytes = None
try:
msg_bytes = self.ws.read()
except OSError:
self.client_close = True
if not msg_bytes and self.client_close:
raise ClientClosedError()
return msg_bytes
def write(self, msg):
try:
self.ws.write(msg)
except OSError:
self.client_close = True
def _check_socket_state(self):
self._need_check = False
sock_str = str(self.socket)
state_str = sock_str.split(" ")[1]
state = int(state_str.split("=")[1])
if state == 4:
self.client_close = True
def is_closed(self):
return self.socket is None
def close(self):
print("Closing connection.")
self.socket.setsockopt(socket.SOL_SOCKET, 20, None)
self.socket.close()
self.socket = None
self.ws = None
if self.close_callback:
self.close_callback(self)