-
Notifications
You must be signed in to change notification settings - Fork 0
/
com_tcp.py
97 lines (87 loc) · 3.18 KB
/
com_tcp.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import sys
import socket
import threading
import com_base
class TCPServer(com_base.BaseServer):
def __init__(self, app: any) -> None:
super().__init__()
self.should_kill = not sys.platform == 'win32'
self.app = app
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.sock.bind((app.config['socket_ip'], app.config['socket_port']))
except OSError:
raise RuntimeError('Failed to create socket')
# self.sock.setblocking(False)
self.clients = []
self.sock.listen()
self.running = True
threading.Thread(target=self.accept_clients).start()
def update(self) -> None:
pass
def accept_clients(self) -> None:
while self.running and self.sock:
try:
conn, addr = self.sock.accept()
except OSError:
break
self.clients.append(conn)
threading.Thread(target=self.client_thread, args=(conn, )).start()
def client_thread(self, conn: socket.socket) -> None:
should_exit = True
while self.running:
try:
msg_len_buf = conn.recv(10)
if not msg_len_buf:
continue
msg_len = int.from_bytes(msg_len_buf, 'little', signed=False)
encoded_msg = conn.recv(msg_len)
except OSError:
return
msg = self.decode_msg(encoded_msg)
if should_exit:
if msg == 'i_want_to_live_please_do\'nt_die':
should_exit = False
continue
self.commands.append(msg)
conn.close()
if conn in self.clients:
self.clients.remove(conn)
return
if msg == 'disconnect':
conn.close()
if conn in self.clients:
self.clients.remove(conn)
return
self.commands.append(msg)
# self.should_kill = False
def destroy(self) -> None:
for conn in self.clients:
conn.close()
self.clients.clear()
self.running = False
if self.sock:
self.sock.close()
self.sock = None
self.app = None
class TCPClient(com_base.BaseClient):
def __init__(self, app: any) -> None:
super().__init__(app)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.sock.connect((app.config['socket_ip'], app.config['socket_port']))
except Exception as _err:
raise RuntimeError(str(_err))
def send(self, msg: str) -> None:
if not msg:
return
encoded_msg = com_base.BaseServer.encode_msg(msg)
try:
self.sock.send(int.to_bytes(len(encoded_msg), 10, 'little', signed=False) + encoded_msg)
except Exception as _err:
raise RuntimeError(str(_err))
def destroy(self) -> None:
super().destroy()
if self.sock:
self.sock.close()
self.sock = None