nibkeep_sync/transport.rs
1//! What carries the messages between two replicas.
2
3/// How long a message may be: 16 MiB.
4pub const MAX_MESSAGE_LEN: usize = 16 * 1024 * 1024;
5
6/// A connection between two replicas, which carries whole messages in order, and is
7/// encrypted and bound to the two endpoints' keys ([Sync](../../../docs/spec/sync.md#1-connections)).
8pub trait Transport {
9 /// Sends a message.
10 ///
11 /// # Errors
12 ///
13 /// Returns an error if the connection fails.
14 fn send(&mut self, message: &[u8]) -> Result<(), TransportError>;
15
16 /// Waits for the next message.
17 ///
18 /// # Errors
19 ///
20 /// Returns an error if the connection fails or closes, or the message is longer than
21 /// [`MAX_MESSAGE_LEN`].
22 fn receive(&mut self) -> Result<Vec<u8>, TransportError>;
23
24 /// Whether [`Transport::receive`] would return at once: the next message has arrived, or
25 /// the connection failed. A transport that cannot tell says no, which costs only speed:
26 /// a replica saves what it received before it waits ([`Holder::pause`](crate::Holder::pause)).
27 fn has_message(&mut self) -> bool {
28 false
29 }
30
31 /// The 32 bytes exported from the connection's encryption, the same on both sides of
32 /// this connection and on no other.
33 fn binding(&self) -> [u8; 32];
34
35 /// This side's endpoint ID.
36 fn own_endpoint(&self) -> [u8; 32];
37
38 /// The other side's endpoint ID, as the connection proved it.
39 fn peer_endpoint(&self) -> [u8; 32];
40}
41
42/// Why a connection failed.
43#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
44#[error("{0}")]
45pub struct TransportError(pub String);