def deserialize_and_process_message(self, stream, address=None, **kwargs): message = None # This uses the dask protocol to break up the message/receive the chunks of the message. try: n_frames = yield stream.read_bytes(8) n_frames = struct.unpack("Q", n_frames)[0] lengths = yield stream.read_bytes(8 * n_frames) lengths = struct.unpack("Q" * n_frames, lengths) frames = [] for length in lengths: if length: frame = bytearray(length) n = yield stream.read_into(frame) assert n == length, (n, length) else: frame = b"" frames.append(frame) except StreamClosedError as e: print( "[ {} ] [ERROR] - Stream Closed Error: stream from address {} is closed..." .format(datetime.datetime.utcnow(), address)) print("Real Error: ", e.real_error.__str__()) raise except AssertionError as e: _, _, tb = sys.exc_info() traceback.print_tb(tb) # Fixed format tb_info = traceback.extract_tb(tb) filename, line, func, text = tb_info[-1] print( 'An error occurred on line {} in statement {}. Currently processing a stream from addresss {}.' .format(line, text, address)) raise else: try: message = yield from_frames(frames) except EOFError: print( "[ {} ] [ERROR] - Aborted stream on truncated data".format( datetime.datetime.utcnow())) raise CommClosedError("aborted stream on truncated data") # The 'op' (operation) entry specifies what the Proxy should do. op = message["op"] print("[ DEBUG ] \n\tMessage: {}".format(message)) yield self.handlers[op](message, address=address, stream=stream)
def read(self, deserializers=None): # print("[ {} ] Attempting to read in TCP Comm...".format(datetime.datetime.utcnow())) stream = self.stream if stream is None: raise CommClosedError try: n_frames = yield stream.read_bytes(8) n_frames = struct.unpack("Q", n_frames)[0] lengths = yield stream.read_bytes(8 * n_frames) lengths = struct.unpack("Q" * n_frames, lengths) frames = [] # print("[ {} ] Reading {} lengths now...".format(datetime.datetime.utcnow(), len(lengths))) for length in lengths: if length: if PY3 and self._iostream_has_read_into: frame = bytearray(length) n = yield stream.read_into(frame) assert n == length, (n, length) else: frame = yield stream.read_bytes(length) else: frame = b"" frames.append(frame) except StreamClosedError as e: self.stream = None print("StreamClosedError...") raise StreamClosedError("Stream closed...") else: try: msg = yield serialization.from_frames( frames, deserialize=self.deserialize, deserializers=deserializers) except EOFError: # Frames possibly garbled or truncated by communication error self.abort() # print("aborted stream on truncated data") raise CommClosedError("aborted stream on truncated data") raise gen.Return(msg)
def deserialize_payload(payload): msg = yield from_frames(payload) raise gen.Return(msg)