commonware_stream/
lib.rs

1//! Exchange messages over arbitrary transport.
2
3pub mod public_key;
4pub mod utils;
5
6use bytes::Bytes;
7use commonware_codec::Error as CodecError;
8use commonware_runtime::Error as RuntimeError;
9use std::future::Future;
10use thiserror::Error;
11
12/// Errors that can occur when interacting with a stream.
13#[derive(Error, Debug)]
14pub enum Error {
15    #[error("unable to decode: {0}")]
16    UnableToDecode(CodecError),
17    #[error("handshake not for us")]
18    HandshakeNotForUs,
19    #[error("handshake timeout")]
20    HandshakeTimeout,
21    #[error("invalid signature")]
22    InvalidSignature,
23    #[error("wrong peer")]
24    WrongPeer,
25    #[error("recv failed")]
26    RecvFailed(RuntimeError),
27    #[error("recv too large: {0} bytes")]
28    RecvTooLarge(usize),
29    #[error("send failed")]
30    SendFailed(RuntimeError),
31    #[error("send zero size")]
32    SendZeroSize,
33    #[error("send too large: {0} bytes")]
34    SendTooLarge(usize),
35    #[error("connection closed")]
36    StreamClosed,
37    #[error("cipher creation failed")]
38    CipherCreationFailed,
39    #[error("nonce overflow")]
40    NonceOverflow,
41    #[error("encryption failed")]
42    EncryptionFailed,
43    #[error("decryption failed")]
44    DecryptionFailed,
45    #[error("timestamp too old: {0}")]
46    InvalidTimestampOld(u64),
47    #[error("timestamp too future: {0}")]
48    InvalidTimestampFuture(u64),
49}
50
51/// A trait for sending messages.
52pub trait Sender: Sync + Send + 'static {
53    /// Send a message to the stream.
54    fn send(&mut self, msg: &[u8]) -> impl Future<Output = Result<(), Error>> + Send;
55}
56
57/// A trait for receiving messages.
58pub trait Receiver: Sync + Send + 'static {
59    /// Receive a message from the stream.
60    fn receive(&mut self) -> impl Future<Output = Result<Bytes, Error>> + Send;
61}