def get_protocol_command_for(self, msg: bytes) -> protocol.Command: """Return the Command corresponding to the cmd_id encoded in the given msg.""" cmd_id = get_devp2p_cmd_id(msg) self.logger.debug("Got msg with cmd_id: %s", cmd_id) if cmd_id < self.base_protocol.cmd_length: proto = self.base_protocol elif cmd_id < self.sub_proto.cmd_id_offset + self.sub_proto.cmd_length: proto = self.sub_proto # type: ignore else: raise UnknownProtocolCommand("No protocol found for cmd_id {}".format(cmd_id)) return proto.cmd_by_id[cmd_id]
def process_msg(self, msg: bytes) -> protocol._DecodedMsgType: cmd_id = get_devp2p_cmd_id(msg) self.logger.debug("Got msg with cmd_id: {}".format(cmd_id)) proto = self.get_protocol_for(cmd_id) if proto is None: raise UnknownProtocolCommand( "No protocol found for cmd_id {}".format(cmd_id)) decoded = proto.process(cmd_id, msg) if decoded is None: return None # Check if this is a reply we're waiting for and, if so, call the callback for this # request_id. request_id = decoded.get('request_id') if request_id is not None and request_id in self._pending_replies: callback = self._pending_replies.pop(request_id) callback(decoded) return decoded
def process_msg( self, msg: bytes) -> Tuple[protocol.Command, protocol._DecodedMsgType]: cmd_id = get_devp2p_cmd_id(msg) self.logger.debug("Got msg with cmd_id: %s", cmd_id) proto = self.get_protocol_for(cmd_id) if proto is None: raise UnknownProtocolCommand( "No protocol found for cmd_id {}".format(cmd_id)) cmd, decoded = proto.process(cmd_id, msg) if isinstance(decoded, dict): # Check if this is a reply we're waiting for and, if so, call the callback for this # request_id. request_id = decoded.get('request_id') if request_id is not None and request_id in self._pending_replies: callback = self._pending_replies.pop(request_id) callback(decoded) if self.received_msg_callback is not None: self.received_msg_callback(self, cmd, decoded) return cmd, decoded