Skip to content

Commit

Permalink
Add UdpSocket::peek(_from) methods (#1052)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomasdezeeuw authored and carllerche committed Aug 7, 2019
1 parent 211223e commit 2f688de
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,36 @@ impl UdpSocket {
self.sys.recv_from(buf)
}

/// Receives data from the socket, without removing it from the input queue.
/// On success, returns the number of bytes read and the address from whence
/// the data came.
/// Receives data from the socket. On success, returns the number of bytes
/// read and the address from whence the data came.
///
/// # Examples
///
/// ```no_run
/// # use std::error::Error;
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// use mio::net::UdpSocket;
///
/// let socket = UdpSocket::bind("127.0.0.1:0".parse()?)?;
///
/// // We must check if the socket is readable before calling recv_from,
/// // or we could run into a WouldBlock error.
///
/// let mut buf = [0; 9];
/// let (num_recv, from_addr) = socket.peek_from(&mut buf)?;
/// println!("Received {:?} -> {:?} bytes from {:?}", buf, num_recv, from_addr);
/// #
/// # Ok(())
/// # }
/// ```
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
self.sys.peek_from(buf)
}

/// Sends data on the socket to the address previously bound via connect(). On success,
/// returns the number of bytes written.
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
Expand All @@ -260,6 +290,12 @@ impl UdpSocket {
self.sys.recv(buf)
}

/// Receives data from the socket, without removing it from the input queue.
/// On success, returns the number of bytes read.
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
self.sys.peek(buf)
}

/// Connects the UDP socket setting the default destination for `send()`
/// and limiting packets that are read via `recv` from the address specified
/// in `addr`.
Expand Down
8 changes: 8 additions & 0 deletions src/sys/unix/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ impl UdpSocket {
self.io.recv_from(buf)
}

pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
self.io.peek_from(buf)
}

pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
self.io.send(buf)
}
Expand All @@ -41,6 +45,10 @@ impl UdpSocket {
self.io.recv(buf)
}

pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
self.io.peek(buf)
}

pub fn connect(&self, addr: SocketAddr) -> io::Result<()> {
self.io.connect(addr)
}
Expand Down
8 changes: 8 additions & 0 deletions src/sys/windows/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ impl UdpSocket {
self.io.recv_from(buf)
}

pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
self.io.peek_from(buf)
}

pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
wouldblock!(self, send, buf)
}
Expand All @@ -87,6 +91,10 @@ impl UdpSocket {
wouldblock!(self, recv, buf)
}

pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
wouldblock!(self, peek, buf)
}

pub fn connect(&self, addr: SocketAddr) -> io::Result<()> {
wouldblock!(self, connect, addr)
}
Expand Down

0 comments on commit 2f688de

Please sign in to comment.