Skip to content

Commit 2f688de

Browse files
Thomasdezeeuwcarllerche
authored andcommitted
Add UdpSocket::peek(_from) methods (#1052)
1 parent 211223e commit 2f688de

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

Diff for: src/net/udp.rs

+36
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,36 @@ impl UdpSocket {
248248
self.sys.recv_from(buf)
249249
}
250250

251+
/// Receives data from the socket, without removing it from the input queue.
252+
/// On success, returns the number of bytes read and the address from whence
253+
/// the data came.
254+
/// Receives data from the socket. On success, returns the number of bytes
255+
/// read and the address from whence the data came.
256+
///
257+
/// # Examples
258+
///
259+
/// ```no_run
260+
/// # use std::error::Error;
261+
/// #
262+
/// # fn main() -> Result<(), Box<dyn Error>> {
263+
/// use mio::net::UdpSocket;
264+
///
265+
/// let socket = UdpSocket::bind("127.0.0.1:0".parse()?)?;
266+
///
267+
/// // We must check if the socket is readable before calling recv_from,
268+
/// // or we could run into a WouldBlock error.
269+
///
270+
/// let mut buf = [0; 9];
271+
/// let (num_recv, from_addr) = socket.peek_from(&mut buf)?;
272+
/// println!("Received {:?} -> {:?} bytes from {:?}", buf, num_recv, from_addr);
273+
/// #
274+
/// # Ok(())
275+
/// # }
276+
/// ```
277+
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
278+
self.sys.peek_from(buf)
279+
}
280+
251281
/// Sends data on the socket to the address previously bound via connect(). On success,
252282
/// returns the number of bytes written.
253283
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
@@ -260,6 +290,12 @@ impl UdpSocket {
260290
self.sys.recv(buf)
261291
}
262292

293+
/// Receives data from the socket, without removing it from the input queue.
294+
/// On success, returns the number of bytes read.
295+
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
296+
self.sys.peek(buf)
297+
}
298+
263299
/// Connects the UDP socket setting the default destination for `send()`
264300
/// and limiting packets that are read via `recv` from the address specified
265301
/// in `addr`.

Diff for: src/sys/unix/udp.rs

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ impl UdpSocket {
3333
self.io.recv_from(buf)
3434
}
3535

36+
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
37+
self.io.peek_from(buf)
38+
}
39+
3640
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
3741
self.io.send(buf)
3842
}
@@ -41,6 +45,10 @@ impl UdpSocket {
4145
self.io.recv(buf)
4246
}
4347

48+
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
49+
self.io.peek(buf)
50+
}
51+
4452
pub fn connect(&self, addr: SocketAddr) -> io::Result<()> {
4553
self.io.connect(addr)
4654
}

Diff for: src/sys/windows/udp.rs

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ impl UdpSocket {
7979
self.io.recv_from(buf)
8080
}
8181

82+
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
83+
self.io.peek_from(buf)
84+
}
85+
8286
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
8387
wouldblock!(self, send, buf)
8488
}
@@ -87,6 +91,10 @@ impl UdpSocket {
8791
wouldblock!(self, recv, buf)
8892
}
8993

94+
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
95+
wouldblock!(self, peek, buf)
96+
}
97+
9098
pub fn connect(&self, addr: SocketAddr) -> io::Result<()> {
9199
wouldblock!(self, connect, addr)
92100
}

0 commit comments

Comments
 (0)