externally_driven_async_rt/net/udp_socket.rs
1use core::net::SocketAddr;
2
3use rustix::{
4 io::Errno,
5 net::{SocketType, addr::SocketAddrArg},
6};
7
8use crate::context::Context;
9
10use super::{NetError, socket::Socket};
11
12/// A UDP socket.
13#[must_use]
14pub struct UdpSocket<'sel, const FDS: usize> {
15 socket: Socket<'sel, FDS>,
16}
17
18impl<'sel, const FDS: usize> UdpSocket<'sel, FDS> {
19 /// Binds on a given address.
20 ///
21 /// # Errors
22 ///
23 /// Returns an error if a syscall fails or if all `FDS` fd slots are taken.
24 pub fn bind<
25 E: 'static,
26 const EVENTS: usize,
27 const TASKS: usize,
28 const TASK_SIZE: usize,
29 const ROOT_TASK_SIZE: usize,
30 const TIMERS: usize,
31 >(
32 ctx: &Context<'sel, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
33 addr: SocketAddr,
34 ) -> Result<Self, NetError> {
35 let socket = Socket::new(
36 &ctx.storage.io_waiters,
37 ctx.selector,
38 addr.as_any().address_family(),
39 SocketType::DGRAM,
40 )?;
41 socket.bind(&addr)?;
42 Ok(Self { socket })
43 }
44
45 /// Sends a given buffer to a given address.
46 ///
47 /// # Errors
48 ///
49 /// Returns an error if a syscall fails.
50 ///
51 /// # Cancel safety
52 ///
53 /// This method is cancel safe.
54 pub async fn send_to(&mut self, buf: &[u8], addr: SocketAddr) -> Result<usize, NetError> {
55 self.socket.send_to(buf, &addr).await
56 }
57
58 /// Receives a datagram from any sender into a given buffer, returns its size and the sender address.
59 ///
60 /// # Errors
61 ///
62 /// Returns an error if a syscall fails.
63 ///
64 /// # Cancel safety
65 ///
66 /// This method is cancel safe.
67 pub fn recv_from<'a>(
68 &'a mut self,
69 buf: &'a mut [u8],
70 ) -> impl Future<Output = Result<(usize, SocketAddr), NetError>> {
71 let recv = self.socket.recv_from(buf);
72 async move {
73 let (len, addr) = recv.await?;
74 Ok((len, addr.ok_or(Errno::AFNOSUPPORT)?.try_into()?))
75 }
76 }
77
78 /// Connects to a given address, which makes it the peer of [`send`](Self::send) and [`recv`](Self::recv).
79 ///
80 /// # Errors
81 ///
82 /// Returns an error if a syscall fails.
83 pub fn connect(&self, addr: SocketAddr) -> rustix::io::Result<()> {
84 self.socket.connect(&addr)
85 }
86
87 /// Sends a given buffer to the connected peer, see [`connect`](Self::connect).
88 ///
89 /// # Errors
90 ///
91 /// Returns an error if a syscall fails.
92 ///
93 /// # Cancel safety
94 ///
95 /// This method is cancel safe.
96 pub fn send<'a>(&'a mut self, buf: &'a [u8]) -> impl Future<Output = Result<usize, NetError>> {
97 self.socket.send(buf)
98 }
99
100 /// Receives a datagram from the connected peer into a given buffer, see [`connect`](Self::connect).
101 ///
102 /// # Errors
103 ///
104 /// Returns an error if a syscall fails.
105 ///
106 /// # Cancel safety
107 ///
108 /// This method is cancel safe.
109 pub fn recv<'a>(
110 &'a mut self,
111 buf: &'a mut [u8],
112 ) -> impl Future<Output = Result<usize, NetError>> {
113 self.socket.recv(buf)
114 }
115
116 /// Returns local address.
117 ///
118 /// # Errors
119 ///
120 /// Returns an error if a syscall fails.
121 pub fn local_addr(&self) -> rustix::io::Result<SocketAddr> {
122 self.socket.local_addr()
123 }
124}