• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![warn(rust_2018_idioms)]
2 #![cfg(not(target_os = "wasi"))] // Wasi doesn't support UDP
3 
4 use tokio::net::UdpSocket;
5 use tokio_stream::StreamExt;
6 use tokio_util::codec::{Decoder, Encoder, LinesCodec};
7 use tokio_util::udp::UdpFramed;
8 
9 use bytes::{BufMut, BytesMut};
10 use futures::future::try_join;
11 use futures::future::FutureExt;
12 use futures::sink::SinkExt;
13 use std::io;
14 use std::sync::Arc;
15 
16 #[cfg_attr(
17     any(target_os = "macos", target_os = "ios", target_os = "tvos"),
18     allow(unused_assignments)
19 )]
20 #[tokio::test]
send_framed_byte_codec() -> std::io::Result<()>21 async fn send_framed_byte_codec() -> std::io::Result<()> {
22     let mut a_soc = UdpSocket::bind("127.0.0.1:0").await?;
23     let mut b_soc = UdpSocket::bind("127.0.0.1:0").await?;
24 
25     let a_addr = a_soc.local_addr()?;
26     let b_addr = b_soc.local_addr()?;
27 
28     // test sending & receiving bytes
29     {
30         let mut a = UdpFramed::new(a_soc, ByteCodec);
31         let mut b = UdpFramed::new(b_soc, ByteCodec);
32 
33         let msg = b"4567";
34 
35         let send = a.send((msg, b_addr));
36         let recv = b.next().map(|e| e.unwrap());
37         let (_, received) = try_join(send, recv).await.unwrap();
38 
39         let (data, addr) = received;
40         assert_eq!(msg, &*data);
41         assert_eq!(a_addr, addr);
42 
43         a_soc = a.into_inner();
44         b_soc = b.into_inner();
45     }
46 
47     #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
48     // test sending & receiving an empty message
49     {
50         let mut a = UdpFramed::new(a_soc, ByteCodec);
51         let mut b = UdpFramed::new(b_soc, ByteCodec);
52 
53         let msg = b"";
54 
55         let send = a.send((msg, b_addr));
56         let recv = b.next().map(|e| e.unwrap());
57         let (_, received) = try_join(send, recv).await.unwrap();
58 
59         let (data, addr) = received;
60         assert_eq!(msg, &*data);
61         assert_eq!(a_addr, addr);
62     }
63 
64     Ok(())
65 }
66 
67 pub struct ByteCodec;
68 
69 impl Decoder for ByteCodec {
70     type Item = Vec<u8>;
71     type Error = io::Error;
72 
decode(&mut self, buf: &mut BytesMut) -> Result<Option<Vec<u8>>, io::Error>73     fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Vec<u8>>, io::Error> {
74         let len = buf.len();
75         Ok(Some(buf.split_to(len).to_vec()))
76     }
77 }
78 
79 impl Encoder<&[u8]> for ByteCodec {
80     type Error = io::Error;
81 
encode(&mut self, data: &[u8], buf: &mut BytesMut) -> Result<(), io::Error>82     fn encode(&mut self, data: &[u8], buf: &mut BytesMut) -> Result<(), io::Error> {
83         buf.reserve(data.len());
84         buf.put_slice(data);
85         Ok(())
86     }
87 }
88 
89 #[tokio::test]
send_framed_lines_codec() -> std::io::Result<()>90 async fn send_framed_lines_codec() -> std::io::Result<()> {
91     let a_soc = UdpSocket::bind("127.0.0.1:0").await?;
92     let b_soc = UdpSocket::bind("127.0.0.1:0").await?;
93 
94     let a_addr = a_soc.local_addr()?;
95     let b_addr = b_soc.local_addr()?;
96 
97     let mut a = UdpFramed::new(a_soc, ByteCodec);
98     let mut b = UdpFramed::new(b_soc, LinesCodec::new());
99 
100     let msg = b"1\r\n2\r\n3\r\n".to_vec();
101     a.send((&msg, b_addr)).await?;
102 
103     assert_eq!(b.next().await.unwrap().unwrap(), ("1".to_string(), a_addr));
104     assert_eq!(b.next().await.unwrap().unwrap(), ("2".to_string(), a_addr));
105     assert_eq!(b.next().await.unwrap().unwrap(), ("3".to_string(), a_addr));
106 
107     Ok(())
108 }
109 
110 #[tokio::test]
framed_half() -> std::io::Result<()>111 async fn framed_half() -> std::io::Result<()> {
112     let a_soc = Arc::new(UdpSocket::bind("127.0.0.1:0").await?);
113     let b_soc = a_soc.clone();
114 
115     let a_addr = a_soc.local_addr()?;
116     let b_addr = b_soc.local_addr()?;
117 
118     let mut a = UdpFramed::new(a_soc, ByteCodec);
119     let mut b = UdpFramed::new(b_soc, LinesCodec::new());
120 
121     let msg = b"1\r\n2\r\n3\r\n".to_vec();
122     a.send((&msg, b_addr)).await?;
123 
124     let msg = b"4\r\n5\r\n6\r\n".to_vec();
125     a.send((&msg, b_addr)).await?;
126 
127     assert_eq!(b.next().await.unwrap().unwrap(), ("1".to_string(), a_addr));
128     assert_eq!(b.next().await.unwrap().unwrap(), ("2".to_string(), a_addr));
129     assert_eq!(b.next().await.unwrap().unwrap(), ("3".to_string(), a_addr));
130 
131     assert_eq!(b.next().await.unwrap().unwrap(), ("4".to_string(), a_addr));
132     assert_eq!(b.next().await.unwrap().unwrap(), ("5".to_string(), a_addr));
133     assert_eq!(b.next().await.unwrap().unwrap(), ("6".to_string(), a_addr));
134 
135     Ok(())
136 }
137