-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomm.py
More file actions
39 lines (33 loc) · 878 Bytes
/
comm.py
File metadata and controls
39 lines (33 loc) · 878 Bytes
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
# {'msg_type' : 'HELLO', serial: "12356"}
import json
import struct
import pbs
from pbs import ECEncoder
#USED FOR MESSAGE TYPE TRACKING
HELLO = 1
PARAMS = 2
SIGNREQ = 3
BLINDED = 4
END = 5
ERROR = -1
def sendError(conn, err):
msg = {"msg_type": ERROR, "error": err}
sendMessage(conn, msg)
def sendMessage(conn, data: dict):
payload = json.dumps(data, cls=ECEncoder).encode()
packet = struct.pack(">I", len(payload)) + payload
#print(f"Sending {len(payload)} bytes")
conn.send(packet)
def recvN(conn, n):
dat = b''
c = 0
while c < n:
newdat = conn.recv(n-c)
c += len(newdat)
dat += newdat
return dat
def recvMessage(conn):
toRecv = struct.unpack(">I", recvN(conn, 4))[0]
print(f"Going to recv {toRecv} bytes")
payload = recvN(conn, toRecv)
return json.loads(payload, object_hook=pbs.as_dict)