@@ -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