|
| 1 | +use crate::buf::BoundedBuf; |
| 2 | +use crate::io::SharedFd; |
| 3 | +use crate::runtime::driver::op::{Completable, CqeResult, Op}; |
| 4 | +use crate::runtime::CONTEXT; |
| 5 | +use crate::BufResult; |
| 6 | +use socket2::SockAddr; |
| 7 | +use std::io::IoSlice; |
| 8 | +use std::{boxed::Box, io, net::SocketAddr}; |
| 9 | + |
| 10 | +pub(crate) struct SendMsgZc<T> { |
| 11 | + #[allow(dead_code)] |
| 12 | + fd: SharedFd, |
| 13 | + |
| 14 | + /*pub(crate) buf: T, |
| 15 | + |
| 16 | + #[allow(dead_code)] |
| 17 | + io_slices: Vec<IoSlice<'static>>, |
| 18 | + |
| 19 | + #[allow(dead_code)] |
| 20 | + socket_addr: Box<SockAddr>,*/ |
| 21 | + |
| 22 | + pub(crate) msghdr: Box<libc::msghdr>, |
| 23 | +} |
| 24 | + |
| 25 | +impl<T: BoundedBuf> Op<SendZc<T>, MultiCQEFuture> { |
| 26 | + pub(crate) fn sendmsg_zc(fd: &SharedFd, msghdr: Box<libc::msghdr>) -> io::Result<Self> { |
| 27 | + use io_uring::{opcode, types}; |
| 28 | + |
| 29 | + /*let io_slices = vec![IoSlice::new(unsafe { |
| 30 | + std::slice::from_raw_parts(buf.stable_ptr(), buf.bytes_init()) |
| 31 | + })]; |
| 32 | +
|
| 33 | + let socket_addr = Box::new(SockAddr::from(socket_addr)); |
| 34 | +
|
| 35 | + let mut msghdr: Box<libc::msghdr> = Box::new(unsafe { std::mem::zeroed() }); |
| 36 | + msghdr.msg_iov = io_slices.as_ptr() as *mut _; |
| 37 | + msghdr.msg_iovlen = io_slices.len() as _; |
| 38 | + msghdr.msg_name = socket_addr.as_ptr() as *mut libc::c_void; |
| 39 | + msghdr.msg_namelen = socket_addr.len();*/ |
| 40 | + |
| 41 | + CONTEXT.with(|x| { |
| 42 | + x.handle().expect("Not in a runtime context").submit_op( |
| 43 | + SendMsgZc { |
| 44 | + fd: fd.clone(), |
| 45 | + /*buf, |
| 46 | + io_slices, |
| 47 | + socket_addr,*/ |
| 48 | + msghdr: msghdr.clone(), |
| 49 | + }, |
| 50 | + |sendmsg_zc| { |
| 51 | + opcode::SendMsgZc::new(types::Fd(sendmsg_zc.fd.raw_fd()), sendmsg_zc.msghdr.as_ref() as *const _).build() |
| 52 | + }, |
| 53 | + ) |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl<T> Completable for SendMsgZc<T> { |
| 59 | + type Output = BufResult<usize, T>; |
| 60 | + |
| 61 | + fn complete(self, cqe: CqeResult) -> Self::Output { |
| 62 | + // Convert the operation result to `usize` |
| 63 | + let res = cqe.result.map(|v| v as usize); |
| 64 | + // Recover the buffer |
| 65 | + let buf = self.buf; |
| 66 | + |
| 67 | + (res, buf) |
| 68 | + } |
| 69 | +} |
0 commit comments