def _reply_handler(self): '''Reply-handling coroutine. Must not throw exceptions.''' while True: try: buf = yield gen.Task(self._read, RPCHeader.ENCODED_LENGTH) hdr = RPCHeader.decode(buf) # Always read the body, regardless of encoding errors, to # keep the stream in sync data = yield gen.Task(self._read, hdr.datalen) except ConnectionFailure: # Error out pending callbacks callbacks = self._pending.values() self._pending.clear() for callback in callbacks: callback(None, '') return if hdr.status == RPC_PENDING: _log.warning('RPC client received request message') self._send_message(hdr.sequence, RPCEncodingError.code, hdr.cmd) continue try: callback = self._pending.pop(hdr.sequence) except KeyError: _log.warning('Received reply with no registered callback') # Drop on the floor continue try: callback(hdr.status, data) except Exception: # Don't crash the coroutine if the callback fails _log.exception('Reply callback raised exception')
def _get_reply(self): buf = self._read_bytes(self._sock, RPCHeader.ENCODED_LENGTH) hdr = RPCHeader.decode(buf) # Always read the body, regardless of encoding errors, to # keep the stream in sync data = self._read_bytes(self._sock, hdr.datalen) if hdr.status == RPC_PENDING: _log.warning('RPC client received request message') self._send_message(hdr.sequence, RPCEncodingError.code, hdr.cmd) return hdr.status, data
def _send_message(self, sequence, status, cmd, body=''): hdr = RPCHeader(sequence, status, cmd, len(body)) self._write(hdr.encode() + body)
def _send_message(self, sequence, status, cmd, body=''): hdr = RPCHeader(sequence, status, cmd, len(body)) self._sock.sendall(hdr.encode() + body)