From 21ea762b46653fa0417436be8bb2baa4f7c2fac1 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Sat, 2 Nov 2013 13:05:58 -0400 Subject: [PATCH] Further progress state of tlvdata Correct read to recv and add some more info that will be required. --- confluent/common/tlvdata.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/confluent/common/tlvdata.py b/confluent/common/tlvdata.py index 98854a3b..f9be3c42 100644 --- a/confluent/common/tlvdata.py +++ b/confluent/common/tlvdata.py @@ -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)