Skip to content

Commit 293e3e3

Browse files
committed
feat(net): basic recv_msg/send_msg on UdpSocket
1 parent 97eb90a commit 293e3e3

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

compio-net/src/socket.rs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use compio_driver::op::CreateSocket;
66
use compio_driver::{
77
impl_raw_fd,
88
op::{
9-
Accept, BufResultExt, CloseSocket, Connect, Recv, RecvFrom, RecvFromVectored,
10-
RecvResultExt, RecvVectored, Send, SendTo, SendToVectored, SendVectored, ShutdownSocket,
9+
Accept, BufResultExt, CloseSocket, Connect, MsgBuf, Recv, RecvFrom, RecvFromVectored,
10+
RecvMsg, RecvMsgVectored, RecvResultExt, RecvVectored, Send, SendMsg, SendMsgVectored,
11+
SendTo, SendToVectored, SendVectored, ShutdownSocket,
1112
},
1213
ToSharedFd,
1314
};
@@ -256,6 +257,36 @@ impl Socket {
256257
.map_advanced()
257258
}
258259

260+
pub async fn recv_msg<T: IoBufMut, C: IoBufMut>(
261+
&self,
262+
buffer: T,
263+
control: C,
264+
) -> BufResult<(usize, SockAddr), (T, C)> {
265+
let fd = self.to_shared_fd();
266+
let op = RecvMsg::new(fd, MsgBuf::new(buffer, control));
267+
compio_runtime::submit(op)
268+
.await
269+
.into_inner()
270+
.map_addr()
271+
.map_advanced()
272+
.map_buffer(MsgBuf::into_tuple)
273+
}
274+
275+
pub async fn recv_msg_vectored<T: IoVectoredBufMut, C: IoBufMut>(
276+
&self,
277+
buffer: T,
278+
control: C,
279+
) -> BufResult<(usize, SockAddr), (T, C)> {
280+
let fd = self.to_shared_fd();
281+
let op = RecvMsgVectored::new(fd, MsgBuf::new(buffer, control));
282+
compio_runtime::submit(op)
283+
.await
284+
.into_inner()
285+
.map_addr()
286+
.map_advanced()
287+
.map_buffer(MsgBuf::into_tuple)
288+
}
289+
259290
pub async fn send_to<T: IoBuf>(&self, buffer: T, addr: &SockAddr) -> BufResult<usize, T> {
260291
let fd = self.to_shared_fd();
261292
let op = SendTo::new(fd, buffer, addr.clone());
@@ -271,6 +302,34 @@ impl Socket {
271302
let op = SendToVectored::new(fd, buffer, addr.clone());
272303
compio_runtime::submit(op).await.into_inner()
273304
}
305+
306+
pub async fn send_msg<T: IoBuf, C: IoBuf>(
307+
&self,
308+
buffer: T,
309+
control: C,
310+
addr: &SockAddr,
311+
) -> BufResult<usize, (T, C)> {
312+
let fd = self.to_shared_fd();
313+
let op = SendMsg::new(fd, MsgBuf::new(buffer, control), addr.clone());
314+
compio_runtime::submit(op)
315+
.await
316+
.into_inner()
317+
.map_buffer(MsgBuf::into_tuple)
318+
}
319+
320+
pub async fn send_msg_vectored<T: IoVectoredBuf, C: IoBuf>(
321+
&self,
322+
buffer: T,
323+
control: C,
324+
addr: &SockAddr,
325+
) -> BufResult<usize, (T, C)> {
326+
let fd = self.to_shared_fd();
327+
let op = SendMsgVectored::new(fd, MsgBuf::new(buffer, control), addr.clone());
328+
compio_runtime::submit(op)
329+
.await
330+
.into_inner()
331+
.map_buffer(MsgBuf::into_tuple)
332+
}
274333
}
275334

276335
impl_raw_fd!(Socket, Socket2, socket, socket);

compio-net/src/udp.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,32 @@ impl UdpSocket {
222222
.map_res(|(n, addr)| (n, addr.as_socket().expect("should be SocketAddr")))
223223
}
224224

225+
/// Receives a single datagram message and ancillary data on the socket. On
226+
/// success, returns the number of bytes received and the origin.
227+
pub async fn recv_msg<T: IoBufMut, C: IoBufMut>(
228+
&self,
229+
buffer: T,
230+
control: C,
231+
) -> BufResult<(usize, SocketAddr), (T, C)> {
232+
self.inner
233+
.recv_msg(buffer, control)
234+
.await
235+
.map_res(|(n, addr)| (n, addr.as_socket().expect("should be SocketAddr")))
236+
}
237+
238+
/// Receives a single datagram message and ancillary data on the socket. On
239+
/// success, returns the number of bytes received and the origin.
240+
pub async fn recv_msg_vectored<T: IoVectoredBufMut, C: IoBufMut>(
241+
&self,
242+
buffer: T,
243+
control: C,
244+
) -> BufResult<(usize, SocketAddr), (T, C)> {
245+
self.inner
246+
.recv_msg_vectored(buffer, control)
247+
.await
248+
.map_res(|(n, addr)| (n, addr.as_socket().expect("should be SocketAddr")))
249+
}
250+
225251
/// Sends data on the socket to the given address. On success, returns the
226252
/// number of bytes sent.
227253
pub async fn send_to<T: IoBuf>(
@@ -249,6 +275,46 @@ impl UdpSocket {
249275
})
250276
.await
251277
}
278+
279+
/// Sends data on the socket to the given address accompanied by ancillary
280+
/// data. On success, returns the number of bytes sent.
281+
pub async fn send_msg<T: IoBuf, C: IoBuf>(
282+
&self,
283+
buffer: T,
284+
control: C,
285+
addr: impl ToSocketAddrsAsync,
286+
) -> BufResult<usize, (T, C)> {
287+
super::first_addr_buf(
288+
addr,
289+
(buffer, control),
290+
|addr, (buffer, control)| async move {
291+
self.inner
292+
.send_msg(buffer, control, &SockAddr::from(addr))
293+
.await
294+
},
295+
)
296+
.await
297+
}
298+
299+
/// Sends data on the socket to the given address accompanied by ancillary
300+
/// data. On success, returns the number of bytes sent.
301+
pub async fn send_msg_vectored<T: IoVectoredBuf, C: IoBuf>(
302+
&self,
303+
buffer: T,
304+
control: C,
305+
addr: impl ToSocketAddrsAsync,
306+
) -> BufResult<usize, (T, C)> {
307+
super::first_addr_buf(
308+
addr,
309+
(buffer, control),
310+
|addr, (buffer, control)| async move {
311+
self.inner
312+
.send_msg_vectored(buffer, control, &SockAddr::from(addr))
313+
.await
314+
},
315+
)
316+
.await
317+
}
252318
}
253319

254320
impl_raw_fd!(UdpSocket, socket2::Socket, inner, socket);

0 commit comments

Comments
 (0)