35 releases (breaking)

0.26.1 Dec 9, 2023
0.26.0 Nov 5, 2023
0.25.3 Oct 26, 2023
0.25.0 Jun 20, 2023
0.1.1 Jul 13, 2020

#2018 in Asynchronous

Download history 43152/week @ 2023-12-06 43221/week @ 2023-12-13 29664/week @ 2023-12-20 18938/week @ 2023-12-27 35650/week @ 2024-01-03 40261/week @ 2024-01-10 46630/week @ 2024-01-17 45640/week @ 2024-01-24 40316/week @ 2024-01-31 48840/week @ 2024-02-07 52904/week @ 2024-02-14 49747/week @ 2024-02-21 49518/week @ 2024-02-28 46556/week @ 2024-03-06 44949/week @ 2024-03-13 45261/week @ 2024-03-20

195,935 downloads per month
Used in 179 crates (6 directly)

MIT license

1MB
16K SLoC

Generic request/response protocols.

General Usage

The Behaviour struct is a NetworkBehaviour that implements a generic request/response protocol or protocol family, whereby each request is sent over a new substream on a connection. Behaviour is generic over the actual messages being sent, which are defined in terms of a Codec. Creating a request/response protocol thus amounts to providing an implementation of this trait which can then be given to Behaviour::with_codec. Further configuration options are available via the Config.

Requests are sent using Behaviour::send_request and the responses received as Message::Response via Event::Message.

Responses are sent using Behaviour::send_response upon receiving a Message::Request via Event::Message.

Predefined codecs

In case your message types implement serde::Serialize and serde::Deserialize, you can use two predefined behaviours:

Protocol Families

A single Behaviour instance can be used with an entire protocol family that share the same request and response types. For that purpose, Codec::Protocol is typically instantiated with a sum type.

Limited Protocol Support

It is possible to only support inbound or outbound requests for a particular protocol. This is achieved by instantiating Behaviour with protocols using ProtocolSupport::Inbound or ProtocolSupport::Outbound. Any subset of protocols of a protocol family can be configured in this way. Such protocols will not be advertised during inbound respectively outbound protocol negotiation on the substreams.

Dependencies

~7–15MB
~199K SLoC