1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::net::SocketAddr;

use crate::Context;
pub use crate::{Address, Error, Result};
pub use async_trait::async_trait;
pub use futures_io::{AsyncRead, AsyncWrite};
pub use futures_util::future::RemoteHandle;
pub use std::sync::Arc;

/// A TcpListener.
#[async_trait]
pub trait ITcpListener: Unpin + Send + Sync {
    async fn accept(&self) -> Result<(TcpStream, SocketAddr)>;
    async fn local_addr(&self) -> Result<SocketAddr>;
}
pub type TcpListener = Box<dyn ITcpListener>;

/// A TcpStream.
#[async_trait]
pub trait ITcpStream: AsyncRead + AsyncWrite + Unpin + Send + Sync {
    async fn peer_addr(&self) -> Result<SocketAddr>;
    async fn local_addr(&self) -> Result<SocketAddr>;
}
pub type TcpStream = Box<dyn ITcpStream>;

/// A UdpSocket.
#[async_trait]
pub trait IUdpSocket: Unpin + Send + Sync {
    async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>;
    async fn send_to(&self, buf: &[u8], addr: SocketAddr) -> Result<usize>;
    async fn local_addr(&self) -> Result<SocketAddr>;
}
pub type UdpSocket = Box<dyn IUdpSocket>;

/// A Net.
#[async_trait]
pub trait INet: Unpin + Send + Sync {
    async fn tcp_connect(&self, ctx: &mut Context, addr: Address) -> Result<TcpStream>;
    async fn tcp_bind(&self, ctx: &mut Context, addr: Address) -> Result<TcpListener>;
    async fn udp_bind(&self, ctx: &mut Context, addr: Address) -> Result<UdpSocket>;
}
pub type Net = Arc<dyn INet>;

/// A Server.
#[async_trait]
pub trait IServer: Unpin + Send + Sync {
    async fn start(&self) -> Result<()>;
    async fn stop(&self) -> Result<()>;
}
pub type Server = Box<dyn IServer>;