async def run(self) -> None: # Our multiplexer will already be streaming in the background (as it was used during # handshake), so we do this to ensure we only start if it is still running. self._multiplexer.raise_if_streaming_error() for protocol in self._multiplexer.get_protocols(): self.manager.run_daemon_task(self._feed_protocol_handlers, protocol) try: await self._multiplexer.wait_streaming_finished() except PeerConnectionLost: pass except MalformedMessage as err: self.logger.debug( "Disconnecting peer %s for sending MalformedMessage: %s", self.remote, err, exc_info=True, ) try: self.get_base_protocol().send( Disconnect(DisconnectReason.BAD_PROTOCOL)) except PeerConnectionLost: self.logger.debug( "%s went away while trying to disconnect for MalformedMessage", self, ) finally: self.manager.cancel()
async def run(self) -> None: try: async with self._multiplexer.multiplex(): for protocol in self._multiplexer.get_protocols(): self.manager.run_daemon_task(self._feed_protocol_handlers, protocol) await self.manager.wait_finished() except PeerConnectionLost: pass except (MalformedMessage, ) as err: self.logger.debug( "Disconnecting peer %s for sending MalformedMessage: %s", self.remote, err, ) try: self.get_base_protocol().send( Disconnect(DisconnectReason.BAD_PROTOCOL)) except PeerConnectionLost: self.logger.debug( "%s went away while trying to disconnect for MalformedMessage", self, ) finally: await self._multiplexer.close()
async def _run(self) -> None: try: async with self._multiplexer.multiplex(): for protocol in self._multiplexer.get_protocols(): self.run_daemon_task( self._feed_protocol_handlers(protocol)) await self.cancellation() except (PeerConnectionLost, asyncio.CancelledError): pass except (MalformedMessage, ) as err: self.logger.debug( "Disconnecting peer %s for sending MalformedMessage: %s", self.remote, err, ) try: self.get_base_protocol().send( Disconnect(DisconnectReason.BAD_PROTOCOL)) except PeerConnectionLost: self.logger.debug( "%s went away while trying to disconnect for MalformedMessage", self, ) pass
async def test_p2p_api_triggers_cancellation_on_disconnect(bob, alice): async with P2PAPI().as_behavior().apply(alice): p2p_api = alice.get_logic('p2p', P2PAPI) bob.get_base_protocol().send( Disconnect(DisconnectReason.CLIENT_QUITTING)) await asyncio.wait_for(alice.events.cancelled.wait(), timeout=1) assert p2p_api.remote_disconnect_reason is DisconnectReason.CLIENT_QUITTING assert p2p_api.local_disconnect_reason is None
def send_disconnect(self, reason: DisconnectReason) -> None: self.logger.debug2("Sending Disconnect on %s", self.connection) self.connection.get_base_protocol().send(Disconnect(reason))
def send_disconnect(self, reason: DisconnectReason) -> None: self.connection.get_base_protocol().send(Disconnect(reason))