mirror of
https://github.com/xcat2/confluent.git
synced 2024-11-23 10:02:04 +00:00
Further progress state of tlvdata
Correct read to recv and add some more info that will be required.
This commit is contained in:
parent
35a5e9cbf0
commit
21ea762b46
@ -9,10 +9,10 @@ def send_tlvdata(handle, data):
|
||||
if tl < 16777216:
|
||||
#type for string is '0', so we don't need
|
||||
#to xor anything in
|
||||
handle.write(struct.pack("!I", tl))
|
||||
handle.sendall(struct.pack("!I", tl))
|
||||
else:
|
||||
raise Exception("String data length exceeds protocol")
|
||||
handle.write(data)
|
||||
handle.sendall(data)
|
||||
handle.flush()
|
||||
elif isinstance(data, dict): # JSON currently only goes to 4 bytes
|
||||
sdata = json.dumps(data, separators=(',',':'))
|
||||
@ -21,12 +21,12 @@ def send_tlvdata(handle, data):
|
||||
raise Exception("JSON data exceeds protocol limits")
|
||||
# xor in the type (0b1 << 24)
|
||||
tl |= 16777216
|
||||
handle.write(struct.pack("!I", tl))
|
||||
handle.sendall(struct.pack("!I", tl))
|
||||
handle.write(sdata)
|
||||
handle.flush()
|
||||
|
||||
def recv_tlvdata(handle):
|
||||
tl = handle.read(4)
|
||||
tl = handle.recv(4)
|
||||
tl = struct.unpack("!B", tl)[0]
|
||||
if tl & 0b10000000:
|
||||
raise Exception("Protocol Violation, reserved bit set")
|
||||
@ -34,7 +34,11 @@ def recv_tlvdata(handle):
|
||||
dlen = tl & 16777215 # 24 ones
|
||||
type = (tl & 2130706432) >> 24 # 7 ones, followed by 24 zeroes
|
||||
if type == 0:
|
||||
return(handle.read(dlen))
|
||||
data = handle.recv(dlen)
|
||||
while len(data) < dlen:
|
||||
ndata = handle.recv(dlen - len(data))
|
||||
if not ndata:
|
||||
raise Exception("Error reading data")
|
||||
elif type == 1:
|
||||
sdata = handle.read(dlen)
|
||||
sdata = handle.recv(dlen)
|
||||
return json.loads(sdata)
|
||||
|
Loading…
Reference in New Issue
Block a user