@@ -779,256 +779,125 @@ impl<T: IoVectoredBuf, S: AsRawFd> OpCode for SendToVectored<T, S> {
779
779
780
780
static WSA_RECVMSG : OnceLock < LPFN_WSARECVMSG > = OnceLock :: new ( ) ;
781
781
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 > {
784
784
addr : SOCKADDR_STORAGE ,
785
785
addr_len : socklen_t ,
786
+ fd : SharedFd < S > ,
787
+ buffer : T ,
788
+ control : C ,
786
789
_p : PhantomPinned ,
787
790
}
788
791
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 {
791
795
Self {
792
- fd,
793
796
addr : unsafe { std:: mem:: zeroed ( ) } ,
794
797
addr_len : std:: mem:: size_of :: < SOCKADDR_STORAGE > ( ) as _ ,
798
+ fd,
799
+ buffer,
800
+ control,
795
801
_p : PhantomPinned ,
796
802
}
797
803
}
804
+ }
798
805
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 )
801
811
}
802
812
}
803
813
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 > > {
811
816
let recvmsg_fn = WSA_RECVMSG
812
817
. get_or_try_init ( || get_wsa_fn ( self . fd . as_raw_fd ( ) , WSAID_WSARECVMSG ) ) ?
813
818
. ok_or_else ( || {
814
819
io:: Error :: new ( io:: ErrorKind :: Unsupported , "cannot retrieve WSARecvMsg" )
815
820
} ) ?;
816
821
822
+ let this = self . get_unchecked_mut ( ) ;
823
+ let mut slices = this. buffer . as_io_slices_mut ( ) ;
817
824
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 ,
820
827
lpBuffers : slices. as_mut_ptr ( ) as _ ,
821
828
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 ( ) ) ,
823
830
dwFlags : 0 ,
824
831
} ;
825
832
826
833
let mut received = 0 ;
827
834
let res = recvmsg_fn (
828
- self . fd . as_raw_fd ( ) as _ ,
835
+ this . fd . as_raw_fd ( ) as _ ,
829
836
& mut msg,
830
837
& mut received,
831
838
optr,
832
839
None ,
833
840
) ;
834
841
winsock_result ( res, received)
835
842
}
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
- }
872
843
873
844
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)
875
846
}
876
847
}
877
848
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 ,
882
857
}
883
858
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 {
887
862
Self {
888
- header : RecvMsgHeader :: new ( fd ) ,
863
+ fd ,
889
864
buffer,
865
+ control,
866
+ addr,
867
+ _p : PhantomPinned ,
890
868
}
891
869
}
892
870
}
893
871
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 ) ;
896
874
897
875
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 )
900
877
}
901
878
}
902
879
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 > {
904
881
unsafe fn operate ( self : Pin < & mut Self > , optr : * mut OVERLAPPED ) -> Poll < io:: Result < usize > > {
905
882
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
- }
923
883
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 ( ) ;
941
885
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 ( ) ,
944
888
lpBuffers : slices. as_ptr ( ) as _ ,
945
889
dwBufferCount : slices. len ( ) as _ ,
946
- Control : std:: mem:: transmute :: < IoSlice , WSABUF > ( control) ,
890
+ Control : std:: mem:: transmute :: < IoSlice , WSABUF > ( this . control . as_io_slice ( ) ) ,
947
891
dwFlags : 0 ,
948
892
} ;
949
893
950
894
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 ) ;
952
896
winsock_result ( res, sent)
953
897
}
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
- }
989
898
990
899
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)
1032
901
}
1033
902
}
1034
903
0 commit comments