pub(crate) mod grpc;
#[allow(dead_code)]
pub(crate) mod inmem;
use std::sync::Arc;
use crate::transport_protocol::Envelope;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Endpoint(pub(crate) String);
impl Endpoint {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Clone, Debug)]
pub(crate) enum TransportError {
ConnectFailed(String),
BindFailed(String),
Closed,
Full,
}
impl std::fmt::Display for TransportError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ConnectFailed(detail) => write!(formatter, "connect failed: {detail}"),
Self::BindFailed(detail) => write!(formatter, "bind failed: {detail}"),
Self::Closed => formatter.write_str("stream closed"),
Self::Full => formatter.write_str("send buffer full"),
}
}
}
impl std::error::Error for TransportError {}
pub(crate) trait TransportSender: Send + Sync {
fn try_send(&self, envelope: Envelope) -> Result<(), TransportError>;
fn close(&self);
}
#[async_trait::async_trait]
pub(crate) trait TransportStream: Send {
fn sender(&self) -> Arc<dyn TransportSender>;
async fn recv(&mut self) -> Option<Envelope>;
}
#[async_trait::async_trait]
pub(crate) trait TransportListener: Send {
async fn accept(&mut self) -> Option<Box<dyn TransportStream>>;
fn shutdown(&self);
}
pub(crate) trait ServerTransport: Send + Sync {
fn listen(
&self,
advertised: &Endpoint,
listener: Option<tokio::net::TcpListener>,
) -> Result<Box<dyn TransportListener>, TransportError>;
}
#[async_trait::async_trait]
pub(crate) trait ClientTransport: Send + Sync {
async fn connect(
&self,
endpoint: &Endpoint,
) -> Result<Box<dyn TransportStream>, TransportError>;
}