forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp.rs
More file actions
40 lines (34 loc) · 1.79 KB
/
Copy pathudp.rs
File metadata and controls
40 lines (34 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(unix)]
use tokio::net::UdpSocket;
#[cfg(unix)]
// This function will be obsolete after tokio/mio internally use `socket2` and expose the methods to
// apply options to a socket. Until then, use of `unsafe` is necessary here.
pub fn set_receive_buffer_size(socket: &UdpSocket, size: usize) {
// SAFETY: We create a socket from an existing file descriptor without destructing the previous
// owner and therefore temporarily have two objects that own the same socket.
//
// This is safe since we make sure that the new socket owner does not call its destructor by
// giving up its ownership at the end of this scope.
let socket = unsafe { socket2::Socket::from_raw_fd(socket.as_raw_fd()) };
if let Err(error) = socket.set_recv_buffer_size(size) {
warn!(message = "Failed configuring receive buffer size on UDP socket.", %error);
}
socket.into_raw_fd();
}
#[cfg(unix)]
// This function will be obsolete after tokio/mio internally use `socket2` and expose the methods to
// apply options to a socket. Until then, use of `unsafe` is necessary here.
pub fn set_send_buffer_size(socket: &UdpSocket, size: usize) {
// SAFETY: We create a socket from an existing file descriptor without destructing the previous
// owner and therefore temporarily have two objects that own the same socket.
//
// This is safe since we make sure that the new socket owner does not call its destructor by
// giving up its ownership at the end of this scope.
let socket = unsafe { socket2::Socket::from_raw_fd(socket.as_raw_fd()) };
if let Err(error) = socket.set_send_buffer_size(size) {
warn!(message = "Failed configuring send buffer size on UDP socket.", %error);
}
socket.into_raw_fd();
}