use std::{error::Error, fmt};
mod any_storage;
mod cfg;
mod client;
mod credit;
mod listener;
mod msg;
mod mux;
mod port_allocator;
mod receiver;
mod sender;
pub use any_storage::{AnyBox, AnyEntry, AnyStorage};
pub use cfg::{Cfg, PortsExhausted};
pub use client::{Client, Connect, ConnectError};
pub use listener::{Listener, ListenerError, ListenerStream, Request};
pub use mux::ChMux;
pub use port_allocator::{PortAllocator, PortNumber};
pub use receiver::{DataBuf, Received, Receiver, ReceiverStream, RecvAnyError, RecvChunkError, RecvError};
pub use sender::{ChunkSender, Closed, SendError, Sender, SenderSink, TrySendError};
pub const PROTOCOL_VERSION: u8 = 2;
#[derive(Debug, Clone)]
pub enum ChMuxError<SinkError, StreamError> {
SinkError(SinkError),
StreamError(StreamError),
StreamClosed,
Reset,
Timeout,
Protocol(String),
}
impl<SinkError, StreamError> fmt::Display for ChMuxError<SinkError, StreamError>
where
SinkError: fmt::Display,
StreamError: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::SinkError(err) => write!(f, "send error: {}", err),
Self::StreamError(err) => write!(f, "receive error: {}", err),
Self::StreamClosed => write!(f, "end of receive stream"),
Self::Reset => write!(f, "connection reset"),
Self::Timeout => write!(f, "connection timeout"),
Self::Protocol(err) => write!(f, "protocol error: {}", err),
}
}
}
impl<SinkError, StreamError> Error for ChMuxError<SinkError, StreamError>
where
SinkError: Error,
StreamError: Error,
{
}
impl From<ChMuxError<std::io::Error, std::io::Error>> for std::io::Error {
fn from(err: ChMuxError<std::io::Error, std::io::Error>) -> Self {
use std::io::ErrorKind;
match err {
ChMuxError::SinkError(err) => err,
ChMuxError::StreamError(err) => err,
ChMuxError::StreamClosed => std::io::Error::new(ErrorKind::ConnectionReset, err.to_string()),
ChMuxError::Reset => std::io::Error::new(ErrorKind::ConnectionReset, err.to_string()),
ChMuxError::Timeout => std::io::Error::new(ErrorKind::TimedOut, err.to_string()),
ChMuxError::Protocol(_) => std::io::Error::new(ErrorKind::InvalidData, err.to_string()),
}
}
}