@@ -5,6 +5,7 @@ pub use close::close;
55pub use connect:: connect;
66pub use listen:: listen;
77pub use nanosleep:: nanosleep;
8+ pub use recv:: recv;
89pub use send:: send;
910pub use shutdown:: shutdown;
1011pub use sleep:: sleep;
@@ -118,6 +119,69 @@ macro_rules! impl_nio_read {
118119 }
119120}
120121
122+ macro_rules! impl_nio_expected_read {
123+ ( $struct_name: ident, $trait_name: ident, $syscall: ident( $fd: ident : $fd_type: ty,
124+ $buf: ident : $buf_type: ty, $len: ident : $len_type: ty, $( $arg: ident : $arg_type: ty) ,* ) -> $result: ty ) => {
125+ #[ derive( Debug , Default ) ]
126+ struct $struct_name<I : $trait_name> {
127+ inner: I ,
128+ }
129+
130+ impl <I : $trait_name> $trait_name for $struct_name<I > {
131+ extern "C" fn $syscall(
132+ & self ,
133+ fn_ptr: Option <& extern "C" fn ( $fd_type, $buf_type, $len_type, $( $arg_type) ,* ) -> $result>,
134+ $fd: $fd_type,
135+ $buf: $buf_type,
136+ $len: $len_type,
137+ $( $arg: $arg_type) ,*
138+ ) -> $result {
139+ let blocking = $crate:: syscall:: common:: is_blocking( $fd) ;
140+ if blocking {
141+ $crate:: syscall:: common:: set_non_blocking( $fd) ;
142+ }
143+ let mut received = 0 ;
144+ let mut r = 0 ;
145+ while received < $len {
146+ r = self . inner. $syscall(
147+ fn_ptr,
148+ $fd,
149+ ( $buf as usize + received) as * mut c_void,
150+ $len - received,
151+ $( $arg, ) *
152+ ) ;
153+ if r != -1 {
154+ $crate:: syscall:: common:: reset_errno( ) ;
155+ received += r as size_t;
156+ if received >= $len || r == 0 {
157+ r = received as ssize_t;
158+ break ;
159+ }
160+ }
161+ let error_kind = std:: io:: Error :: last_os_error( ) . kind( ) ;
162+ if error_kind == std:: io:: ErrorKind :: WouldBlock {
163+ //wait read event
164+ if $crate:: net:: event_loop:: EventLoops :: wait_read_event(
165+ $fd,
166+ Some ( std:: time:: Duration :: from_millis( 10 ) ) ,
167+ )
168+ . is_err( )
169+ {
170+ break ;
171+ }
172+ } else if error_kind != std:: io:: ErrorKind :: Interrupted {
173+ break ;
174+ }
175+ }
176+ if blocking {
177+ $crate:: syscall:: common:: set_blocking( $fd) ;
178+ }
179+ r
180+ }
181+ }
182+ }
183+ }
184+
121185macro_rules! impl_nio_expected_write {
122186 ( $struct_name: ident, $trait_name: ident, $syscall: ident( $fd: ident : $fd_type: ty,
123187 $buf: ident : $buf_type: ty, $len: ident : $len_type: ty, $( $arg: ident : $arg_type: ty) ,* ) -> $result: ty ) => {
@@ -209,6 +273,7 @@ mod close;
209273mod connect;
210274mod listen;
211275mod nanosleep;
276+ mod recv;
212277mod send;
213278mod shutdown;
214279mod sleep;
0 commit comments