tokio_util/udp/
frame.rs

1use crate::codec::{Decoder, Encoder};
2
3use futures_core::Stream;
4use tokio::{io::ReadBuf, net::UdpSocket};
5
6use bytes::{BufMut, BytesMut};
7use futures_sink::Sink;
8use std::io;
9use std::pin::Pin;
10use std::task::{ready, Context, Poll};
11use std::{
12    borrow::Borrow,
13    net::{Ipv4Addr, SocketAddr, SocketAddrV4},
14};
15
16/// A unified [`Stream`] and [`Sink`] interface to an underlying `UdpSocket`, using
17/// the `Encoder` and `Decoder` traits to encode and decode frames.
18///
19/// Raw UDP sockets work with datagrams, but higher-level code usually wants to
20/// batch these into meaningful chunks, called "frames". This method layers
21/// framing on top of this socket by using the `Encoder` and `Decoder` traits to
22/// handle encoding and decoding of messages frames. Note that the incoming and
23/// outgoing frame types may be distinct.
24///
25/// This function returns a *single* object that is both [`Stream`] and [`Sink`];
26/// grouping this into a single object is often useful for layering things which
27/// require both read and write access to the underlying object.
28///
29/// If you want to work more directly with the streams and sink, consider
30/// calling [`split`] on the `UdpFramed` returned by this method, which will break
31/// them into separate objects, allowing them to interact more easily.
32///
33/// [`Stream`]: futures_core::Stream
34/// [`Sink`]: futures_sink::Sink
35/// [`split`]: https://docs.rs/futures/0.3/futures/stream/trait.StreamExt.html#method.split
36#[must_use = "sinks do nothing unless polled"]
37#[derive(Debug)]
38pub struct UdpFramed<C, T = UdpSocket> {
39    socket: T,
40    codec: C,
41    rd: BytesMut,
42    wr: BytesMut,
43    out_addr: SocketAddr,
44    flushed: bool,
45    is_readable: bool,
46    current_addr: Option<SocketAddr>,
47}
48
49const INITIAL_RD_CAPACITY: usize = 64 * 1024;
50const INITIAL_WR_CAPACITY: usize = 8 * 1024;
51
52impl<C, T> Unpin for UdpFramed<C, T> {}
53
54impl<C, T> Stream for UdpFramed<C, T>
55where
56    T: Borrow<UdpSocket>,
57    C: Decoder,
58{
59    type Item = Result<(C::Item, SocketAddr), C::Error>;
60
61    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
62        let pin = self.get_mut();
63
64        pin.rd.reserve(INITIAL_RD_CAPACITY);
65
66        loop {
67            // Are there still bytes left in the read buffer to decode?
68            if pin.is_readable {
69                if let Some(frame) = pin.codec.decode_eof(&mut pin.rd)? {
70                    let current_addr = pin
71                        .current_addr
72                        .expect("will always be set before this line is called");
73
74                    return Poll::Ready(Some(Ok((frame, current_addr))));
75                }
76
77                // if this line has been reached then decode has returned `None`.
78                pin.is_readable = false;
79                pin.rd.clear();
80            }
81
82            // We're out of data. Try and fetch more data to decode
83            let addr = {
84                // Safety: `chunk_mut()` returns a `&mut UninitSlice`, and `UninitSlice` is a
85                // transparent wrapper around `[MaybeUninit<u8>]`.
86                let buf = unsafe { pin.rd.chunk_mut().as_uninit_slice_mut() };
87                let mut read = ReadBuf::uninit(buf);
88                let ptr = read.filled().as_ptr();
89                let res = ready!(pin.socket.borrow().poll_recv_from(cx, &mut read));
90
91                assert_eq!(ptr, read.filled().as_ptr());
92                let addr = res?;
93
94                let filled = read.filled().len();
95                // Safety: This is guaranteed to be the number of initialized (and read) bytes due
96                // to the invariants provided by `ReadBuf::filled`.
97                unsafe { pin.rd.advance_mut(filled) };
98
99                addr
100            };
101
102            pin.current_addr = Some(addr);
103            pin.is_readable = true;
104        }
105    }
106}
107
108impl<I, C, T> Sink<(I, SocketAddr)> for UdpFramed<C, T>
109where
110    T: Borrow<UdpSocket>,
111    C: Encoder<I>,
112{
113    type Error = C::Error;
114
115    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
116        if !self.flushed {
117            match self.poll_flush(cx)? {
118                Poll::Ready(()) => {}
119                Poll::Pending => return Poll::Pending,
120            }
121        }
122
123        Poll::Ready(Ok(()))
124    }
125
126    fn start_send(self: Pin<&mut Self>, item: (I, SocketAddr)) -> Result<(), Self::Error> {
127        let (frame, out_addr) = item;
128
129        let pin = self.get_mut();
130
131        pin.codec.encode(frame, &mut pin.wr)?;
132        pin.out_addr = out_addr;
133        pin.flushed = false;
134
135        Ok(())
136    }
137
138    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
139        if self.flushed {
140            return Poll::Ready(Ok(()));
141        }
142
143        let Self {
144            ref socket,
145            ref mut out_addr,
146            ref mut wr,
147            ..
148        } = *self;
149
150        let n = ready!(socket.borrow().poll_send_to(cx, wr, *out_addr))?;
151
152        let wrote_all = n == self.wr.len();
153        self.wr.clear();
154        self.flushed = true;
155
156        let res = if wrote_all {
157            Ok(())
158        } else {
159            Err(io::Error::new(
160                io::ErrorKind::Other,
161                "failed to write entire datagram to socket",
162            )
163            .into())
164        };
165
166        Poll::Ready(res)
167    }
168
169    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
170        ready!(self.poll_flush(cx))?;
171        Poll::Ready(Ok(()))
172    }
173}
174
175impl<C, T> UdpFramed<C, T>
176where
177    T: Borrow<UdpSocket>,
178{
179    /// Create a new `UdpFramed` backed by the given socket and codec.
180    ///
181    /// See struct level documentation for more details.
182    pub fn new(socket: T, codec: C) -> UdpFramed<C, T> {
183        Self {
184            socket,
185            codec,
186            out_addr: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0)),
187            rd: BytesMut::with_capacity(INITIAL_RD_CAPACITY),
188            wr: BytesMut::with_capacity(INITIAL_WR_CAPACITY),
189            flushed: true,
190            is_readable: false,
191            current_addr: None,
192        }
193    }
194
195    /// Returns a reference to the underlying I/O stream wrapped by `Framed`.
196    ///
197    /// # Note
198    ///
199    /// Care should be taken to not tamper with the underlying stream of data
200    /// coming in as it may corrupt the stream of frames otherwise being worked
201    /// with.
202    pub fn get_ref(&self) -> &T {
203        &self.socket
204    }
205
206    /// Returns a mutable reference to the underlying I/O stream wrapped by `Framed`.
207    ///
208    /// # Note
209    ///
210    /// Care should be taken to not tamper with the underlying stream of data
211    /// coming in as it may corrupt the stream of frames otherwise being worked
212    /// with.
213    pub fn get_mut(&mut self) -> &mut T {
214        &mut self.socket
215    }
216
217    /// Returns a reference to the underlying codec wrapped by
218    /// `Framed`.
219    ///
220    /// Note that care should be taken to not tamper with the underlying codec
221    /// as it may corrupt the stream of frames otherwise being worked with.
222    pub fn codec(&self) -> &C {
223        &self.codec
224    }
225
226    /// Returns a mutable reference to the underlying codec wrapped by
227    /// `UdpFramed`.
228    ///
229    /// Note that care should be taken to not tamper with the underlying codec
230    /// as it may corrupt the stream of frames otherwise being worked with.
231    pub fn codec_mut(&mut self) -> &mut C {
232        &mut self.codec
233    }
234
235    /// Returns a reference to the read buffer.
236    pub fn read_buffer(&self) -> &BytesMut {
237        &self.rd
238    }
239
240    /// Returns a mutable reference to the read buffer.
241    pub fn read_buffer_mut(&mut self) -> &mut BytesMut {
242        &mut self.rd
243    }
244
245    /// Consumes the `Framed`, returning its underlying I/O stream.
246    pub fn into_inner(self) -> T {
247        self.socket
248    }
249}