Handle StreamClosed
in ping protocol handler
This commit is contained in:
parent
9be9b4bbfc
commit
86e0fa4563
|
@ -1,7 +1,7 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from libp2p.network.stream.exceptions import StreamEOF, StreamReset
|
from libp2p.network.stream.exceptions import StreamClosed, StreamEOF, StreamReset
|
||||||
from libp2p.network.stream.net_stream_interface import INetStream
|
from libp2p.network.stream.net_stream_interface import INetStream
|
||||||
from libp2p.peer.id import ID as PeerID
|
from libp2p.peer.id import ID as PeerID
|
||||||
from libp2p.typing import TProtocol
|
from libp2p.typing import TProtocol
|
||||||
|
@ -25,17 +25,24 @@ async def _handle_ping(stream: INetStream, peer_id: PeerID) -> bool:
|
||||||
logger.debug("Other side closed while waiting for ping from %s", peer_id)
|
logger.debug("Other side closed while waiting for ping from %s", peer_id)
|
||||||
return False
|
return False
|
||||||
except StreamReset as error:
|
except StreamReset as error:
|
||||||
|
print("peer", peer_id, "ping reset")
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"Other side reset while waiting for ping from %s: %s", peer_id, error
|
"Other side reset while waiting for ping from %s: %s", peer_id, error
|
||||||
)
|
)
|
||||||
raise
|
raise
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
|
print("peer", peer_id, "ping", type(error))
|
||||||
logger.debug("Error while waiting to read ping for %s: %s", peer_id, error)
|
logger.debug("Error while waiting to read ping for %s: %s", peer_id, error)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
logger.debug("Received ping from %s with data: 0x%s", peer_id, payload.hex())
|
logger.debug("Received ping from %s with data: 0x%s", peer_id, payload.hex())
|
||||||
|
print("receive ping from", peer_id)
|
||||||
|
|
||||||
|
try:
|
||||||
await stream.write(payload)
|
await stream.write(payload)
|
||||||
|
except StreamClosed:
|
||||||
|
logger.debug("Fail to respond to ping from %s: stream closed", peer_id)
|
||||||
|
raise
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,11 +51,13 @@ async def handle_ping(stream: INetStream) -> None:
|
||||||
or closes the ``stream``."""
|
or closes the ``stream``."""
|
||||||
peer_id = stream.muxed_conn.peer_id
|
peer_id = stream.muxed_conn.peer_id
|
||||||
|
|
||||||
|
print("handling ping from", peer_id)
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
should_continue = await _handle_ping(stream, peer_id)
|
should_continue = await _handle_ping(stream, peer_id)
|
||||||
if not should_continue:
|
if not should_continue:
|
||||||
return
|
return
|
||||||
except Exception:
|
except Exception:
|
||||||
|
print("error finish ping")
|
||||||
await stream.reset()
|
await stream.reset()
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue
Block a user