def handle_p2p_msg(self, cmd: protocol.Command, msg: protocol._DecodedMsgType): """Handle the base protocol (P2P) messages.""" if isinstance(cmd, Disconnect): msg = cast(Dict[str, Any], msg) self.logger.debug( "%s disconnected; reason given: %s", self, msg['reason_name']) self.close() elif isinstance(cmd, Ping): self.base_protocol.send_pong() elif isinstance(cmd, Pong): # Currently we don't do anything when we get a pong, but eventually we should # update the last time we heard from a peer in our DB (which doesn't exist yet). pass else: raise UnexpectedMessage("Unexpected msg: {} ({})".format(cmd, msg))
async def _handle_peer(self, peer: LESPeer) -> None: self._announcement_queue.put_nowait((peer, peer.head_info)) while True: try: cmd, msg = await peer.read_sub_proto_msg() except PeerFinished: break # We currently implement only the LES commands for retrieving data (apart from # Announce), and those should always come as a response to requests we make so will be # handled by LESPeer.handle_sub_proto_msg(). if isinstance(cmd, les.Announce): peer.head_info = cmd.as_head_info(msg) self._announcement_queue.put_nowait((peer, peer.head_info)) else: raise UnexpectedMessage("Unexpected msg from %s: %s", peer, msg) await self.drop_peer(peer)
async def _handle_peer(self, peer: LESPeer) -> None: self._announcement_queue.put_nowait((peer, peer.head_info)) while True: try: cmd, msg = await peer.read_sub_proto_msg(self.cancel_token) except OperationCancelled: # Either the peer disconnected or our cancel_token has been triggered, so break # out of the loop to stop attempting to sync with this peer. break # We currently implement only the LES commands for retrieving data (apart from # Announce), and those should always come as a response to requests we make so will be # handled by LESPeer.handle_sub_proto_msg(). if isinstance(cmd, les.Announce): peer.head_info = cmd.as_head_info(msg) self._announcement_queue.put_nowait((peer, peer.head_info)) else: raise UnexpectedMessage("Unexpected msg from {}: {}".format( peer, msg)) await self.drop_peer(peer) self.logger.debug("%s finished", peer)