chore: refacto use dedicated trait

This commit is contained in:
Σrebe - Romain GERARD 2024-07-28 22:11:34 +02:00
parent ef016f0467
commit 5e74ed233d
No known key found for this signature in database
GPG key ID: 7A42B4B97E0332F4
11 changed files with 408 additions and 242 deletions

View file

@ -6,6 +6,6 @@ pub use server::connect;
#[cfg(target_os = "linux")]
pub use server::mk_send_socket_tproxy;
pub use server::run_server;
pub use server::MyUdpSocket;
pub use server::UdpStream;
pub use server::UdpStreamWriter;
pub use server::WsUdpSocket;

View file

@ -295,17 +295,17 @@ pub async fn run_server(
}
#[derive(Clone)]
pub struct MyUdpSocket {
pub struct WsUdpSocket {
socket: Arc<UdpSocket>,
}
impl MyUdpSocket {
impl WsUdpSocket {
pub fn new(socket: Arc<UdpSocket>) -> Self {
Self { socket }
}
}
impl AsyncRead for MyUdpSocket {
impl AsyncRead for WsUdpSocket {
fn poll_read(self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<io::Result<()>> {
unsafe { self.map_unchecked_mut(|x| &mut x.socket) }
.poll_recv_from(cx, buf)
@ -313,7 +313,7 @@ impl AsyncRead for MyUdpSocket {
}
}
impl AsyncWrite for MyUdpSocket {
impl AsyncWrite for WsUdpSocket {
fn poll_write(self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &[u8]) -> Poll<Result<usize, Error>> {
unsafe { self.map_unchecked_mut(|x| &mut x.socket) }.poll_send(cx, buf)
}
@ -333,7 +333,7 @@ pub async fn connect(
connect_timeout: Duration,
so_mark: Option<u32>,
dns_resolver: &DnsResolver,
) -> anyhow::Result<MyUdpSocket> {
) -> anyhow::Result<WsUdpSocket> {
info!("Opening UDP connection to {}:{}", host, port);
let socket_addrs: Vec<SocketAddr> = match host {
@ -419,7 +419,7 @@ pub async fn connect(
}
if let Some(cnx) = cnx {
Ok(MyUdpSocket::new(Arc::new(cnx)))
Ok(WsUdpSocket::new(Arc::new(cnx)))
} else {
Err(anyhow!("Cannot connect to udp peer {}:{} reason {:?}", host, port, last_err))
}