-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcom_udp.py
63 lines (54 loc) · 1.98 KB
/
com_udp.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
import sys
import socket
import threading
import com_base
class UDPServer(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_DGRAM)
try:
self.sock.bind((app.config['socket_ip'], app.config['socket_port']))
except OSError:
raise RuntimeError('Failed to create socket')
self.running = True
self.buf_size = 1024 * 1024 * 100
self.messages = {}
threading.Thread(target=self.client_thread).start()
def update(self) -> None:
pass
def client_thread(self) -> None:
# TODO: multiple clients at the same time
while self.running:
try:
encoded_msg, new_addr = self.sock.recvfrom(self.buf_size)
except OSError:
continue
msg = self.decode_msg(encoded_msg)
self.commands.append(msg)
# self.should_kill = False
def destroy(self) -> None:
self.running = False
if self.sock:
self.sock.close()
self.sock = None
self.app = None
class UDPClient(com_base.BaseClient):
def __init__(self, app: any) -> None:
super().__init__(app)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.server_addr = (app.config['socket_ip'], app.config['socket_port'])
def send(self, msg: str) -> None:
if not msg or msg == 'i_want_to_live_please_do\'nt_die':
return
encoded_msg = com_base.BaseServer.encode_msg(msg)
try:
self.sock.sendto(encoded_msg, self.server_addr)
except Exception as _err:
raise RuntimeError(str(_err))
def destroy(self) -> None:
super().destroy()
if self.sock:
self.sock.close()
self.sock = None