mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Fix all modules except for security
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
import trio
|
||||
|
||||
from libp2p.io.abc import ReadWriteCloser
|
||||
from libp2p.io.exceptions import IOException
|
||||
|
||||
@ -8,17 +6,17 @@ from .raw_connection_interface import IRawConnection
|
||||
|
||||
|
||||
class RawConnection(IRawConnection):
|
||||
read_write_closer: ReadWriteCloser
|
||||
stream: ReadWriteCloser
|
||||
is_initiator: bool
|
||||
|
||||
def __init__(self, read_write_closer: ReadWriteCloser, initiator: bool) -> None:
|
||||
self.read_write_closer = read_write_closer
|
||||
def __init__(self, stream: ReadWriteCloser, initiator: bool) -> None:
|
||||
self.stream = stream
|
||||
self.is_initiator = initiator
|
||||
|
||||
async def write(self, data: bytes) -> None:
|
||||
"""Raise `RawConnError` if the underlying connection breaks."""
|
||||
try:
|
||||
await self.read_write_closer.write(data)
|
||||
await self.stream.write(data)
|
||||
except IOException as error:
|
||||
raise RawConnError(error)
|
||||
|
||||
@ -30,9 +28,9 @@ class RawConnection(IRawConnection):
|
||||
Raise `RawConnError` if the underlying connection breaks
|
||||
"""
|
||||
try:
|
||||
return await self.read_write_closer.read(n)
|
||||
return await self.stream.read(n)
|
||||
except IOException as error:
|
||||
raise RawConnError(error)
|
||||
|
||||
async def close(self) -> None:
|
||||
await self.read_write_closer.close()
|
||||
await self.stream.close()
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from typing import TYPE_CHECKING, Any, Awaitable, List, Set, Tuple
|
||||
from typing import TYPE_CHECKING, Set, Tuple
|
||||
|
||||
from async_service import Service
|
||||
import trio
|
||||
@ -45,16 +45,11 @@ class SwarmConn(INetConn, Service):
|
||||
# before we cancel the stream handler tasks.
|
||||
await trio.sleep(0.1)
|
||||
|
||||
# FIXME: Now let `_notify_disconnected` finish first.
|
||||
# Schedule `self._notify_disconnected` to make it execute after `close` is finished.
|
||||
await self._notify_disconnected()
|
||||
|
||||
async def _handle_new_streams(self) -> None:
|
||||
while self.manager.is_running:
|
||||
try:
|
||||
print(
|
||||
f"!@# SwarmConn._handle_new_streams: {self.muxed_conn._id}: waiting for new streams"
|
||||
)
|
||||
stream = await self.muxed_conn.accept_stream()
|
||||
except MuxedConnUnavailable:
|
||||
# If there is anything wrong in the MuxedConn,
|
||||
@ -63,9 +58,6 @@ class SwarmConn(INetConn, Service):
|
||||
# Asynchronously handle the accepted stream, to avoid blocking the next stream.
|
||||
self.manager.run_task(self._handle_muxed_stream, stream)
|
||||
|
||||
print(
|
||||
f"!@# SwarmConn._handle_new_streams: {self.muxed_conn._id}: out of the loop"
|
||||
)
|
||||
await self.close()
|
||||
|
||||
async def _call_stream_handler(self, net_stream: NetStream) -> None:
|
||||
@ -92,8 +84,7 @@ class SwarmConn(INetConn, Service):
|
||||
await self.swarm.notify_disconnected(self)
|
||||
|
||||
async def run(self) -> None:
|
||||
self.manager.run_task(self._handle_new_streams)
|
||||
await self.manager.wait_finished()
|
||||
await self._handle_new_streams()
|
||||
|
||||
async def new_stream(self) -> NetStream:
|
||||
muxed_stream = await self.muxed_conn.open_stream()
|
||||
|
||||
@ -203,16 +203,17 @@ class Swarm(INetwork, Service):
|
||||
await self.add_conn(muxed_conn)
|
||||
|
||||
logger.debug("successfully opened connection to peer %s", peer_id)
|
||||
# FIXME: This is a intentional barrier to prevent from the handler exiting and
|
||||
# closing the connection. Probably change to `Service.manager.wait_finished`?
|
||||
await trio.sleep_forever()
|
||||
# NOTE: This is a intentional barrier to prevent from the handler exiting and
|
||||
# closing the connection.
|
||||
await self.manager.wait_finished()
|
||||
|
||||
try:
|
||||
# Success
|
||||
listener = self.transport.create_listener(conn_handler)
|
||||
self.listeners[str(maddr)] = listener
|
||||
# FIXME: Hack
|
||||
await listener.listen(maddr, self.manager._task_nursery)
|
||||
# TODO: `listener.listen` is not bounded with nursery. If we want to be
|
||||
# I/O agnostic, we should change the API.
|
||||
await listener.listen(maddr, self.manager._task_nursery) # type: ignore
|
||||
|
||||
# Call notifiers since event occurred
|
||||
await self.notify_listen(maddr)
|
||||
@ -278,6 +279,7 @@ class Swarm(INetwork, Service):
|
||||
"""
|
||||
self.notifees.append(notifee)
|
||||
|
||||
# TODO: Use `run_task`.
|
||||
async def notify_opened_stream(self, stream: INetStream) -> None:
|
||||
async with trio.open_nursery() as nursery:
|
||||
for notifee in self.notifees:
|
||||
|
||||
Reference in New Issue
Block a user