-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoundation.py
42 lines (27 loc) · 1016 Bytes
/
foundation.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
# foundation.py
# Foundation code for the stack.
# This code is released to the public domain.
# "Share and enjoy....." :)
import socket
from protocol import parse_packet, packet_to_bytes
class Foundation:
"""
A simple wrapper around a UDP socket for sending/receiving packets.
This class just abstracts sending and receiving JSON packets.
"""
def __init__(self, addr, is_server=False):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.addr = addr
if is_server:
self.sock.bind(addr)
self.mode = "server"
else:
self.mode = "client"
self.sock.settimeout(5.0) # Set a timeout for simplicity
def send_packet(self, packet: dict, dest_addr):
data = packet_to_bytes(packet)
self.sock.sendto(data, dest_addr)
def receive_packet(self):
data, addr = self.sock.recvfrom(65535)
packet = parse_packet(data)
return packet, addr