31 releases (breaking)

0.25.1 Aug 11, 2023
0.25.0 Jun 20, 2023
0.24.1 May 1, 2023
0.24.0 Feb 24, 2023
0.1.1 Jul 13, 2020

#1021 in Network programming

Download history 24166/week @ 2023-06-06 29144/week @ 2023-06-13 31631/week @ 2023-06-20 25089/week @ 2023-06-27 26583/week @ 2023-07-04 28721/week @ 2023-07-11 28927/week @ 2023-07-18 32395/week @ 2023-07-25 26819/week @ 2023-08-01 28482/week @ 2023-08-08 33013/week @ 2023-08-15 32569/week @ 2023-08-22 49951/week @ 2023-08-29 45487/week @ 2023-09-05 44389/week @ 2023-09-12 26678/week @ 2023-09-19

175,626 downloads per month
Used in 180 crates (7 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

~6–14MB
~193K SLoC