Skip to content

Commit

Permalink
feat(shadowsocks): impls As*Fd As*Socket for ProxySocket
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Sep 22, 2024
1 parent 0ce6cb3 commit 8c95ada
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions crates/shadowsocks/src/relay/udprelay/proxy_socket.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! UDP socket for communicating with shadowsocks' proxy server
#[cfg(unix)]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, AsSocket, BorrowedSocket, IntoRawSocket, RawSocket};
use std::{
io::{self, ErrorKind},
net::SocketAddr,
Expand Down Expand Up @@ -30,9 +34,6 @@ use super::{
},
};

#[cfg(unix)]
use std::os::fd::{AsRawFd, RawFd};

static DEFAULT_CONNECT_OPTS: Lazy<ConnectOpts> = Lazy::new(Default::default);
static DEFAULT_SOCKET_CONTROL: Lazy<UdpSocketControlData> = Lazy::new(UdpSocketControlData::default);

Expand Down Expand Up @@ -626,3 +627,53 @@ where
self.io.as_raw_fd()
}
}

#[cfg(unix)]
impl<S> AsFd for ProxySocket<S>
where
S: AsFd,
{
fn as_fd(&self) -> BorrowedFd<'_> {
self.io.as_fd()
}
}

#[cfg(unix)]
impl<S> IntoRawFd for ProxySocket<S>
where
S: IntoRawFd,
{
fn into_raw_fd(self) -> RawFd {
self.io.into_raw_fd()
}
}

#[cfg(windows)]
impl<S> AsRawSocket for ProxySocket<S>
where
S: AsRawSocket,
{
fn as_raw_socket(&self) -> RawSocket {
self.io.as_raw_socket()
}
}

#[cfg(windows)]
impl<S> AsSocket for ProxySocket<S>
where
S: AsSocket,
{
fn as_socket(&self) -> BorrowedSocket<'_> {
self.io.as_socket()
}
}

#[cfg(windows)]
impl<S> IntoRawSocket for ProxySocket<S>
where
S: IntoRawSocket,
{
fn into_raw_socket(self) -> RawSocket {
self.io.into_raw_socket()
}
}

0 comments on commit 8c95ada

Please sign in to comment.