use std::sync::Arc;
use std::time::Duration;
#[derive(Debug, thiserror::Error)]
pub enum TransportError {
#[error("connection failed: {0}")]
ConnectionFailed(String),
#[error("send failed: {0}")]
SendFailed(String),
#[error("receive failed: {0}")]
ReceiveFailed(String),
#[error("timeout")]
Timeout,
#[error("connection closed")]
ConnectionClosed,
#[error("payload too large: {size} bytes (max {max})")]
PayloadTooLarge { size: usize, max: usize },
#[error("io error: {0}")]
Io(#[from] std::io::Error),
}
pub trait Transport: Send + Sync {
fn send(&self, destination: &str, payload: &[u8]) -> Result<Vec<u8>, TransportError>;
fn connect(&self, destination: &str) -> Result<Box<dyn Connection>, TransportError>;
}
pub trait Connection: Send {
fn send(&mut self, payload: &[u8]) -> Result<(), TransportError>;
fn recv(&mut self, timeout: Option<Duration>) -> Result<Vec<u8>, TransportError>;
fn close(&mut self) -> Result<(), TransportError>;
fn supports_sidecars(&self) -> bool {
false
}
fn send_sidecar(&mut self, payload: &[u8]) -> Result<(), TransportError> {
self.send(payload)
}
fn recv_any(&mut self, timeout: Option<Duration>) -> Result<Vec<u8>, TransportError> {
self.recv(timeout)
}
}
impl<T: Transport + ?Sized> Transport for Arc<T> {
fn send(&self, destination: &str, payload: &[u8]) -> Result<Vec<u8>, TransportError> {
self.as_ref().send(destination, payload)
}
fn connect(&self, destination: &str) -> Result<Box<dyn Connection>, TransportError> {
self.as_ref().connect(destination)
}
}
pub mod factory;
pub mod framing;
pub mod memoized;
pub mod tcp;
#[cfg(feature = "quic")]
pub mod quic;