-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
net: add buffer size methods to UdpSocket #4363
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -253,6 +253,78 @@ impl UdpSocket { | |||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Sets the size of the UDP send buffer on this socket. | ||||||||||||||
/// | ||||||||||||||
/// On most operating systems, this sets the `SO_SNDBUF` socket option. | ||||||||||||||
pub fn set_send_buffer_size(&self, size: u32) -> io::Result<()> { | ||||||||||||||
self.to_socket().set_send_buffer_size(size as usize) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Returns the size of the UDP send buffer for this socket. | ||||||||||||||
/// | ||||||||||||||
/// On most operating systems, this is the value of the `SO_SNDBUF` socket | ||||||||||||||
/// option. | ||||||||||||||
/// | ||||||||||||||
/// Note that if [`set_send_buffer_size`] has been called on this socket | ||||||||||||||
/// previously, the value returned by this function may not be the same as | ||||||||||||||
/// the argument provided to `set_send_buffer_size`. This is for the | ||||||||||||||
/// following reasons: | ||||||||||||||
/// | ||||||||||||||
/// * Most operating systems have minimum and maximum allowed sizes for the | ||||||||||||||
/// send buffer, and will clamp the provided value if it is below the | ||||||||||||||
/// minimum or above the maximum. The minimum and maximum buffer sizes are | ||||||||||||||
/// OS-dependent. | ||||||||||||||
/// * Linux will double the buffer size to account for internal bookkeeping | ||||||||||||||
/// data, and returns the doubled value from `getsockopt(2)`. As per `man | ||||||||||||||
/// 7 socket`: | ||||||||||||||
/// > Sets or gets the maximum socket send buffer in bytes. The | ||||||||||||||
/// > kernel doubles this value (to allow space for bookkeeping | ||||||||||||||
/// > overhead) when it is set using `setsockopt(2)`, and this doubled | ||||||||||||||
/// > value is returned by `getsockopt(2)`. | ||||||||||||||
/// | ||||||||||||||
/// [`set_send_buffer_size`]: Self::set_send_buffer_size | ||||||||||||||
pub fn send_buffer_size(&self) -> io::Result<u32> { | ||||||||||||||
self.to_socket().send_buffer_size().map(|n| n as u32) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Sets the size of the UDP receive buffer on this socket. | ||||||||||||||
/// | ||||||||||||||
/// On most operating systems, this sets the `SO_RCVBUF` socket option. | ||||||||||||||
pub fn set_recv_buffer_size(&self, size: u32) -> io::Result<()> { | ||||||||||||||
self.to_socket().set_recv_buffer_size(size as usize) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Returns the size of the UDP receive buffer for this socket. | ||||||||||||||
/// | ||||||||||||||
/// On most operating systems, this is the value of the `SO_RCVBUF` socket | ||||||||||||||
/// option. | ||||||||||||||
/// | ||||||||||||||
/// Note that if [`set_recv_buffer_size`] has been called on this socket | ||||||||||||||
/// previously, the value returned by this function may not be the same as | ||||||||||||||
/// the argument provided to `set_send_buffer_size`. This is for the | ||||||||||||||
/// following reasons: | ||||||||||||||
/// | ||||||||||||||
/// * Most operating systems have minimum and maximum allowed sizes for the | ||||||||||||||
/// receive buffer, and will clamp the provided value if it is below the | ||||||||||||||
/// minimum or above the maximum. The minimum and maximum buffer sizes are | ||||||||||||||
/// OS-dependent. | ||||||||||||||
/// * Linux will double the buffer size to account for internal bookkeeping | ||||||||||||||
/// data, and returns the doubled value from `getsockopt(2)`. As per `man | ||||||||||||||
/// 7 socket`: | ||||||||||||||
/// > Sets or gets the maximum socket send buffer in bytes. The | ||||||||||||||
/// > kernel doubles this value (to allow space for bookkeeping | ||||||||||||||
/// > overhead) when it is set using `setsockopt(2)`, and this doubled | ||||||||||||||
/// > value is returned by `getsockopt(2)`. | ||||||||||||||
/// | ||||||||||||||
/// [`set_recv_buffer_size`]: Self::set_recv_buffer_size | ||||||||||||||
pub fn recv_buffer_size(&self) -> io::Result<u32> { | ||||||||||||||
self.to_socket().recv_buffer_size().map(|n| n as u32) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
fn to_socket(&self) -> socket2::SockRef<'_> { | ||||||||||||||
socket2::SockRef::from(self) | ||||||||||||||
} | ||||||||||||||
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. According to the API guidelines, this should be called
Suggested change
|
||||||||||||||
|
||||||||||||||
/// Returns the local address that this socket is bound to. | ||||||||||||||
/// | ||||||||||||||
/// # Example | ||||||||||||||
|
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.
Why the casts?
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.
This PR (tokio::net::UdpSocket) uses u32 for consistency with TcpSocket's methods, so we need casts here.
And as I said in #4270 (comment), these casts are fine.
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.
Thanks.