77 lines
1.9 KiB
Rust
77 lines
1.9 KiB
Rust
// 版权所有 (c) ling 保留所有权利。
|
||
// 除非另行说明,否则仅允许在LingTransmit中使用此文件中的代码。
|
||
//
|
||
// 由 ling 创建于 2025/1/18.
|
||
#![allow(non_snake_case)]
|
||
|
||
use async_trait::async_trait;
|
||
use tokio::io;
|
||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||
use tokio::net::{tcp, unix, TcpListener, UnixListener};
|
||
|
||
/// 读取抽象
|
||
#[async_trait]
|
||
pub trait OwnedReadHalfAbstraction: AsyncRead + Unpin + Send + Sync {}
|
||
|
||
/// 写入抽象
|
||
#[async_trait]
|
||
pub trait OwnedWriteHalfAbstraction: AsyncWrite + Unpin + Send + Sync {}
|
||
|
||
#[async_trait]
|
||
impl OwnedReadHalfAbstraction for tcp::OwnedReadHalf {}
|
||
|
||
#[async_trait]
|
||
impl OwnedReadHalfAbstraction for unix::OwnedReadHalf {}
|
||
|
||
#[async_trait]
|
||
impl OwnedWriteHalfAbstraction for tcp::OwnedWriteHalf {}
|
||
|
||
#[async_trait]
|
||
impl OwnedWriteHalfAbstraction for unix::OwnedWriteHalf {}
|
||
|
||
#[async_trait]
|
||
pub trait AcceptSocket {
|
||
async fn accept(
|
||
&self,
|
||
) -> io::Result<(
|
||
Box<dyn OwnedReadHalfAbstraction>,
|
||
Box<dyn OwnedWriteHalfAbstraction>,
|
||
SocketAddr,
|
||
)>;
|
||
}
|
||
|
||
pub enum SocketAddr {
|
||
TCP(std::net::SocketAddr),
|
||
Unix(unix::SocketAddr),
|
||
}
|
||
|
||
#[async_trait]
|
||
impl AcceptSocket for TcpListener {
|
||
async fn accept(
|
||
&self,
|
||
) -> io::Result<(
|
||
Box<dyn OwnedReadHalfAbstraction>,
|
||
Box<dyn OwnedWriteHalfAbstraction>,
|
||
SocketAddr,
|
||
)> {
|
||
let (socket, addr) = self.accept().await?;
|
||
let (read, write) = socket.into_split();
|
||
Ok((Box::new(read), Box::new(write), SocketAddr::TCP(addr)))
|
||
}
|
||
}
|
||
|
||
#[async_trait]
|
||
impl AcceptSocket for UnixListener {
|
||
async fn accept(
|
||
&self,
|
||
) -> io::Result<(
|
||
Box<dyn OwnedReadHalfAbstraction>,
|
||
Box<dyn OwnedWriteHalfAbstraction>,
|
||
SocketAddr,
|
||
)> {
|
||
let (socket, addr) = self.accept().await?;
|
||
let (read, write) = socket.into_split();
|
||
Ok((Box::new(read), Box::new(write), SocketAddr::Unix(addr)))
|
||
}
|
||
}
|