-
Notifications
You must be signed in to change notification settings - Fork 2
/
udp_bitmap_decoder.py
56 lines (46 loc) · 2.08 KB
/
udp_bitmap_decoder.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
from prot_http.const_wifi import UDP_PORT_LIVEVIEW
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
print("Started receiver!")
sock.bind(('', UDP_PORT_LIVEVIEW))
sock.settimeout(2)
data = bytearray(b'')
data_idx_frame = None
data_idx_last_packet = -1
data_valid : bool = True
was_last_frame_invalid : bool = False
try:
while True:
try:
pack, addr = sock.recvfrom(1024000)
if len(pack) < 12:
continue
idx_frame = int.from_bytes(pack[:4], byteorder='big')
len_packet_frame = int.from_bytes(pack[4:8], byteorder='big')
idx_packet_frame = int.from_bytes(pack[8:12], byteorder='big')
# If the reconstructed frame is not the delivered one, start reconstruction again
if data_idx_frame != idx_frame:
data = bytearray(b'')
data_idx_frame = idx_frame
data_idx_last_packet = -1
data_valid = True
if data_valid:
if (idx_packet_frame - 1) == data_idx_last_packet:
data.extend(pack[12:])
data_idx_last_packet = idx_packet_frame
else:
data_valid = False
continue
if (data_idx_last_packet == len_packet_frame - 1):
if len(data) > 2048:
with open("packet_%d.txt" % data_idx_frame, 'wb') as out:
out.write(data[:2048])
with open("packet_%d.jpg" % data_idx_frame, 'wb') as out:
out.write(data[2048:])
else:
with open("packet_unk_%d.raw" % data_idx_frame, 'wb') as out:
out.write(data)
except TimeoutError:
print("Timed out...")
except KeyboardInterrupt:
pass