def send_and_receive_response(address, message, timeout=10): """ j.w. ale dekoduje wynik i go zwraca, lub rzuca otrzymany w wiadomoĊci exception """ result = send_and_receive(address, message, timeout) typ = result['message'] if typ == messages.RESULT: return result['result'] elif typ == messages.ERROR: e = exception_deserialize(result) if e is None: raise exceptions.MessageCorrupted() raise e else: raise exceptions.ServiceBusException("Wrong message type received")
def decode_header(self, packet): if len(packet) < self.header.size: raise exceptions.MessageCorrupted() busname, ver, psize, iv, cmpr, trim, resreq = self.header.unpack( packet) # not our service bus if busname != self._busname: raise exceptions.NotOurMessage() # check protocol version if ver != self._version: raise exceptions.ServiceBusException( "Unknown service bus protocol version") return (psize, iv, cmpr, trim, resreq)
def _plain_deserialize(self, msg): # raw message or header, data tuple? if type(msg) is tuple: psize, iv, cmpr, trim, resreq = msg[0] msg = msg[1] else: HS = self.header.size header = msg[:HS] msg = msg[HS:] psize, iv, cmpr, trim, resreq = self.decode_header(header) # check data size declared in header #if (len(msg)-HS) != psize: # raise exceptions.MessageCorrupted() try: return self.bin_2_data(msg), resreq except: raise exceptions.MessageCorrupted()
def _encrypted_deserialize(self, msg): """ msg - message or header tuple of header parameters and message body (tithout header) """ if type(msg) is tuple: psize, iv, cmpr, trim, resreq = msg[0] msg = msg[1] else: HS = self.header.size header = msg[:HS] msg = msg[HS:] psize, iv, cmpr, trim, resreq = self.decode_header(header) # decrypt try: pckt = {"iv": iv, "payload": msg, "trim": trim} msg = self.decrypt(pckt, self._passwd) except Exception: raise exceptions.MessageCorrupted() # unpack message return self.bin_2_data(msg), resreq
def bin_2_data(bin): return msgpack.unpackb(bin, object_hook=decode_obj_types) try: pass except msgpack.exceptions.UnpackException: raise exceptions.MessageCorrupted()
def bin_2_data(bin): try: return bson.loads(bin) except: raise exceptions.MessageCorrupted()
def bin_2_data(bin): try: return dict(py3bson.parse_bytes(bin)) except py3bson.BSON_Error: raise exceptions.MessageCorrupted()
def bin_2_data(bin): try: return pickle.loads(bin) except Exception as e: raise exceptions.MessageCorrupted()