-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_server.py
83 lines (62 loc) · 2.44 KB
/
udp_server.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
import socket
import struct
import asyncio
import time
# from py_frame_buffer import FrameBuffer
from cy_frame_buffer import FrameBuffer
# Header format: frame_number, packet_idx, total_packets, packet_size, timestamp
HeaderStruct = struct.Struct("!IIIId")
unpack_header = HeaderStruct.unpack
SERVER_IP = "0.0.0.0"
class FrameBufferManager:
def __init__(self):
self.buffers: dict[int, FrameBuffer] = {} # Client ID => Frame Buffer
self.last_clean_up = time.time()
def add_packet(
self,
client_id: int,
data: bytes,
):
frame_number, packet_idx, total_packets, packet_size, timestamp = unpack_header(
data[: HeaderStruct.size]
)
if not (buf := self.buffers.get(client_id)):
buf = self.buffers[client_id] = FrameBuffer(packet_size)
body_data = data[HeaderStruct.size :]
buf.add_packet(frame_number, timestamp, packet_idx, total_packets, body_data)
self.cleanup()
def cleanup(self):
current_time = time.time()
if current_time - self.last_clean_up > 1:
for buf in self.buffers.values():
buf.cleanup()
self.last_clean_up = current_time
class EchoDatagramProtocol(asyncio.DatagramProtocol):
def __init__(self, buffer_manager):
self.buffer_manager: FrameBufferManager = buffer_manager
def datagram_received(self, data, addr):
client_id = addr[1]
self.buffer_manager.add_packet(client_id, data)
async def async_udp_server(host=SERVER_IP, port=8003):
loop = asyncio.get_running_loop()
buffer_manager = FrameBufferManager()
transport, protocol = await loop.create_datagram_endpoint(
lambda: EchoDatagramProtocol(buffer_manager), local_addr=(host, port)
)
print(f"Server listening on {host}:{port}")
try:
await asyncio.Future() # Run forever
finally:
transport.close()
def udp_server(host=SERVER_IP, port=8003):
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.bind((host, port))
buffer_manager = FrameBufferManager()
print(f"UDP Server listening on {host}:{port}")
while True:
message, client_address = sock.recvfrom(1080 * 61)
client_id = client_address[1] # Using client's port as a simple identifier
buffer_manager.add_packet(client_id, message)
if __name__ == "__main__":
udp_server()
# asyncio.run(async_udp_server())