Skip to content
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 4 commits into from Dec 31, 2021
Merged

Conversation

taiki-e
Copy link
Member

@taiki-e taiki-e commented Dec 31, 2021

This adds:

  • UdpSocket::set_send_buffer_size
  • UdpSocket::send_buffer_size
  • UdpSocket::set_recv_buffer_size
  • UdpSocket::recv_buffer_size

Closes #3542

The first commit is from #4361.

Refs:

This adds:
- UdpSocket::set_send_buffer_size
- UdpSocket::send_buffer_size
- UdpSocket::set_recv_buffer_size
- UdpSocket::recv_buffer_size
@taiki-e taiki-e added A-tokio Area: The main tokio crate M-net Module: tokio/net labels Dec 31, 2021
Comment on lines 324 to 326
fn to_socket(&self) -> socket2::SockRef<'_> {
socket2::SockRef::from(self)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the API guidelines, this should be called as_socket. It's not an expensive conversion.

Suggested change
fn to_socket(&self) -> socket2::SockRef<'_> {
socket2::SockRef::from(self)
}
fn as_socket(&self) -> socket2::SockRef<'_> {
socket2::SockRef::from(self)
}

Comment on lines 259 to 261
pub fn set_send_buffer_size(&self, size: u32) -> io::Result<()> {
self.to_socket().set_send_buffer_size(size as usize)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the casts?

Copy link
Member Author

@taiki-e taiki-e Dec 31, 2021

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.

  • set_send_buffer_size,set_recv_buffer_size (cast u32 as usize): on tokio, usize is always at least 32 bits.
  • send_buffer_size,recv_buffer_size (cast usize as u32): socket2 gets these values as c_int (always i32) and then cast them to usize (1, 2).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@taiki-e taiki-e merged commit 49a9dc6 into master Dec 31, 2021
@taiki-e taiki-e deleted the taiki-e/net-udp-buffer-size branch December 31, 2021 11:47
taiki-e added a commit that referenced this pull request Jan 11, 2022
@FallingSnow
Copy link

This was removed in aea26b3. Any chance we could bring it back? Or is there another alternative available already?

@Darksonn
Copy link
Contributor

Open an issue about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-net Module: tokio/net
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UDPSocket lacks recv_buffer_size/set_recv_buffer_size/send_buffer_size/set_send_buffer_size
3 participants