@@ -33,7 +33,7 @@ macro_rules! impl_facade {
3333 $crate:: error!( "{} change to running state failed !" , co. name( ) ) ;
3434 }
3535 }
36- $crate:: info!( "exit syscall {}" , syscall) ;
36+ $crate:: info!( "exit syscall {} {:?} {} " , syscall, r , std :: io :: Error :: last_os_error ( ) ) ;
3737 r
3838 }
3939 }
@@ -131,8 +131,9 @@ macro_rules! impl_nio_read {
131131 $crate:: syscall:: common:: set_non_blocking( $fd) ;
132132 }
133133 let start_time = $crate:: common:: now( ) ;
134- let mut r;
135- loop {
134+ let mut left_time = $crate:: syscall:: common:: recv_time_limit( $fd) ;
135+ let mut r = -1 ;
136+ while left_time > 0 {
136137 r = self . inner. $syscall( fn_ptr, $fd, $( $arg, ) * ) ;
137138 if r != -1 {
138139 $crate:: syscall:: common:: reset_errno( ) ;
@@ -141,9 +142,10 @@ macro_rules! impl_nio_read {
141142 let error_kind = std:: io:: Error :: last_os_error( ) . kind( ) ;
142143 if error_kind == std:: io:: ErrorKind :: WouldBlock {
143144 //wait read event
144- let wait_time = std :: time :: Duration :: from_nanos ( start_time
145+ left_time = start_time
145146 . saturating_add( $crate:: syscall:: common:: recv_time_limit( $fd) )
146- . saturating_sub( $crate:: common:: now( ) ) )
147+ . saturating_sub( $crate:: common:: now( ) ) ;
148+ let wait_time = std:: time:: Duration :: from_nanos( left_time)
147149 . min( $crate:: common:: constants:: SLICE ) ;
148150 if $crate:: net:: EventLoops :: wait_read_event(
149151 $fd,
@@ -187,9 +189,10 @@ macro_rules! impl_nio_read_buf {
187189 $crate:: syscall:: common:: set_non_blocking( $fd) ;
188190 }
189191 let start_time = $crate:: common:: now( ) ;
192+ let mut left_time = $crate:: syscall:: common:: recv_time_limit( $fd) ;
190193 let mut received = 0 ;
191194 let mut r = 0 ;
192- while received < $len {
195+ while received < $len && left_time > 0 {
193196 r = self . inner. $syscall(
194197 fn_ptr,
195198 $fd,
@@ -208,9 +211,10 @@ macro_rules! impl_nio_read_buf {
208211 let error_kind = std:: io:: Error :: last_os_error( ) . kind( ) ;
209212 if error_kind == std:: io:: ErrorKind :: WouldBlock {
210213 //wait read event
211- let wait_time = std :: time :: Duration :: from_nanos ( start_time
214+ left_time = start_time
212215 . saturating_add( $crate:: syscall:: common:: recv_time_limit( $fd) )
213- . saturating_sub( $crate:: common:: now( ) ) )
216+ . saturating_sub( $crate:: common:: now( ) ) ;
217+ let wait_time = std:: time:: Duration :: from_nanos( left_time)
214218 . min( $crate:: common:: constants:: SLICE ) ;
215219 if $crate:: net:: EventLoops :: wait_read_event(
216220 $fd,
@@ -253,8 +257,15 @@ macro_rules! impl_nio_read_iovec {
253257 if blocking {
254258 $crate:: syscall:: common:: set_non_blocking( $fd) ;
255259 }
256- let vec = unsafe { Vec :: from_raw_parts( $iov. cast_mut( ) , $iovcnt as usize , $iovcnt as usize ) } ;
257260 let start_time = $crate:: common:: now( ) ;
261+ let mut left_time = $crate:: syscall:: common:: recv_time_limit( $fd) ;
262+ let vec = unsafe {
263+ Vec :: from_raw_parts(
264+ $iov. cast_mut( ) ,
265+ $iovcnt as usize ,
266+ $iovcnt as usize
267+ )
268+ } ;
258269 let mut length = 0 ;
259270 let mut received = 0usize ;
260271 let mut r = 0 ;
@@ -270,7 +281,7 @@ macro_rules! impl_nio_read_iovec {
270281 for i in vec. iter( ) . skip( index) {
271282 arg. push( * i) ;
272283 }
273- while received < length {
284+ while received < length && left_time > 0 {
274285 if 0 != offset {
275286 arg[ 0 ] = libc:: iovec {
276287 iov_base: ( arg[ 0 ] . iov_base as usize + offset) as * mut std:: ffi:: c_void,
@@ -304,9 +315,10 @@ macro_rules! impl_nio_read_iovec {
304315 let error_kind = std:: io:: Error :: last_os_error( ) . kind( ) ;
305316 if error_kind == std:: io:: ErrorKind :: WouldBlock {
306317 //wait read event
307- let wait_time = std :: time :: Duration :: from_nanos ( start_time
318+ left_time = start_time
308319 . saturating_add( $crate:: syscall:: common:: recv_time_limit( $fd) )
309- . saturating_sub( $crate:: common:: now( ) ) )
320+ . saturating_sub( $crate:: common:: now( ) ) ;
321+ let wait_time = std:: time:: Duration :: from_nanos( left_time)
310322 . min( $crate:: common:: constants:: SLICE ) ;
311323 if $crate:: net:: EventLoops :: wait_read_event(
312324 $fd,
@@ -363,9 +375,10 @@ macro_rules! impl_nio_write_buf {
363375 $crate:: syscall:: common:: set_non_blocking( $fd) ;
364376 }
365377 let start_time = $crate:: common:: now( ) ;
378+ let mut left_time = $crate:: syscall:: common:: send_time_limit( $fd) ;
366379 let mut sent = 0 ;
367380 let mut r = 0 ;
368- while sent < $len {
381+ while sent < $len && left_time > 0 {
369382 r = self . inner. $syscall(
370383 fn_ptr,
371384 $fd,
@@ -384,9 +397,10 @@ macro_rules! impl_nio_write_buf {
384397 let error_kind = std:: io:: Error :: last_os_error( ) . kind( ) ;
385398 if error_kind == std:: io:: ErrorKind :: WouldBlock {
386399 //wait write event
387- let wait_time = std :: time :: Duration :: from_nanos ( start_time
400+ left_time = start_time
388401 . saturating_add( $crate:: syscall:: common:: send_time_limit( $fd) )
389- . saturating_sub( $crate:: common:: now( ) ) )
402+ . saturating_sub( $crate:: common:: now( ) ) ;
403+ let wait_time = std:: time:: Duration :: from_nanos( left_time)
390404 . min( $crate:: common:: constants:: SLICE ) ;
391405 if $crate:: net:: EventLoops :: wait_write_event(
392406 $fd,
@@ -431,8 +445,15 @@ macro_rules! impl_nio_write_iovec {
431445 if blocking {
432446 $crate:: syscall:: common:: set_non_blocking( $fd) ;
433447 }
434- let vec = unsafe { Vec :: from_raw_parts( $iov. cast_mut( ) , $iovcnt as usize , $iovcnt as usize ) } ;
435448 let start_time = $crate:: common:: now( ) ;
449+ let mut left_time = $crate:: syscall:: common:: send_time_limit( $fd) ;
450+ let vec = unsafe {
451+ Vec :: from_raw_parts(
452+ $iov. cast_mut( ) ,
453+ $iovcnt as usize ,
454+ $iovcnt as usize
455+ )
456+ } ;
436457 let mut length = 0 ;
437458 let mut sent = 0usize ;
438459 let mut r = 0 ;
@@ -448,7 +469,7 @@ macro_rules! impl_nio_write_iovec {
448469 for i in vec. iter( ) . skip( index) {
449470 arg. push( * i) ;
450471 }
451- while sent < length {
472+ while sent < length && left_time > 0 {
452473 if 0 != offset {
453474 arg[ 0 ] = libc:: iovec {
454475 iov_base: ( arg[ 0 ] . iov_base as usize + offset) as * mut std:: ffi:: c_void,
@@ -476,9 +497,10 @@ macro_rules! impl_nio_write_iovec {
476497 let error_kind = std:: io:: Error :: last_os_error( ) . kind( ) ;
477498 if error_kind == std:: io:: ErrorKind :: WouldBlock {
478499 //wait write event
479- let wait_time = std :: time :: Duration :: from_nanos ( start_time
500+ left_time = start_time
480501 . saturating_add( $crate:: syscall:: common:: send_time_limit( $fd) )
481- . saturating_sub( $crate:: common:: now( ) ) )
502+ . saturating_sub( $crate:: common:: now( ) ) ;
503+ let wait_time = std:: time:: Duration :: from_nanos( left_time)
482504 . min( $crate:: common:: constants:: SLICE ) ;
483505 if $crate:: net:: EventLoops :: wait_write_event(
484506 $fd,
@@ -670,16 +692,21 @@ pub extern "C" fn send_time_limit(fd: c_int) -> u64 {
670692 || unsafe {
671693 let mut tv: libc:: timeval = std:: mem:: zeroed ( ) ;
672694 let mut len = size_of :: < libc:: timeval > ( ) as libc:: socklen_t ;
673- assert_eq ! (
674- 0 ,
675- libc:: getsockopt(
676- fd,
677- libc:: SOL_SOCKET ,
678- libc:: SO_SNDTIMEO ,
679- std:: ptr:: from_mut( & mut tv) . cast( ) ,
680- & mut len,
681- )
682- ) ;
695+ if libc:: getsockopt (
696+ fd,
697+ libc:: SOL_SOCKET ,
698+ libc:: SO_SNDTIMEO ,
699+ std:: ptr:: from_mut ( & mut tv) . cast ( ) ,
700+ & mut len,
701+ ) == -1
702+ {
703+ let error = std:: io:: Error :: last_os_error ( ) ;
704+ if Some ( libc:: ENOTSOCK ) == error. raw_os_error ( ) {
705+ // not a socket
706+ return u64:: MAX ;
707+ }
708+ panic ! ( "getsockopt failed: {error}" ) ;
709+ }
683710 let mut time_limit = ( tv. tv_sec as u64 )
684711 . saturating_mul ( 1_000_000_000 )
685712 . saturating_add ( ( tv. tv_usec as u64 ) . saturating_mul ( 1_000 ) ) ;
@@ -700,16 +727,21 @@ pub extern "C" fn recv_time_limit(fd: c_int) -> u64 {
700727 || unsafe {
701728 let mut tv: libc:: timeval = std:: mem:: zeroed ( ) ;
702729 let mut len = size_of :: < libc:: timeval > ( ) as libc:: socklen_t ;
703- assert_eq ! (
704- 0 ,
705- libc:: getsockopt(
706- fd,
707- libc:: SOL_SOCKET ,
708- libc:: SO_RCVTIMEO ,
709- std:: ptr:: from_mut( & mut tv) . cast( ) ,
710- & mut len,
711- )
712- ) ;
730+ if libc:: getsockopt (
731+ fd,
732+ libc:: SOL_SOCKET ,
733+ libc:: SO_RCVTIMEO ,
734+ std:: ptr:: from_mut ( & mut tv) . cast ( ) ,
735+ & mut len,
736+ ) == -1
737+ {
738+ let error = std:: io:: Error :: last_os_error ( ) ;
739+ if Some ( libc:: ENOTSOCK ) == error. raw_os_error ( ) {
740+ // not a socket
741+ return u64:: MAX ;
742+ }
743+ panic ! ( "getsockopt failed: {error}" ) ;
744+ }
713745 let mut time_limit = ( tv. tv_sec as u64 )
714746 . saturating_mul ( 1_000_000_000 )
715747 . saturating_add ( ( tv. tv_usec as u64 ) . saturating_mul ( 1_000 ) ) ;
0 commit comments