-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrecevier.py
49 lines (39 loc) · 1.13 KB
/
recevier.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
import socket, struct
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("localhost", 2873))
TypeFormats = [
"", # null
"d", # double (float64)
"?", # bool
"ddd"] # Vector3d
class Packet:
def __init__(self, data):
self.data = data
self.pos = 0
def get(self, l): # get l bytes
self.pos += l
return self.data[self.pos - l:self.pos]
def read(self, fmt): # read all formatted values
v = self.readmult(fmt)
if len(v) == 0: return None
if len(v) == 1: return v[0]
return v
def readmult(self, fmt): # read multiple formatted values
return struct.unpack(fmt, self.get(struct.calcsize(fmt)))
@property
def more(self): # is there more data?
return self.pos < len(self.data)
def readPacket(dat):
p = Packet(dat)
while p.more:
nameLen = p.read("H")
name = p.get(nameLen).decode()
tp = p.read("B")
typeCode = TypeFormats[tp]
val = p.read(typeCode)
print(name, "=", val)
print("")
print("Starting")
while 1:
d, a = s.recvfrom(2048)
readPacket(d)