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
41
42
43
44
45
use once_cell::sync::OnceCell;
use smol::prelude::*;
use smol::Async;
use smol::Executor;
use socket2::{Domain, Socket, Type};
use std::{
convert::TryInto,
net::{SocketAddr, UdpSocket},
};
static USER_EXEC: OnceCell<&'static Executor> = OnceCell::new();
pub(crate) fn spawn<T: Send + 'static>(
future: impl Future<Output = T> + Send + 'static,
) -> smol::Task<T> {
if let Some(ex) = USER_EXEC.get() {
ex.spawn(future)
} else {
smolscale::spawn(future)
}
}
pub(crate) fn new_udp_socket_bind(addr: SocketAddr) -> std::io::Result<Async<UdpSocket>> {
let socket = Socket::new(
match addr {
SocketAddr::V4(_) => Domain::ipv4(),
SocketAddr::V6(_) => Domain::ipv6(),
},
Type::dgram(),
None,
)
.unwrap();
drop(socket.set_only_v6(false));
socket.set_recv_buffer_size(10 * 1024 * 1024)?;
socket.set_send_buffer_size(10 * 1024 * 1024)?;
socket.bind(&addr.into())?;
Ok(socket.into_udp_socket().try_into().unwrap())
}