aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src
diff options
context:
space:
mode:
authorArtur Kowalski <[email protected]>2022-08-11 15:52:32 +0200
committerArtur Kowalski <[email protected]>2022-08-11 15:52:32 +0200
commitef473827a2beaca120f45fbe490f84a0be7d381d (patch)
treeee154cb8d8b6d819f8a696a3912b637dcb334d03 /embassy-net/src
parentb97983242d16a321bab8c13f9df4c8af99d89a0f (diff)
Remove UdpIo struct
UdpIo was shared by split sender/receives halves. Since split() API is no more UdpIo is not needed and its APIs may be moved into UdpSocket.
Diffstat (limited to 'embassy-net/src')
-rw-r--r--embassy-net/src/udp.rs98
1 files changed, 41 insertions, 57 deletions
diff --git a/embassy-net/src/udp.rs b/embassy-net/src/udp.rs
index ee90c3010..78b09a492 100644
--- a/embassy-net/src/udp.rs
+++ b/embassy-net/src/udp.rs
@@ -27,7 +27,8 @@ pub enum Error {
27} 27}
28 28
29pub struct UdpSocket<'a> { 29pub struct UdpSocket<'a> {
30 io: UdpIo<'a>, 30 stack: &'a UnsafeCell<SocketStack>,
31 handle: SocketHandle,
31} 32}
32 33
33impl<'a> UdpSocket<'a> { 34impl<'a> UdpSocket<'a> {
@@ -51,10 +52,8 @@ impl<'a> UdpSocket<'a> {
51 )); 52 ));
52 53
53 Self { 54 Self {
54 io: UdpIo { 55 stack: &stack.socket,
55 stack: &stack.socket, 56 handle,
56 handle,
57 },
58 } 57 }
59 } 58 }
60 59
@@ -67,64 +66,17 @@ impl<'a> UdpSocket<'a> {
67 // safety: not accessed reentrantly. 66 // safety: not accessed reentrantly.
68 if endpoint.port == 0 { 67 if endpoint.port == 0 {
69 // If user didn't specify port allocate a dynamic port. 68 // If user didn't specify port allocate a dynamic port.
70 endpoint.port = unsafe { &mut *self.io.stack.get() }.get_local_port(); 69 endpoint.port = unsafe { &mut *self.stack.get() }.get_local_port();
71 } 70 }
72 71
73 // safety: not accessed reentrantly. 72 // safety: not accessed reentrantly.
74 match unsafe { self.io.with_mut(|s, _| s.bind(endpoint)) } { 73 match unsafe { self.with_mut(|s, _| s.bind(endpoint)) } {
75 Ok(()) => Ok(()), 74 Ok(()) => Ok(()),
76 Err(udp::BindError::InvalidState) => Err(BindError::InvalidState), 75 Err(udp::BindError::InvalidState) => Err(BindError::InvalidState),
77 Err(udp::BindError::Unaddressable) => Err(BindError::NoRoute), 76 Err(udp::BindError::Unaddressable) => Err(BindError::NoRoute),
78 } 77 }
79 } 78 }
80 79
81 pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error>
82 where
83 T: Into<IpEndpoint>,
84 {
85 self.io.write(buf, remote_endpoint.into()).await
86 }
87
88 pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> {
89 self.io.read(buf).await
90 }
91
92 pub fn endpoint(&self) -> IpListenEndpoint {
93 unsafe { self.io.with(|s, _| s.endpoint()) }
94 }
95
96 pub fn is_open(&self) -> bool {
97 unsafe { self.io.with(|s, _| s.is_open()) }
98 }
99
100 pub fn close(&mut self) {
101 unsafe { self.io.with_mut(|s, _| s.close()) }
102 }
103
104 pub fn may_send(&self) -> bool {
105 unsafe { self.io.with(|s, _| s.can_send()) }
106 }
107
108 pub fn may_recv(&self) -> bool {
109 unsafe { self.io.with(|s, _| s.can_recv()) }
110 }
111}
112
113impl Drop for UdpSocket<'_> {
114 fn drop(&mut self) {
115 // safety: not accessed reentrantly.
116 let s = unsafe { &mut *self.io.stack.get() };
117 s.sockets.remove(self.io.handle);
118 }
119}
120
121#[derive(Copy, Clone)]
122pub struct UdpIo<'a> {
123 stack: &'a UnsafeCell<SocketStack>,
124 handle: SocketHandle,
125}
126
127impl UdpIo<'_> {
128 /// SAFETY: must not call reentrantly. 80 /// SAFETY: must not call reentrantly.
129 unsafe fn with<R>(&self, f: impl FnOnce(&udp::Socket, &Interface) -> R) -> R { 81 unsafe fn with<R>(&self, f: impl FnOnce(&udp::Socket, &Interface) -> R) -> R {
130 let s = &*self.stack.get(); 82 let s = &*self.stack.get();
@@ -141,7 +93,7 @@ impl UdpIo<'_> {
141 res 93 res
142 } 94 }
143 95
144 async fn read(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { 96 pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> {
145 poll_fn(move |cx| unsafe { 97 poll_fn(move |cx| unsafe {
146 self.with_mut(|s, _| match s.recv_slice(buf) { 98 self.with_mut(|s, _| match s.recv_slice(buf) {
147 Ok(x) => Poll::Ready(Ok(x)), 99 Ok(x) => Poll::Ready(Ok(x)),
@@ -156,9 +108,13 @@ impl UdpIo<'_> {
156 .await 108 .await
157 } 109 }
158 110
159 async fn write(&self, buf: &[u8], ep: IpEndpoint) -> Result<(), Error> { 111 pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error>
112 where
113 T: Into<IpEndpoint>,
114 {
115 let remote_endpoint = remote_endpoint.into();
160 poll_fn(move |cx| unsafe { 116 poll_fn(move |cx| unsafe {
161 self.with_mut(|s, _| match s.send_slice(buf, ep) { 117 self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) {
162 // Entire datagram has been sent 118 // Entire datagram has been sent
163 Ok(()) => Poll::Ready(Ok(())), 119 Ok(()) => Poll::Ready(Ok(())),
164 Err(udp::SendError::BufferFull) => { 120 Err(udp::SendError::BufferFull) => {
@@ -170,4 +126,32 @@ impl UdpIo<'_> {
170 }) 126 })
171 .await 127 .await
172 } 128 }
129
130 pub fn endpoint(&self) -> IpListenEndpoint {
131 unsafe { self.with(|s, _| s.endpoint()) }
132 }
133
134 pub fn is_open(&self) -> bool {
135 unsafe { self.with(|s, _| s.is_open()) }
136 }
137
138 pub fn close(&mut self) {
139 unsafe { self.with_mut(|s, _| s.close()) }
140 }
141
142 pub fn may_send(&self) -> bool {
143 unsafe { self.with(|s, _| s.can_send()) }
144 }
145
146 pub fn may_recv(&self) -> bool {
147 unsafe { self.with(|s, _| s.can_recv()) }
148 }
149}
150
151impl Drop for UdpSocket<'_> {
152 fn drop(&mut self) {
153 // safety: not accessed reentrantly.
154 let s = unsafe { &mut *self.stack.get() };
155 s.sockets.remove(self.handle);
156 }
173} 157}