-
Notifications
You must be signed in to change notification settings - Fork 18
/
udp.rs
149 lines (128 loc) · 3.49 KB
/
udp.rs
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
//! Factory traits for creating UDP sockets on embedded devices
use core::net::SocketAddr;
use embedded_io_async::ErrorType;
use crate::udp::{UdpReceive, UdpSend};
use crate::{MulticastV4, MulticastV6, Readable};
/// This trait is implemented by UDP sockets that can be split into separate `send` and `receive` halves that can operate
/// independently from each other (i.e., a full-duplex connection)
pub trait UdpSplit: ErrorType {
type Receive<'a>: UdpReceive<Error = Self::Error> + Readable<Error = Self::Error>
where
Self: 'a;
type Send<'a>: UdpSend<Error = Self::Error>
where
Self: 'a;
fn split(&mut self) -> (Self::Receive<'_>, Self::Send<'_>);
}
impl<T> UdpSplit for &mut T
where
T: UdpSplit,
{
type Receive<'a>
= T::Receive<'a>
where
Self: 'a;
type Send<'a>
= T::Send<'a>
where
Self: 'a;
fn split(&mut self) -> (Self::Receive<'_>, Self::Send<'_>) {
(**self).split()
}
}
/// This is a factory trait for creating connected UDP sockets
pub trait UdpConnect {
/// Error type returned on socket creation failure
type Error: embedded_io_async::Error;
/// The socket type returned by the factory
type Socket<'a>: UdpReceive<Error = Self::Error>
+ UdpSend<Error = Self::Error>
+ UdpSplit<Error = Self::Error>
+ MulticastV4<Error = Self::Error>
+ MulticastV6<Error = Self::Error>
+ Readable<Error = Self::Error>
where
Self: 'a;
/// Connect to a remote socket
async fn connect(
&self,
local: SocketAddr,
remote: SocketAddr,
) -> Result<Self::Socket<'_>, Self::Error>;
}
/// This is a factory trait for binding UDP sockets
pub trait UdpBind {
/// Error type returned on socket creation failure
type Error: embedded_io_async::Error;
/// The socket type returned by the stack
type Socket<'a>: UdpReceive<Error = Self::Error>
+ UdpSend<Error = Self::Error>
+ UdpSplit<Error = Self::Error>
+ MulticastV4<Error = Self::Error>
+ MulticastV6<Error = Self::Error>
+ Readable<Error = Self::Error>
where
Self: 'a;
/// Bind to a local socket address
async fn bind(&self, local: SocketAddr) -> Result<Self::Socket<'_>, Self::Error>;
}
impl<T> UdpConnect for &T
where
T: UdpConnect,
{
type Error = T::Error;
type Socket<'a>
= T::Socket<'a>
where
Self: 'a;
async fn connect(
&self,
local: SocketAddr,
remote: SocketAddr,
) -> Result<Self::Socket<'_>, Self::Error> {
(*self).connect(local, remote).await
}
}
impl<T> UdpConnect for &mut T
where
T: UdpConnect,
{
type Error = T::Error;
type Socket<'a>
= T::Socket<'a>
where
Self: 'a;
async fn connect(
&self,
local: SocketAddr,
remote: SocketAddr,
) -> Result<Self::Socket<'_>, Self::Error> {
(**self).connect(local, remote).await
}
}
impl<T> UdpBind for &T
where
T: UdpBind,
{
type Error = T::Error;
type Socket<'a>
= T::Socket<'a>
where
Self: 'a;
async fn bind(&self, local: SocketAddr) -> Result<Self::Socket<'_>, Self::Error> {
(*self).bind(local).await
}
}
impl<T> UdpBind for &mut T
where
T: UdpBind,
{
type Error = T::Error;
type Socket<'a>
= T::Socket<'a>
where
Self: 'a;
async fn bind(&self, local: SocketAddr) -> Result<Self::Socket<'_>, Self::Error> {
(**self).bind(local).await
}
}