-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add peek APIs to std::net #38983
Merged
Merged
Add peek APIs to std::net #38983
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule liblibc
updated
14 files
+2 −2 | Cargo.lock | |
+1 −1 | Cargo.toml | |
+4 −0 | src/redox.rs | |
+18 −0 | src/unix/bsd/apple/mod.rs | |
+1 −0 | src/unix/bsd/freebsdlike/dragonfly/mod.rs | |
+16 −0 | src/unix/bsd/freebsdlike/freebsd/mod.rs | |
+4 −0 | src/unix/bsd/mod.rs | |
+1 −0 | src/unix/bsd/netbsdlike/mod.rs | |
+10 −0 | src/unix/bsd/netbsdlike/netbsd/mod.rs | |
+16 −0 | src/unix/haiku/mod.rs | |
+1 −0 | src/unix/notbsd/android/mod.rs | |
+1 −0 | src/unix/notbsd/linux/mod.rs | |
+7 −0 | src/unix/notbsd/mod.rs | |
+20 −0 | src/unix/solaris/mod.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,30 @@ impl UdpSocket { | |
self.0.recv_from(buf) | ||
} | ||
|
||
/// Receives data from the socket, without removing it from the queue. | ||
/// | ||
/// Successive calls return the same data. This is accomplished by passing | ||
/// `MSG_PEEK` as a flag to the underlying `recvfrom` system call. | ||
/// | ||
/// On success, returns the number of bytes peeked and the address from | ||
/// whence the data came. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(peek)] | ||
/// use std::net::UdpSocket; | ||
/// | ||
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address"); | ||
/// let mut buf = [0; 10]; | ||
/// let (number_of_bytes, src_addr) = socket.peek_from(&mut buf) | ||
/// .expect("Didn't receive data"); | ||
/// ``` | ||
#[unstable(feature = "peek", issue = "38980")] | ||
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { | ||
self.0.peek_from(buf) | ||
} | ||
|
||
/// Sends data on the socket to the given address. On success, returns the | ||
/// number of bytes written. | ||
/// | ||
|
@@ -579,6 +603,37 @@ impl UdpSocket { | |
self.0.recv(buf) | ||
} | ||
|
||
/// Receives data on the socket from the remote adress to which it is | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docs should include info about the return value. |
||
/// connected, without removing that data from the queue. On success, | ||
/// returns the number of bytes peeked. | ||
/// | ||
/// Successive calls return the same data. This is accomplished by passing | ||
/// `MSG_PEEK` as a flag to the underlying `recv` system call. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This method will fail if the socket is not connected. The `connect` method | ||
/// will connect this socket to a remote address. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(peek)] | ||
/// use std::net::UdpSocket; | ||
/// | ||
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address"); | ||
/// socket.connect("127.0.0.1:8080").expect("connect function failed"); | ||
/// let mut buf = [0; 10]; | ||
/// match socket.peek(&mut buf) { | ||
/// Ok(received) => println!("received {} bytes", received), | ||
/// Err(e) => println!("peek function failed: {:?}", e), | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "peek", issue = "38980")] | ||
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> { | ||
self.0.peek(buf) | ||
} | ||
|
||
/// Moves this UDP socket into or out of nonblocking mode. | ||
/// | ||
/// On Unix this corresponds to calling fcntl, and on Windows this | ||
|
@@ -869,6 +924,48 @@ mod tests { | |
assert_eq!(b"hello world", &buf[..]); | ||
} | ||
|
||
#[test] | ||
fn connect_send_peek_recv() { | ||
each_ip(&mut |addr, _| { | ||
let socket = t!(UdpSocket::bind(&addr)); | ||
t!(socket.connect(addr)); | ||
|
||
t!(socket.send(b"hello world")); | ||
|
||
for _ in 1..3 { | ||
let mut buf = [0; 11]; | ||
let size = t!(socket.peek(&mut buf)); | ||
assert_eq!(b"hello world", &buf[..]); | ||
assert_eq!(size, 11); | ||
} | ||
|
||
let mut buf = [0; 11]; | ||
let size = t!(socket.recv(&mut buf)); | ||
assert_eq!(b"hello world", &buf[..]); | ||
assert_eq!(size, 11); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn peek_from() { | ||
each_ip(&mut |addr, _| { | ||
let socket = t!(UdpSocket::bind(&addr)); | ||
t!(socket.send_to(b"hello world", &addr)); | ||
|
||
for _ in 1..3 { | ||
let mut buf = [0; 11]; | ||
let (size, _) = t!(socket.peek_from(&mut buf)); | ||
assert_eq!(b"hello world", &buf[..]); | ||
assert_eq!(size, 11); | ||
} | ||
|
||
let mut buf = [0; 11]; | ||
let (size, _) = t!(socket.recv_from(&mut buf)); | ||
assert_eq!(b"hello world", &buf[..]); | ||
assert_eq!(size, 11); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn ttl() { | ||
let ttl = 100; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be good to document the return value as well.