Add peek{,from} methods to UdpSocket

pull/853/head
Matthieu Le brazidec (r3v2d0g) 4 years ago
parent dbc98faf1f
commit 47ce9a370c
No known key found for this signature in database
GPG Key ID: 33FAA936C128C029

@ -206,6 +206,29 @@ impl UdpSocket {
self.watcher.recv_from(buf).await self.watcher.recv_from(buf).await
} }
/// Receives data from socket without removing it from the queue.
///
/// On success, returns the number of bytes peeked and the origin.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::net::UdpSocket;
///
/// let socket = UdpSocket::bind("127.0.0.1:0").await?;
///
/// let mut buf = vec![0; 1024];
/// let (n, peer) = socket.peek_from(&mut buf).await?;
/// println!("Peeked {} bytes from {}", n, peer);
/// #
/// # Ok (()) }) }
/// ```
pub async fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
self.watcher.peek_from(buf).await
}
/// Connects the UDP socket to a remote address. /// Connects the UDP socket to a remote address.
/// ///
/// When connected, methods [`send`] and [`recv`] will use the specified address for sending /// When connected, methods [`send`] and [`recv`] will use the specified address for sending
@ -301,6 +324,30 @@ impl UdpSocket {
self.watcher.recv(buf).await self.watcher.recv(buf).await
} }
/// Receives data from the socket without removing it from the queue.
///
/// On success, returns the number of bytes peeked.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::net::UdpSocket;
///
/// let socket = UdpSocket::bind("127.0.0.1:0").await?;
/// socket.connect("127.0.0.1:8080").await?;
///
/// let mut buf = vec![0; 1024];
/// let n = socket.peek(&mut buf).await?;
/// println!("Peeked {} bytes", n);
/// #
/// # Ok(()) }) }
/// ```
pub async fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
self.watcher.peek(buf).await
}
/// Gets the value of the `SO_BROADCAST` option for this socket. /// Gets the value of the `SO_BROADCAST` option for this socket.
/// ///
/// For more information about this option, see [`set_broadcast`]. /// For more information about this option, see [`set_broadcast`].

@ -12,7 +12,7 @@ const THE_MERCHANT_OF_VENICE: &[u8] = b"
"; ";
#[test] #[test]
fn send_recv() -> io::Result<()> { fn send_recv_peek() -> io::Result<()> {
task::block_on(async { task::block_on(async {
let socket1 = UdpSocket::bind("127.0.0.1:0").await?; let socket1 = UdpSocket::bind("127.0.0.1:0").await?;
let socket2 = UdpSocket::bind("127.0.0.1:0").await?; let socket2 = UdpSocket::bind("127.0.0.1:0").await?;
@ -23,6 +23,9 @@ fn send_recv() -> io::Result<()> {
socket1.send(THE_MERCHANT_OF_VENICE).await?; socket1.send(THE_MERCHANT_OF_VENICE).await?;
let mut buf = [0u8; 1024]; let mut buf = [0u8; 1024];
let n = socket2.peek(&mut buf).await?;
assert_eq!(&buf[..n], THE_MERCHANT_OF_VENICE);
let n = socket2.recv(&mut buf).await?; let n = socket2.recv(&mut buf).await?;
assert_eq!(&buf[..n], THE_MERCHANT_OF_VENICE); assert_eq!(&buf[..n], THE_MERCHANT_OF_VENICE);

Loading…
Cancel
Save