pub mod connect;
pub mod encrypted;
pub mod unencrypted;
#[cfg(feature = "zmq")]
pub mod zeromq;
use std::io::ErrorKind;
#[cfg(feature = "zmq")]
pub use zeromq::{ZmqConnectionType, ZmqSocketType};
use crate::session::HandshakeError;
pub const MAX_FRAME_SIZE: usize =
FRAME_PREFIX_SIZE + MAX_FRAME_PAYLOAD_SIZE + FRAME_SUFFIX_SIZE;
pub const FRAME_PREFIX_SIZE: usize = 2 + 16;
pub const FRAME_SUFFIX_SIZE: usize = 16;
pub const MAX_FRAME_PAYLOAD_SIZE: usize = 0xFFFF;
#[derive(Clone, PartialEq, Eq, Hash, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum Error {
#[from]
SocketIo(std::io::ErrorKind),
#[cfg(feature = "zmq")]
Zmq(zeromq::Error),
ServiceOffline,
RequiresLocalSocket,
OversizedFrame(usize),
FrameTooSmall(usize),
FrameBroken(&'static str),
InvalidLength { expected: u16, actual: u16 },
NoNoiseHeader,
TorNotSupportedYet,
TimedOut,
#[from]
Handshake(HandshakeError),
KeygenFeatureRequired(&'static str),
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
match err.kind() {
ErrorKind::WouldBlock | ErrorKind::TimedOut => Error::TimedOut,
kind => Error::SocketIo(kind),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct RoutedFrame {
pub hop: Vec<u8>,
pub src: Vec<u8>,
pub dst: Vec<u8>,
pub msg: Vec<u8>,
}
pub trait DuplexConnection {
fn as_receiver(&mut self) -> &mut dyn RecvFrame;
fn as_sender(&mut self) -> &mut dyn SendFrame;
fn split(self) -> (Box<dyn RecvFrame + Send>, Box<dyn SendFrame + Send>);
}
pub trait RecvFrame {
fn recv_frame(&mut self) -> Result<Vec<u8>, Error>;
fn recv_raw(&mut self, len: usize) -> Result<Vec<u8>, Error>;
fn recv_routed(&mut self) -> Result<RoutedFrame, Error> {
panic!("Multipeer sockets are not possible with the chosen transport")
}
}
pub trait SendFrame {
fn send_frame(&mut self, frame: &[u8]) -> Result<usize, Error>;
fn send_raw(&mut self, raw_frame: &[u8]) -> Result<usize, Error>;
#[allow(dead_code)]
fn send_routed(
&mut self,
_source: &[u8],
_route: &[u8],
_address: &[u8],
_data: &[u8],
) -> Result<usize, Error> {
panic!("Multipeer sockets are not possible with the chosen transport")
}
}
#[cfg(feature = "async")]
#[async_trait]
pub trait AsyncRecvFrame {
async fn async_recv_frame(&mut self) -> Result<Vec<u8>, Error>;
async fn async_recv_raw(&mut self, len: usize) -> Result<Vec<u8>, Error>;
async fn async_recv_from(&mut self) -> Result<(Vec<u8>, Vec<u8>), Error> {
panic!("Multipeer sockets are not possible with the chosen transport")
}
}
#[cfg(feature = "async")]
#[async_trait]
pub trait AsyncSendFrame {
async fn async_send_frame(&mut self, frame: &[u8]) -> Result<usize, Error>;
async fn async_send_raw(
&mut self,
raw_frame: &[u8],
) -> Result<usize, Error>;
async fn async_send_to(
&mut self,
remote_id: &[u8],
frame: &[u8],
) -> Result<usize, Error> {
panic!("Multipeer sockets are not possible with the chosen transport")
}
}