-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
take arbitrary IO for udp proxy (#1641)
* udp relay * refact * reset * Update crates/shadowsocks/src/relay/udprelay/proxy_socket.rs * export trait
- Loading branch information
Showing
5 changed files
with
154 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use async_trait::async_trait; | ||
use std::{ | ||
io::Result, | ||
net::SocketAddr, | ||
ops::Deref, | ||
task::{Context, Poll}, | ||
}; | ||
use tokio::io::ReadBuf; | ||
|
||
use crate::net::UdpSocket; | ||
|
||
/// a trait for datagram transport that wraps around a tokio `UdpSocket` | ||
#[async_trait] | ||
pub trait DatagramTransport: Send + Sync + std::fmt::Debug { | ||
async fn recv(&self, buf: &mut [u8]) -> Result<usize>; | ||
async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>; | ||
|
||
async fn send(&self, buf: &[u8]) -> Result<usize>; | ||
async fn send_to(&self, buf: &[u8], target: SocketAddr) -> Result<usize>; | ||
|
||
fn poll_recv(&self, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<Result<()>>; | ||
fn poll_recv_from(&self, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<Result<SocketAddr>>; | ||
fn poll_recv_ready(&self, cx: &mut Context<'_>) -> Poll<Result<()>>; | ||
|
||
fn poll_send(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>; | ||
fn poll_send_to(&self, cx: &mut Context<'_>, buf: &[u8], target: SocketAddr) -> Poll<Result<usize>>; | ||
fn poll_send_ready(&self, cx: &mut Context<'_>) -> Poll<Result<()>>; | ||
|
||
fn local_addr(&self) -> Result<SocketAddr>; | ||
|
||
#[cfg(unix)] | ||
fn as_raw_fd(&self) -> std::os::fd::RawFd; | ||
} | ||
|
||
#[async_trait] | ||
impl DatagramTransport for UdpSocket { | ||
async fn recv(&self, buf: &mut [u8]) -> Result<usize> { | ||
UdpSocket::recv(self, buf).await | ||
} | ||
|
||
async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)> { | ||
UdpSocket::recv_from(self, buf).await | ||
} | ||
|
||
async fn send(&self, buf: &[u8]) -> Result<usize> { | ||
UdpSocket::send(self, buf).await | ||
} | ||
|
||
async fn send_to(&self, buf: &[u8], target: SocketAddr) -> Result<usize> { | ||
UdpSocket::send_to(self, buf, target).await | ||
} | ||
|
||
fn poll_recv(&self, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<Result<()>> { | ||
UdpSocket::poll_recv(self, cx, buf) | ||
} | ||
|
||
fn poll_recv_from(&self, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<Result<SocketAddr>> { | ||
UdpSocket::poll_recv_from(self, cx, buf) | ||
} | ||
|
||
fn poll_recv_ready(&self, cx: &mut Context<'_>) -> Poll<Result<()>> { | ||
self.deref().poll_recv_ready(cx) | ||
} | ||
|
||
fn poll_send(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> { | ||
UdpSocket::poll_send(self, cx, buf) | ||
} | ||
|
||
fn poll_send_to(&self, cx: &mut Context<'_>, buf: &[u8], target: SocketAddr) -> Poll<Result<usize>> { | ||
UdpSocket::poll_send_to(self, cx, buf, target) | ||
} | ||
|
||
fn poll_send_ready(&self, cx: &mut Context<'_>) -> Poll<Result<()>> { | ||
self.deref().poll_send_ready(cx) | ||
} | ||
|
||
fn local_addr(&self) -> Result<SocketAddr> { | ||
self.deref().local_addr() | ||
} | ||
|
||
#[cfg(unix)] | ||
fn as_raw_fd(&self) -> std::os::fd::RawFd { | ||
use std::ops::Deref; | ||
use std::os::fd::AsRawFd; | ||
|
||
self.deref().as_raw_fd() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters