1use std::{
54 fmt,
55 future::Future,
56 io,
57 net::SocketAddr,
58 pin::Pin,
59 sync::Arc,
60 task::{ready, Context, Poll},
61 time::Instant,
62};
63
64use quinn::udp::{self, RecvMeta, Transmit};
65use quinn::{AsyncTimer, AsyncUdpSocket, Runtime, UdpPoller};
66use vibeio::io::{AsyncReadPoll, AsyncWritePoll};
67use vibeio::net::PollUdpSocket;
68
69#[derive(Debug, Clone, Copy)]
78pub struct VibeioRuntime;
79
80impl Runtime for VibeioRuntime {
81 #[inline]
82 fn new_timer(&self, i: Instant) -> Pin<Box<dyn AsyncTimer>> {
83 Box::pin(VibeioTimer {
84 inner: Box::pin(vibeio::time::sleep_until(i)),
85 })
86 }
87
88 #[inline]
89 fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
90 let future: Pin<Box<dyn Future<Output = ()>>> = unsafe { std::mem::transmute(future) };
91 vibeio::spawn(future);
92 }
93
94 #[inline]
95 fn wrap_udp_socket(&self, sock: std::net::UdpSocket) -> io::Result<Arc<dyn AsyncUdpSocket>> {
96 let io = sock.try_clone()?;
97 io.set_nonblocking(true)?;
98 let socket = PollUdpSocket::from_std(sock)?;
99 let state = udp::UdpSocketState::new((&io).into())?;
100 Ok(Arc::new(VibeioUdpSocket {
101 io: socket,
102 state,
103 socket: io,
104 }))
105 }
106
107 #[inline]
108 fn now(&self) -> Instant {
109 Instant::now()
110 }
111}
112
113struct VibeioTimer {
114 inner: Pin<Box<vibeio::time::Sleep>>,
115}
116
117impl fmt::Debug for VibeioTimer {
118 #[inline]
119 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120 f.debug_struct("VibeioTimer").finish_non_exhaustive()
121 }
122}
123
124unsafe impl Send for VibeioTimer {}
125unsafe impl Sync for VibeioTimer {}
126
127impl AsyncTimer for VibeioTimer {
128 #[inline]
129 fn reset(self: Pin<&mut Self>, i: Instant) {
130 self.get_mut().inner.as_mut().get_mut().reset(i);
131 }
132
133 #[inline]
134 fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> {
135 self.get_mut().inner.as_mut().poll(cx)
136 }
137}
138
139struct VibeioUdpSocket {
140 io: PollUdpSocket,
141 state: udp::UdpSocketState,
142 socket: std::net::UdpSocket,
143}
144
145impl fmt::Debug for VibeioUdpSocket {
146 #[inline]
147 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148 f.debug_struct("VibeioUdpSocket")
149 .field("local_addr", &self.local_addr())
150 .finish_non_exhaustive()
151 }
152}
153
154unsafe impl Send for VibeioUdpSocket {}
155unsafe impl Sync for VibeioUdpSocket {}
156
157impl AsyncUdpSocket for VibeioUdpSocket {
158 #[inline]
159 fn create_io_poller(self: Arc<Self>) -> Pin<Box<dyn UdpPoller>> {
160 Box::pin(VibeioUdpPoller { socket: self })
161 }
162
163 #[inline]
164 fn try_send(&self, transmit: &Transmit) -> io::Result<()> {
165 self.io
166 .try_io_writable(|| self.state.send((&self.socket).into(), transmit))
167 }
168
169 #[inline]
170 fn poll_recv(
171 &self,
172 cx: &mut Context,
173 bufs: &mut [io::IoSliceMut<'_>],
174 meta: &mut [RecvMeta],
175 ) -> Poll<io::Result<usize>> {
176 loop {
177 ready!(self.io.poll_readable(cx)?);
178 match self
179 .io
180 .try_io_readable(|| self.state.recv((&self.socket).into(), bufs, meta))
181 {
182 Ok(n) => return Poll::Ready(Ok(n)),
183 Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => continue,
184 Err(e) => return Poll::Ready(Err(e)),
185 }
186 }
187 }
188
189 #[inline]
190 fn local_addr(&self) -> io::Result<SocketAddr> {
191 self.io.local_addr()
192 }
193
194 #[inline]
195 fn max_transmit_segments(&self) -> usize {
196 self.state.max_gso_segments()
197 }
198
199 #[inline]
200 fn max_receive_segments(&self) -> usize {
201 self.state.gro_segments()
202 }
203
204 #[inline]
205 fn may_fragment(&self) -> bool {
206 self.state.may_fragment()
207 }
208}
209
210struct VibeioUdpPoller {
211 socket: Arc<VibeioUdpSocket>,
212}
213
214impl fmt::Debug for VibeioUdpPoller {
215 #[inline]
216 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217 f.debug_struct("VibeioUdpPoller").finish_non_exhaustive()
218 }
219}
220
221unsafe impl Send for VibeioUdpPoller {}
222unsafe impl Sync for VibeioUdpPoller {}
223
224impl UdpPoller for VibeioUdpPoller {
225 #[inline]
226 fn poll_writable(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
227 self.socket.io.poll_writable(cx)
228 }
229}