Skip to content

Commit 10da37e

Browse files
committed
fix(driver): simplify opcodes
1 parent 293e3e3 commit 10da37e

File tree

6 files changed

+131
-471
lines changed

6 files changed

+131
-471
lines changed

compio-driver/src/iocp/op.rs

Lines changed: 51 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -779,256 +779,125 @@ impl<T: IoVectoredBuf, S: AsRawFd> OpCode for SendToVectored<T, S> {
779779

780780
static WSA_RECVMSG: OnceLock<LPFN_WSARECVMSG> = OnceLock::new();
781781

782-
struct RecvMsgHeader<S> {
783-
fd: SharedFd<S>,
782+
/// Receive data and source address with ancillary data into vectored buffer.
783+
pub struct RecvMsg<T: IoVectoredBufMut, C: IoBufMut, S> {
784784
addr: SOCKADDR_STORAGE,
785785
addr_len: socklen_t,
786+
fd: SharedFd<S>,
787+
buffer: T,
788+
control: C,
786789
_p: PhantomPinned,
787790
}
788791

789-
impl<S> RecvMsgHeader<S> {
790-
fn new(fd: SharedFd<S>) -> Self {
792+
impl<T: IoVectoredBufMut, C: IoBufMut, S> RecvMsg<T, C, S> {
793+
/// Create [`RecvMsgVectored`].
794+
pub fn new(fd: SharedFd<S>, buffer: T, control: C) -> Self {
791795
Self {
792-
fd,
793796
addr: unsafe { std::mem::zeroed() },
794797
addr_len: std::mem::size_of::<SOCKADDR_STORAGE>() as _,
798+
fd,
799+
buffer,
800+
control,
795801
_p: PhantomPinned,
796802
}
797803
}
804+
}
798805

799-
fn into_addr(self) -> (SOCKADDR_STORAGE, socklen_t) {
800-
(self.addr, self.addr_len)
806+
impl<T: IoVectoredBufMut, C: IoBufMut, S> IntoInner for RecvMsg<T, C, S> {
807+
type Inner = ((T, C), SOCKADDR_STORAGE, socklen_t);
808+
809+
fn into_inner(self) -> Self::Inner {
810+
((self.buffer, self.control), self.addr, self.addr_len)
801811
}
802812
}
803813

804-
impl<S: AsRawFd> RecvMsgHeader<S> {
805-
unsafe fn operate(
806-
&mut self,
807-
slices: &mut [IoSliceMut],
808-
control: IoSliceMut,
809-
optr: *mut OVERLAPPED,
810-
) -> Poll<io::Result<usize>> {
814+
impl<T: IoVectoredBufMut, C: IoBufMut, S: AsRawFd> OpCode for RecvMsg<T, C, S> {
815+
unsafe fn operate(self: Pin<&mut Self>, optr: *mut OVERLAPPED) -> Poll<io::Result<usize>> {
811816
let recvmsg_fn = WSA_RECVMSG
812817
.get_or_try_init(|| get_wsa_fn(self.fd.as_raw_fd(), WSAID_WSARECVMSG))?
813818
.ok_or_else(|| {
814819
io::Error::new(io::ErrorKind::Unsupported, "cannot retrieve WSARecvMsg")
815820
})?;
816821

822+
let this = self.get_unchecked_mut();
823+
let mut slices = this.buffer.as_io_slices_mut();
817824
let mut msg = WSAMSG {
818-
name: &mut self.addr as *mut _ as _,
819-
namelen: self.addr_len,
825+
name: &mut this.addr as *mut _ as _,
826+
namelen: this.addr_len,
820827
lpBuffers: slices.as_mut_ptr() as _,
821828
dwBufferCount: slices.len() as _,
822-
Control: std::mem::transmute::<IoSliceMut, WSABUF>(control),
829+
Control: std::mem::transmute::<IoSliceMut, WSABUF>(this.control.as_io_slice_mut()),
823830
dwFlags: 0,
824831
};
825832

826833
let mut received = 0;
827834
let res = recvmsg_fn(
828-
self.fd.as_raw_fd() as _,
835+
this.fd.as_raw_fd() as _,
829836
&mut msg,
830837
&mut received,
831838
optr,
832839
None,
833840
);
834841
winsock_result(res, received)
835842
}
836-
}
837-
838-
/// Receive data and source address with ancillary data.
839-
pub struct RecvMsg<T: IoBufMut, C: IoBufMut, S> {
840-
header: RecvMsgHeader<S>,
841-
buffer: MsgBuf<T, C>,
842-
}
843-
844-
impl<T: IoBufMut, C: IoBufMut, S> RecvMsg<T, C, S> {
845-
/// Create [`RecvMsg`].
846-
pub fn new(fd: SharedFd<S>, buffer: MsgBuf<T, C>) -> Self {
847-
Self {
848-
header: RecvMsgHeader::new(fd),
849-
buffer,
850-
}
851-
}
852-
}
853-
854-
impl<T: IoBufMut, C: IoBufMut, S> IntoInner for RecvMsg<T, C, S> {
855-
type Inner = (MsgBuf<T, C>, SOCKADDR_STORAGE, socklen_t);
856-
857-
fn into_inner(self) -> Self::Inner {
858-
let (addr, addr_len) = self.header.into_addr();
859-
(self.buffer, addr, addr_len)
860-
}
861-
}
862-
863-
impl<T: IoBufMut, C: IoBufMut, S: AsRawFd> OpCode for RecvMsg<T, C, S> {
864-
unsafe fn operate(self: Pin<&mut Self>, optr: *mut OVERLAPPED) -> Poll<io::Result<usize>> {
865-
let this = self.get_unchecked_mut();
866-
this.header.operate(
867-
&mut [this.buffer.inner.as_io_slice_mut()],
868-
this.buffer.control.as_io_slice_mut(),
869-
optr,
870-
)
871-
}
872843

873844
unsafe fn cancel(self: Pin<&mut Self>, optr: *mut OVERLAPPED) -> io::Result<()> {
874-
cancel(self.header.fd.as_raw_fd(), optr)
845+
cancel(self.fd.as_raw_fd(), optr)
875846
}
876847
}
877848

878-
/// Receive data and source address with ancillary data into vectored buffer.
879-
pub struct RecvMsgVectored<T: IoVectoredBufMut, C: IoBufMut, S> {
880-
header: RecvMsgHeader<S>,
881-
buffer: MsgBuf<T, C>,
849+
/// Send data to specified address accompanied by ancillary data from vectored
850+
/// buffer.
851+
pub struct SendMsg<T: IoVectoredBuf, C: IoBuf, S> {
852+
fd: SharedFd<S>,
853+
buffer: T,
854+
control: C,
855+
addr: SockAddr,
856+
_p: PhantomPinned,
882857
}
883858

884-
impl<T: IoVectoredBufMut, C: IoBufMut, S> RecvMsgVectored<T, C, S> {
885-
/// Create [`RecvMsgVectored`].
886-
pub fn new(fd: SharedFd<S>, buffer: MsgBuf<T, C>) -> Self {
859+
impl<T: IoVectoredBuf, C: IoBuf, S> SendMsg<T, C, S> {
860+
/// Create [`SendMsgVectored`].
861+
pub fn new(fd: SharedFd<S>, buffer: T, control: C, addr: SockAddr) -> Self {
887862
Self {
888-
header: RecvMsgHeader::new(fd),
863+
fd,
889864
buffer,
865+
control,
866+
addr,
867+
_p: PhantomPinned,
890868
}
891869
}
892870
}
893871

894-
impl<T: IoVectoredBufMut, C: IoBufMut, S> IntoInner for RecvMsgVectored<T, C, S> {
895-
type Inner = (MsgBuf<T, C>, SOCKADDR_STORAGE, socklen_t);
872+
impl<T: IoVectoredBuf, C: IoBuf, S> IntoInner for SendMsg<T, C, S> {
873+
type Inner = (T, C);
896874

897875
fn into_inner(self) -> Self::Inner {
898-
let (addr, addr_len) = self.header.into_addr();
899-
(self.buffer, addr, addr_len)
876+
(self.buffer, self.control)
900877
}
901878
}
902879

903-
impl<T: IoVectoredBufMut, C: IoBufMut, S: AsRawFd> OpCode for RecvMsgVectored<T, C, S> {
880+
impl<T: IoVectoredBuf, C: IoBuf, S: AsRawFd> OpCode for SendMsg<T, C, S> {
904881
unsafe fn operate(self: Pin<&mut Self>, optr: *mut OVERLAPPED) -> Poll<io::Result<usize>> {
905882
let this = self.get_unchecked_mut();
906-
this.header.operate(
907-
&mut this.buffer.inner.as_io_slices_mut(),
908-
this.buffer.control.as_io_slice_mut(),
909-
optr,
910-
)
911-
}
912-
913-
unsafe fn cancel(self: Pin<&mut Self>, optr: *mut OVERLAPPED) -> io::Result<()> {
914-
cancel(self.header.fd.as_raw_fd(), optr)
915-
}
916-
}
917-
918-
struct SendMsgHeader<S> {
919-
fd: SharedFd<S>,
920-
addr: SockAddr,
921-
_p: PhantomPinned,
922-
}
923883

924-
impl<S> SendMsgHeader<S> {
925-
pub fn new(fd: SharedFd<S>, addr: SockAddr) -> Self {
926-
Self {
927-
fd,
928-
addr,
929-
_p: PhantomPinned,
930-
}
931-
}
932-
}
933-
934-
impl<S: AsRawFd> SendMsgHeader<S> {
935-
unsafe fn operate(
936-
&mut self,
937-
slices: &mut [IoSlice],
938-
control: IoSlice,
939-
optr: *mut OVERLAPPED,
940-
) -> Poll<io::Result<usize>> {
884+
let slices = this.buffer.as_io_slices();
941885
let msg = WSAMSG {
942-
name: self.addr.as_ptr() as _,
943-
namelen: self.addr.len(),
886+
name: this.addr.as_ptr() as _,
887+
namelen: this.addr.len(),
944888
lpBuffers: slices.as_ptr() as _,
945889
dwBufferCount: slices.len() as _,
946-
Control: std::mem::transmute::<IoSlice, WSABUF>(control),
890+
Control: std::mem::transmute::<IoSlice, WSABUF>(this.control.as_io_slice()),
947891
dwFlags: 0,
948892
};
949893

950894
let mut sent = 0;
951-
let res = WSASendMsg(self.fd.as_raw_fd() as _, &msg, 0, &mut sent, optr, None);
895+
let res = WSASendMsg(this.fd.as_raw_fd() as _, &msg, 0, &mut sent, optr, None);
952896
winsock_result(res, sent)
953897
}
954-
}
955-
956-
/// Send data to specified address accompanied by ancillary data.
957-
pub struct SendMsg<T: IoBuf, C: IoBuf, S> {
958-
header: SendMsgHeader<S>,
959-
buffer: MsgBuf<T, C>,
960-
}
961-
962-
impl<T: IoBuf, C: IoBuf, S> SendMsg<T, C, S> {
963-
/// Create [`SendMsg`].
964-
pub fn new(fd: SharedFd<S>, buffer: MsgBuf<T, C>, addr: SockAddr) -> Self {
965-
Self {
966-
header: SendMsgHeader::new(fd, addr),
967-
buffer,
968-
}
969-
}
970-
}
971-
972-
impl<T: IoBuf, C: IoBuf, S> IntoInner for SendMsg<T, C, S> {
973-
type Inner = MsgBuf<T, C>;
974-
975-
fn into_inner(self) -> Self::Inner {
976-
self.buffer
977-
}
978-
}
979-
980-
impl<T: IoBuf, C: IoBuf, S: AsRawFd> OpCode for SendMsg<T, C, S> {
981-
unsafe fn operate(self: Pin<&mut Self>, optr: *mut OVERLAPPED) -> Poll<io::Result<usize>> {
982-
let this = self.get_unchecked_mut();
983-
this.header.operate(
984-
&mut [this.buffer.inner.as_io_slice()],
985-
this.buffer.control.as_io_slice(),
986-
optr,
987-
)
988-
}
989898

990899
unsafe fn cancel(self: Pin<&mut Self>, optr: *mut OVERLAPPED) -> io::Result<()> {
991-
cancel(self.header.fd.as_raw_fd(), optr)
992-
}
993-
}
994-
995-
/// Send data to specified address accompanied by ancillary data from vectored
996-
/// buffer.
997-
pub struct SendMsgVectored<T: IoVectoredBuf, C: IoBuf, S> {
998-
header: SendMsgHeader<S>,
999-
buffer: MsgBuf<T, C>,
1000-
}
1001-
1002-
impl<T: IoVectoredBuf, C: IoBuf, S> SendMsgVectored<T, C, S> {
1003-
/// Create [`SendMsgVectored`].
1004-
pub fn new(fd: SharedFd<S>, buffer: MsgBuf<T, C>, addr: SockAddr) -> Self {
1005-
Self {
1006-
header: SendMsgHeader::new(fd, addr),
1007-
buffer,
1008-
}
1009-
}
1010-
}
1011-
1012-
impl<T: IoVectoredBuf, C: IoBuf, S> IntoInner for SendMsgVectored<T, C, S> {
1013-
type Inner = MsgBuf<T, C>;
1014-
1015-
fn into_inner(self) -> Self::Inner {
1016-
self.buffer
1017-
}
1018-
}
1019-
1020-
impl<T: IoVectoredBuf, C: IoBuf, S: AsRawFd> OpCode for SendMsgVectored<T, C, S> {
1021-
unsafe fn operate(self: Pin<&mut Self>, optr: *mut OVERLAPPED) -> Poll<io::Result<usize>> {
1022-
let this = self.get_unchecked_mut();
1023-
this.header.operate(
1024-
&mut this.buffer.inner.as_io_slices(),
1025-
this.buffer.control.as_io_slice(),
1026-
optr,
1027-
)
1028-
}
1029-
1030-
unsafe fn cancel(self: Pin<&mut Self>, optr: *mut OVERLAPPED) -> io::Result<()> {
1031-
cancel(self.header.fd.as_raw_fd(), optr)
900+
cancel(self.fd.as_raw_fd(), optr)
1032901
}
1033902
}
1034903

compio-driver/src/iour/op.rs

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -556,51 +556,23 @@ impl<T: IoVectoredBuf, S> IntoInner for SendToVectored<T, S> {
556556
}
557557
}
558558

559-
impl<S: AsRawFd> RecvMsgHeader<S> {
560-
pub fn create_entry(&mut self) -> OpEntry {
561-
opcode::RecvMsg::new(Fd(self.fd.as_raw_fd()), &mut self.msg)
562-
.build()
563-
.into()
564-
}
565-
}
566-
567-
impl<T: IoBufMut, C: IoBufMut, S: AsRawFd> OpCode for RecvMsg<T, C, S> {
568-
fn create_entry(self: Pin<&mut Self>) -> OpEntry {
569-
let this = unsafe { self.get_unchecked_mut() };
570-
this.set_msg();
571-
this.header.create_entry()
572-
}
573-
}
574-
575-
impl<T: IoVectoredBufMut, C: IoBufMut, S: AsRawFd> OpCode for RecvMsgVectored<T, C, S> {
559+
impl<T: IoVectoredBufMut, C: IoBufMut, S: AsRawFd> OpCode for RecvMsg<T, C, S> {
576560
fn create_entry(self: Pin<&mut Self>) -> OpEntry {
577561
let this = unsafe { self.get_unchecked_mut() };
578562
this.set_msg();
579-
this.header.create_entry()
580-
}
581-
}
582-
583-
impl<S: AsRawFd> SendMsgHeader<S> {
584-
pub fn create_entry(&mut self) -> OpEntry {
585-
opcode::SendMsg::new(Fd(self.fd.as_raw_fd()), &self.msg)
563+
opcode::RecvMsg::new(Fd(this.fd.as_raw_fd()), &mut this.msg)
586564
.build()
587565
.into()
588566
}
589567
}
590568

591-
impl<T: IoBuf, C: IoBuf, S: AsRawFd> OpCode for SendMsg<T, C, S> {
592-
fn create_entry(self: Pin<&mut Self>) -> OpEntry {
593-
let this = unsafe { self.get_unchecked_mut() };
594-
this.set_msg();
595-
this.header.create_entry()
596-
}
597-
}
598-
599-
impl<T: IoVectoredBuf, C: IoBuf, S: AsRawFd> OpCode for SendMsgVectored<T, C, S> {
569+
impl<T: IoVectoredBuf, C: IoBuf, S: AsRawFd> OpCode for SendMsg<T, C, S> {
600570
fn create_entry(self: Pin<&mut Self>) -> OpEntry {
601571
let this = unsafe { self.get_unchecked_mut() };
602572
this.set_msg();
603-
this.header.create_entry()
573+
opcode::SendMsg::new(Fd(this.fd.as_raw_fd()), &this.msg)
574+
.build()
575+
.into()
604576
}
605577
}
606578

0 commit comments

Comments
 (0)