38 releases (breaking)

0.27.0 Aug 9, 2024
0.26.3 Jun 4, 2024
0.26.2 May 1, 2024
0.26.1 Dec 9, 2023
0.1.1 Jul 13, 2020

#1923 in Network programming

Download history 51694/week @ 2024-08-16 60424/week @ 2024-08-23 65963/week @ 2024-08-30 65235/week @ 2024-09-06 58397/week @ 2024-09-13 65625/week @ 2024-09-20 60757/week @ 2024-09-27 69081/week @ 2024-10-04 63840/week @ 2024-10-11 65273/week @ 2024-10-18 59571/week @ 2024-10-25 64210/week @ 2024-11-01 53764/week @ 2024-11-08 53159/week @ 2024-11-15 54200/week @ 2024-11-22 42314/week @ 2024-11-29

214,642 downloads per month
Used in 218 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

~8–15MB
~185K SLoC