Skip to content

Commit 49a9dc6

Browse files
authored
net: add buffer size methods to UdpSocket (#4363)
This adds the following methods: - UdpSocket::set_send_buffer_size - UdpSocket::send_buffer_size - UdpSocket::set_recv_buffer_size - UdpSocket::recv_buffer_size
1 parent 96370ba commit 49a9dc6

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

tokio/src/net/tcp/stream.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ impl TcpStream {
10901090
/// # }
10911091
/// ```
10921092
pub fn linger(&self) -> io::Result<Option<Duration>> {
1093-
let socket = self.to_socket();
1093+
let socket = self.as_socket();
10941094
socket.linger()
10951095
}
10961096

@@ -1116,11 +1116,11 @@ impl TcpStream {
11161116
/// # }
11171117
/// ```
11181118
pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
1119-
let socket = self.to_socket();
1119+
let socket = self.as_socket();
11201120
socket.set_linger(dur)
11211121
}
11221122

1123-
fn to_socket(&self) -> socket2::SockRef<'_> {
1123+
fn as_socket(&self) -> socket2::SockRef<'_> {
11241124
socket2::SockRef::from(self)
11251125
}
11261126

tokio/src/net/udp.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,78 @@ impl UdpSocket {
253253
}
254254
}
255255

256+
/// Sets the size of the UDP send buffer on this socket.
257+
///
258+
/// On most operating systems, this sets the `SO_SNDBUF` socket option.
259+
pub fn set_send_buffer_size(&self, size: u32) -> io::Result<()> {
260+
self.as_socket().set_send_buffer_size(size as usize)
261+
}
262+
263+
/// Returns the size of the UDP send buffer for this socket.
264+
///
265+
/// On most operating systems, this is the value of the `SO_SNDBUF` socket
266+
/// option.
267+
///
268+
/// Note that if [`set_send_buffer_size`] has been called on this socket
269+
/// previously, the value returned by this function may not be the same as
270+
/// the argument provided to `set_send_buffer_size`. This is for the
271+
/// following reasons:
272+
///
273+
/// * Most operating systems have minimum and maximum allowed sizes for the
274+
/// send buffer, and will clamp the provided value if it is below the
275+
/// minimum or above the maximum. The minimum and maximum buffer sizes are
276+
/// OS-dependent.
277+
/// * Linux will double the buffer size to account for internal bookkeeping
278+
/// data, and returns the doubled value from `getsockopt(2)`. As per `man
279+
/// 7 socket`:
280+
/// > Sets or gets the maximum socket send buffer in bytes. The
281+
/// > kernel doubles this value (to allow space for bookkeeping
282+
/// > overhead) when it is set using `setsockopt(2)`, and this doubled
283+
/// > value is returned by `getsockopt(2)`.
284+
///
285+
/// [`set_send_buffer_size`]: Self::set_send_buffer_size
286+
pub fn send_buffer_size(&self) -> io::Result<u32> {
287+
self.as_socket().send_buffer_size().map(|n| n as u32)
288+
}
289+
290+
/// Sets the size of the UDP receive buffer on this socket.
291+
///
292+
/// On most operating systems, this sets the `SO_RCVBUF` socket option.
293+
pub fn set_recv_buffer_size(&self, size: u32) -> io::Result<()> {
294+
self.as_socket().set_recv_buffer_size(size as usize)
295+
}
296+
297+
/// Returns the size of the UDP receive buffer for this socket.
298+
///
299+
/// On most operating systems, this is the value of the `SO_RCVBUF` socket
300+
/// option.
301+
///
302+
/// Note that if [`set_recv_buffer_size`] has been called on this socket
303+
/// previously, the value returned by this function may not be the same as
304+
/// the argument provided to `set_send_buffer_size`. This is for the
305+
/// following reasons:
306+
///
307+
/// * Most operating systems have minimum and maximum allowed sizes for the
308+
/// receive buffer, and will clamp the provided value if it is below the
309+
/// minimum or above the maximum. The minimum and maximum buffer sizes are
310+
/// OS-dependent.
311+
/// * Linux will double the buffer size to account for internal bookkeeping
312+
/// data, and returns the doubled value from `getsockopt(2)`. As per `man
313+
/// 7 socket`:
314+
/// > Sets or gets the maximum socket send buffer in bytes. The
315+
/// > kernel doubles this value (to allow space for bookkeeping
316+
/// > overhead) when it is set using `setsockopt(2)`, and this doubled
317+
/// > value is returned by `getsockopt(2)`.
318+
///
319+
/// [`set_recv_buffer_size`]: Self::set_recv_buffer_size
320+
pub fn recv_buffer_size(&self) -> io::Result<u32> {
321+
self.as_socket().recv_buffer_size().map(|n| n as u32)
322+
}
323+
324+
fn as_socket(&self) -> socket2::SockRef<'_> {
325+
socket2::SockRef::from(self)
326+
}
327+
256328
/// Returns the local address that this socket is bound to.
257329
///
258330
/// # Example

0 commit comments

Comments
 (0)