karyon_net/
connection.rs

1use std::result::Result;
2
3use async_trait::async_trait;
4
5use crate::Endpoint;
6
7/// Alias for `Box<dyn Connection>`
8pub type Conn<C, E> = Box<dyn Connection<Message = C, Error = E>>;
9
10/// A trait for objects which can be converted to [`Conn`].
11pub trait ToConn {
12    type Message;
13    type Error;
14    fn to_conn(self) -> Conn<Self::Message, Self::Error>;
15}
16
17/// Connection is a generic network connection interface for
18/// [`udp::UdpConn`], [`tcp::TcpConn`], [`tls::TlsConn`], [`ws::WsConn`],
19/// and [`unix::UnixConn`].
20///
21/// If you are familiar with the Go language, this is similar to the
22/// [Conn](https://pkg.go.dev/net#Conn) interface
23#[async_trait]
24pub trait Connection: Send + Sync {
25    type Message;
26    type Error;
27    /// Returns the remote peer endpoint of this connection
28    fn peer_endpoint(&self) -> Result<Endpoint, Self::Error>;
29
30    /// Returns the local socket endpoint of this connection
31    fn local_endpoint(&self) -> Result<Endpoint, Self::Error>;
32
33    /// Recvs data from this connection.  
34    async fn recv(&self) -> Result<Self::Message, Self::Error>;
35
36    /// Sends data to this connection
37    async fn send(&self, msg: Self::Message) -> Result<(), Self::Error>;
38}