Skip to content

feat: Adding stream state to the Network Stream #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
263 changes: 0 additions & 263 deletions examples/doc-examples/example_net_stream.py

This file was deleted.

13 changes: 12 additions & 1 deletion libp2p/network/connection/swarm_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from libp2p.network.stream.net_stream import (
NetStream,
StreamState,
)
from libp2p.stream_muxer.exceptions import (
MuxedConnUnavailable,
Expand Down Expand Up @@ -129,7 +130,13 @@ async def _handle_muxed_stream(self, muxed_stream: IMuxedStream) -> None:
self.remove_stream(net_stream)

async def _add_stream(self, muxed_stream: IMuxedStream) -> NetStream:
net_stream = NetStream(muxed_stream)
#
net_stream = NetStream(muxed_stream, self)
# Set Stream state to OPEN if the event has already started.
# This is to ensure that the new streams created after connection has started
# are immediately set to OPEN state.
if self.event_started.is_set():
net_stream.set_state(StreamState.OPEN)
self.streams.add(net_stream)
await self.swarm.notify_opened_stream(net_stream)
return net_stream
Expand All @@ -138,6 +145,10 @@ async def _notify_disconnected(self) -> None:
await self.swarm.notify_disconnected(self)

async def start(self) -> None:
streams_open = self.get_streams()
for stream in streams_open:
"""Set the state of the stream to OPEN."""
stream.set_state(StreamState.OPEN)
await self._handle_new_streams()

async def new_stream(self) -> NetStream:
Expand Down
Loading
Loading