-
Notifications
You must be signed in to change notification settings - Fork 4
/
connection.py
41 lines (32 loc) · 1.07 KB
/
connection.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
import socket
import struct
import random
import sys
import util
class Connection:
def __init__(self, host, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.settimeout(5)
self.sock.connect((host, port))
self.cache = {}
def readparam(self, address, slot, index, cached=True):
key = (address, slot, index)
if cached and key in self.cache:
return self.cache[key]
for i in range(0, 3):
marker = random.randint(0x00, 0xff)
packet = struct.pack("BBBB", marker, address, slot, index)
self.sock.send(packet)
try:
data = self.sock.recv(256)
except socket.timeout:
print("Timeout!", file=sys.stderr)
return b""
# check marker
if data[0] != marker:
return b""
data = data[1:]
if len(data) > 0:
break
self.cache[key] = data
return data