Skip to content

Commit 1319def

Browse files
authored
Merge pull request #853 from r3v2d0g/udp-peek
Add peek{,from} methods to UdpSocket
2 parents 26f6198 + 47ce9a3 commit 1319def

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/net/udp/mod.rs

+47
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,29 @@ impl UdpSocket {
206206
self.watcher.recv_from(buf).await
207207
}
208208

209+
/// Receives data from socket without removing it from the queue.
210+
///
211+
/// On success, returns the number of bytes peeked and the origin.
212+
///
213+
/// # Examples
214+
///
215+
/// ```no_run
216+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
217+
/// #
218+
/// use async_std::net::UdpSocket;
219+
///
220+
/// let socket = UdpSocket::bind("127.0.0.1:0").await?;
221+
///
222+
/// let mut buf = vec![0; 1024];
223+
/// let (n, peer) = socket.peek_from(&mut buf).await?;
224+
/// println!("Peeked {} bytes from {}", n, peer);
225+
/// #
226+
/// # Ok (()) }) }
227+
/// ```
228+
pub async fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
229+
self.watcher.peek_from(buf).await
230+
}
231+
209232
/// Connects the UDP socket to a remote address.
210233
///
211234
/// When connected, methods [`send`] and [`recv`] will use the specified address for sending
@@ -301,6 +324,30 @@ impl UdpSocket {
301324
self.watcher.recv(buf).await
302325
}
303326

327+
/// Receives data from the socket without removing it from the queue.
328+
///
329+
/// On success, returns the number of bytes peeked.
330+
///
331+
/// # Examples
332+
///
333+
/// ```no_run
334+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
335+
/// #
336+
/// use async_std::net::UdpSocket;
337+
///
338+
/// let socket = UdpSocket::bind("127.0.0.1:0").await?;
339+
/// socket.connect("127.0.0.1:8080").await?;
340+
///
341+
/// let mut buf = vec![0; 1024];
342+
/// let n = socket.peek(&mut buf).await?;
343+
/// println!("Peeked {} bytes", n);
344+
/// #
345+
/// # Ok(()) }) }
346+
/// ```
347+
pub async fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
348+
self.watcher.peek(buf).await
349+
}
350+
304351
/// Gets the value of the `SO_BROADCAST` option for this socket.
305352
///
306353
/// For more information about this option, see [`set_broadcast`].

tests/udp.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const THE_MERCHANT_OF_VENICE: &[u8] = b"
1212
";
1313

1414
#[test]
15-
fn send_recv() -> io::Result<()> {
15+
fn send_recv_peek() -> io::Result<()> {
1616
task::block_on(async {
1717
let socket1 = UdpSocket::bind("127.0.0.1:0").await?;
1818
let socket2 = UdpSocket::bind("127.0.0.1:0").await?;
@@ -23,6 +23,9 @@ fn send_recv() -> io::Result<()> {
2323
socket1.send(THE_MERCHANT_OF_VENICE).await?;
2424

2525
let mut buf = [0u8; 1024];
26+
let n = socket2.peek(&mut buf).await?;
27+
assert_eq!(&buf[..n], THE_MERCHANT_OF_VENICE);
28+
2629
let n = socket2.recv(&mut buf).await?;
2730
assert_eq!(&buf[..n], THE_MERCHANT_OF_VENICE);
2831

0 commit comments

Comments
 (0)