Generic (stream) protocols
This module provides a generic NetworkBehaviour
for stream-oriented protocols.
Streams are the fundamental primitive of libp2p and all other protocols are implemented using streams.
In contrast to other NetworkBehaviour
s, this module takes a different design approach.
All interaction happens through a [Control
] that can be obtained via [Behaviour::new_control
].
[Control
]s can be cloned and thus shared across your application.
Inbound
To accept streams for a particular StreamProtocol
using this module, use [Control::accept
]:
Example
#
Resource management
[Control::accept
] returns you an instance of [IncomingStreams
].
This struct implements Stream
and like other streams, is lazy.
You must continuously poll it to make progress.
In the example above, this taken care of by using the StreamExt::next
helper.
Internally, we will drop streams if your application falls behind in processing these incoming streams, i.e. if whatever loop calls .next()
is not fast enough.
Drop
As soon as you drop [IncomingStreams
], the protocol will be de-registered.
Any further attempt by remote peers to open a stream using the provided protocol will result in a negotiation error.
Outbound
To open a new outbound stream for a particular protocol, use [Control::open_stream
].
Example
#