Skip to content

Commit

Permalink
refactor(net): unify TracerSocket trait
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Dec 31, 2022
1 parent e9ce47b commit 38e37f3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
16 changes: 9 additions & 7 deletions src/tracing/net/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ impl Socket {
inner: socket2::Socket::new(Domain::IPV6, Type::RAW, Some(protocol))?,
})
}

fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
self.inner.set_nonblocking(nonblocking)
}

fn local_addr(&self) -> io::Result<Option<SocketAddr>> {
Ok(self.inner.local_addr()?.as_socket())
}
}

impl TracerSocket for Socket {
Expand Down Expand Up @@ -223,9 +231,6 @@ impl TracerSocket for Socket {
fn set_header_included(&self, included: bool) -> io::Result<()> {
self.inner.set_header_included(included)
}
fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
self.inner.set_nonblocking(nonblocking)
}
fn set_unicast_hops_v6(&self, hops: u8) -> io::Result<()> {
self.inner.set_unicast_hops_v6(u32::from(hops))
}
Expand Down Expand Up @@ -259,7 +264,7 @@ impl TracerSocket for Socket {
)?;
Ok(writable == 1)
}
fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, Option<SocketAddr>)> {
fn recv_from(&mut self, buf: &mut [u8]) -> io::Result<(usize, Option<SocketAddr>)> {
self.inner.recv_from_into_buf(buf)
}
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
Expand All @@ -268,9 +273,6 @@ impl TracerSocket for Socket {
fn shutdown(&self, how: Shutdown) -> io::Result<()> {
self.inner.shutdown(how)
}
fn local_addr(&self) -> io::Result<Option<SocketAddr>> {
Ok(self.inner.local_addr()?.as_socket())
}
fn peer_addr(&self) -> io::Result<Option<SocketAddr>> {
Ok(self.inner.peer_addr()?.as_socket())
}
Expand Down
14 changes: 1 addition & 13 deletions src/tracing/net/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,6 @@ impl TracerSocket for Socket {
unimplemented!()
}

/// TODO
#[allow(dead_code)]
fn set_nonblocking(&self, _nonblocking: bool) -> io::Result<()> {
unimplemented!()
}

/// TODO
fn set_unicast_hops_v6(&self, _hops: u8) -> io::Result<()> {
unimplemented!()
Expand All @@ -163,7 +157,7 @@ impl TracerSocket for Socket {
}

/// TODO
fn recv_from(&self, _buf: &mut [u8]) -> io::Result<(usize, Option<SocketAddr>)> {
fn recv_from(&mut self, _buf: &mut [u8]) -> io::Result<(usize, Option<SocketAddr>)> {
unimplemented!()
}

Expand All @@ -176,12 +170,6 @@ impl TracerSocket for Socket {
unimplemented!()
}

/// TODO
#[allow(dead_code, clippy::unused_self)]
fn local_addr(&self) -> io::Result<Option<SocketAddr>> {
unimplemented!()
}

/// TODO
fn peer_addr(&self) -> io::Result<Option<SocketAddr>> {
unimplemented!()
Expand Down
4 changes: 1 addition & 3 deletions src/tracing/net/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@ where
fn set_ttl(&self, ttl: u32) -> Result<()>;
fn set_reuse_port(&self, reuse: bool) -> Result<()>;
fn set_header_included(&self, included: bool) -> Result<()>;
fn set_nonblocking(&self, nonblocking: bool) -> Result<()>;
fn set_unicast_hops_v6(&self, hops: u8) -> Result<()>;
fn connect(&self, address: SocketAddr) -> Result<()>;
fn send_to(&self, buf: &[u8], addr: SocketAddr) -> Result<usize>;
/// Returns true if the socket becomes readable before the timeout, false otherwise.
fn is_readable(&self, timeout: Duration) -> Result<bool>;
/// Returns true if the socket is currently writeable, false otherwise.
fn is_writable(&self) -> Result<bool>;
fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, Option<SocketAddr>)>;
fn recv_from(&mut self, buf: &mut [u8]) -> Result<(usize, Option<SocketAddr>)>;
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
fn shutdown(&self, how: Shutdown) -> Result<()>;
fn local_addr(&self) -> Result<Option<SocketAddr>>;
fn peer_addr(&self) -> Result<Option<SocketAddr>>;
fn take_error(&self) -> Result<Option<Error>>;
fn icmp_error_info(&self) -> Result<IpAddr>;
Expand Down

0 comments on commit 38e37f3

Please sign in to comment.