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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use async_std::{net::UdpSocket, task::spawn};
use futures::{future::BoxFuture, ready, Future, FutureExt, Stream, StreamExt};
use std::{
io,
net::SocketAddr,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use crate::GenTransport;
pub type Transport = GenTransport<Provider>;
pub struct Provider {
socket: Arc<UdpSocket>,
send_packet: Option<BoxFuture<'static, Result<(), io::Error>>>,
recv_stream: ReceiveStream,
}
impl super::Provider for Provider {
type IfWatcher = if_watch::smol::IfWatcher;
fn from_socket(socket: std::net::UdpSocket) -> io::Result<Self> {
let socket = Arc::new(socket.into());
let recv_stream = ReceiveStream::new(Arc::clone(&socket));
Ok(Provider {
socket,
send_packet: None,
recv_stream,
})
}
fn poll_recv_from(&mut self, cx: &mut Context<'_>) -> Poll<io::Result<(Vec<u8>, SocketAddr)>> {
match self.recv_stream.poll_next_unpin(cx) {
Poll::Ready(ready) => {
Poll::Ready(ready.expect("ReceiveStream::poll_next never returns None."))
}
Poll::Pending => Poll::Pending,
}
}
fn start_send(&mut self, data: Vec<u8>, addr: SocketAddr) {
let socket = self.socket.clone();
let send = async move {
socket.send_to(&data, addr).await?;
Ok(())
}
.boxed();
self.send_packet = Some(send)
}
fn poll_send_flush(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
let pending = match self.send_packet.as_mut() {
Some(pending) => pending,
None => return Poll::Ready(Ok(())),
};
match pending.poll_unpin(cx) {
Poll::Ready(result) => {
self.send_packet = None;
Poll::Ready(result)
}
Poll::Pending => Poll::Pending,
}
}
fn spawn(future: impl Future<Output = ()> + Send + 'static) {
spawn(future);
}
fn new_if_watcher() -> io::Result<Self::IfWatcher> {
if_watch::smol::IfWatcher::new()
}
fn poll_if_event(
watcher: &mut Self::IfWatcher,
cx: &mut Context<'_>,
) -> Poll<io::Result<if_watch::IfEvent>> {
watcher.poll_if_event(cx)
}
}
type ReceiveStreamItem = (
Result<(usize, SocketAddr), io::Error>,
Arc<UdpSocket>,
Vec<u8>,
);
struct ReceiveStream {
fut: BoxFuture<'static, ReceiveStreamItem>,
}
impl ReceiveStream {
fn new(socket: Arc<UdpSocket>) -> Self {
let fut = ReceiveStream::next(socket, vec![0; super::RECEIVE_BUFFER_SIZE]).boxed();
Self { fut: fut.boxed() }
}
async fn next(socket: Arc<UdpSocket>, mut socket_recv_buffer: Vec<u8>) -> ReceiveStreamItem {
let recv = socket.recv_from(&mut socket_recv_buffer).await;
(recv, socket, socket_recv_buffer)
}
}
impl Stream for ReceiveStream {
type Item = Result<(Vec<u8>, SocketAddr), io::Error>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let (result, socket, buffer) = ready!(self.fut.poll_unpin(cx));
let result = result.map(|(packet_len, packet_src)| {
debug_assert!(packet_len <= buffer.len());
(buffer[..packet_len].into(), packet_src)
});
self.fut = ReceiveStream::next(socket, buffer).boxed();
Poll::Ready(Some(result))
}
}