Skip to main content

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, net::socket::Socket, state::State};
9
10/// A UDP socket.
11#[must_use]
12pub struct UdpSocket {
13    socket: Socket,
14}
15
16impl UdpSocket {
17    /// Binds on a given address.
18    ///
19    /// # Errors
20    ///
21    /// Returns an error if a syscall fails.
22    pub fn bind(addr: SocketAddr) -> rustix::io::Result<Self> {
23        let socket = Socket::new(addr.as_any().address_family(), SocketType::DGRAM)?;
24        socket.bind(&addr)?;
25        Ok(Self { socket })
26    }
27
28    /// Sends a given buffer to a given address.
29    ///
30    /// # Errors
31    ///
32    /// Returns an error if a syscall fails.
33    ///
34    /// # Cancel safety
35    ///
36    /// This method is cancel safe.
37    pub async fn send_to<
38        S: State,
39        const TASKS: usize,
40        const TASK_SIZE: usize,
41        const ROOT_TASK_SIZE: usize,
42        const TIMERS: usize,
43        const FDS: usize,
44    >(
45        &mut self,
46        ctx: &Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
47        buf: &[u8],
48        addr: SocketAddr,
49    ) -> rustix::io::Result<usize> {
50        self.socket.send_to(ctx, buf, &addr).await
51    }
52
53    /// Receives a datagram from any sender into a given buffer, returns its size and the sender address.
54    ///
55    /// # Errors
56    ///
57    /// Returns an error if a syscall fails.
58    ///
59    /// # Cancel safety
60    ///
61    /// This method is cancel safe.
62    pub fn recv_from<
63        'a,
64        S: State,
65        const TASKS: usize,
66        const TASK_SIZE: usize,
67        const ROOT_TASK_SIZE: usize,
68        const TIMERS: usize,
69        const FDS: usize,
70    >(
71        &'a mut self,
72        ctx: &'a Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
73        buf: &'a mut [u8],
74    ) -> impl Future<Output = rustix::io::Result<(usize, SocketAddr)>> {
75        let recv = self.socket.recv_from(ctx, buf);
76        async move {
77            let (len, addr) = recv.await?;
78            Ok((len, addr.ok_or(Errno::AFNOSUPPORT)?.try_into()?))
79        }
80    }
81
82    /// Connects to a given address, which makes it the peer of [`send`](Self::send) and [`recv`](Self::recv).
83    ///
84    /// # Errors
85    ///
86    /// Returns an error if a syscall fails.
87    pub fn connect(&self, addr: SocketAddr) -> rustix::io::Result<()> {
88        self.socket.connect(&addr)
89    }
90
91    /// Sends a given buffer to the connected peer, see [`connect`](Self::connect).
92    ///
93    /// # Errors
94    ///
95    /// Returns an error if a syscall fails.
96    ///
97    /// # Cancel safety
98    ///
99    /// This method is cancel safe.
100    pub fn send<
101        'a,
102        S: State,
103        const TASKS: usize,
104        const TASK_SIZE: usize,
105        const ROOT_TASK_SIZE: usize,
106        const TIMERS: usize,
107        const FDS: usize,
108    >(
109        &'a mut self,
110        ctx: &'a Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
111        buf: &'a [u8],
112    ) -> impl Future<Output = rustix::io::Result<usize>> {
113        self.socket.send(ctx, buf)
114    }
115
116    /// Receives a datagram from the connected peer into a given buffer, see [`connect`](Self::connect).
117    ///
118    /// # Errors
119    ///
120    /// Returns an error if a syscall fails.
121    ///
122    /// # Cancel safety
123    ///
124    /// This method is cancel safe.
125    pub fn recv<
126        'a,
127        S: State,
128        const TASKS: usize,
129        const TASK_SIZE: usize,
130        const ROOT_TASK_SIZE: usize,
131        const TIMERS: usize,
132        const FDS: usize,
133    >(
134        &'a mut self,
135        ctx: &'a Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
136        buf: &'a mut [u8],
137    ) -> impl Future<Output = rustix::io::Result<usize>> {
138        self.socket.recv(ctx, buf)
139    }
140
141    /// Returns local address.
142    ///
143    /// # Errors
144    ///
145    /// Returns an error if a syscall fails.
146    pub fn local_addr(&self) -> rustix::io::Result<SocketAddr> {
147        self.socket.local_addr()
148    }
149}
150
151impl core::fmt::Debug for UdpSocket {
152    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
153        f.write_str("UdpSocket")
154    }
155}