@@ -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,9 @@ 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 { Vec :: from_raw_parts( $iov. cast_mut( ) , $iovcnt as usize , $iovcnt as usize ) } ;
258263 let mut length = 0 ;
259264 let mut received = 0usize ;
260265 let mut r = 0 ;
@@ -270,7 +275,7 @@ macro_rules! impl_nio_read_iovec {
270275 for i in vec. iter( ) . skip( index) {
271276 arg. push( * i) ;
272277 }
273- while received < length {
278+ while received < length && left_time > 0 {
274279 if 0 != offset {
275280 arg[ 0 ] = libc:: iovec {
276281 iov_base: ( arg[ 0 ] . iov_base as usize + offset) as * mut std:: ffi:: c_void,
@@ -304,9 +309,10 @@ macro_rules! impl_nio_read_iovec {
304309 let error_kind = std:: io:: Error :: last_os_error( ) . kind( ) ;
305310 if error_kind == std:: io:: ErrorKind :: WouldBlock {
306311 //wait read event
307- let wait_time = std :: time :: Duration :: from_nanos ( start_time
312+ left_time = start_time
308313 . saturating_add( $crate:: syscall:: common:: recv_time_limit( $fd) )
309- . saturating_sub( $crate:: common:: now( ) ) )
314+ . saturating_sub( $crate:: common:: now( ) ) ;
315+ let wait_time = std:: time:: Duration :: from_nanos( left_time)
310316 . min( $crate:: common:: constants:: SLICE ) ;
311317 if $crate:: net:: EventLoops :: wait_read_event(
312318 $fd,
@@ -363,9 +369,10 @@ macro_rules! impl_nio_write_buf {
363369 $crate:: syscall:: common:: set_non_blocking( $fd) ;
364370 }
365371 let start_time = $crate:: common:: now( ) ;
372+ let mut left_time = $crate:: syscall:: common:: send_time_limit( $fd) ;
366373 let mut sent = 0 ;
367374 let mut r = 0 ;
368- while sent < $len {
375+ while sent < $len && left_time > 0 {
369376 r = self . inner. $syscall(
370377 fn_ptr,
371378 $fd,
@@ -384,9 +391,10 @@ macro_rules! impl_nio_write_buf {
384391 let error_kind = std:: io:: Error :: last_os_error( ) . kind( ) ;
385392 if error_kind == std:: io:: ErrorKind :: WouldBlock {
386393 //wait write event
387- let wait_time = std :: time :: Duration :: from_nanos ( start_time
394+ left_time = start_time
388395 . saturating_add( $crate:: syscall:: common:: send_time_limit( $fd) )
389- . saturating_sub( $crate:: common:: now( ) ) )
396+ . saturating_sub( $crate:: common:: now( ) ) ;
397+ let wait_time = std:: time:: Duration :: from_nanos( left_time)
390398 . min( $crate:: common:: constants:: SLICE ) ;
391399 if $crate:: net:: EventLoops :: wait_write_event(
392400 $fd,
@@ -431,8 +439,9 @@ macro_rules! impl_nio_write_iovec {
431439 if blocking {
432440 $crate:: syscall:: common:: set_non_blocking( $fd) ;
433441 }
434- let vec = unsafe { Vec :: from_raw_parts( $iov. cast_mut( ) , $iovcnt as usize , $iovcnt as usize ) } ;
435442 let start_time = $crate:: common:: now( ) ;
443+ let mut left_time = $crate:: syscall:: common:: send_time_limit( $fd) ;
444+ let vec = unsafe { Vec :: from_raw_parts( $iov. cast_mut( ) , $iovcnt as usize , $iovcnt as usize ) } ;
436445 let mut length = 0 ;
437446 let mut sent = 0usize ;
438447 let mut r = 0 ;
@@ -448,7 +457,7 @@ macro_rules! impl_nio_write_iovec {
448457 for i in vec. iter( ) . skip( index) {
449458 arg. push( * i) ;
450459 }
451- while sent < length {
460+ while sent < length && left_time > 0 {
452461 if 0 != offset {
453462 arg[ 0 ] = libc:: iovec {
454463 iov_base: ( arg[ 0 ] . iov_base as usize + offset) as * mut std:: ffi:: c_void,
@@ -476,9 +485,10 @@ macro_rules! impl_nio_write_iovec {
476485 let error_kind = std:: io:: Error :: last_os_error( ) . kind( ) ;
477486 if error_kind == std:: io:: ErrorKind :: WouldBlock {
478487 //wait write event
479- let wait_time = std :: time :: Duration :: from_nanos ( start_time
488+ left_time = start_time
480489 . saturating_add( $crate:: syscall:: common:: send_time_limit( $fd) )
481- . saturating_sub( $crate:: common:: now( ) ) )
490+ . saturating_sub( $crate:: common:: now( ) ) ;
491+ let wait_time = std:: time:: Duration :: from_nanos( left_time)
482492 . min( $crate:: common:: constants:: SLICE ) ;
483493 if $crate:: net:: EventLoops :: wait_write_event(
484494 $fd,
@@ -670,16 +680,21 @@ pub extern "C" fn send_time_limit(fd: c_int) -> u64 {
670680 || unsafe {
671681 let mut tv: libc:: timeval = std:: mem:: zeroed ( ) ;
672682 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- ) ;
683+ if libc:: getsockopt (
684+ fd,
685+ libc:: SOL_SOCKET ,
686+ libc:: SO_SNDTIMEO ,
687+ std:: ptr:: from_mut ( & mut tv) . cast ( ) ,
688+ & mut len,
689+ ) == -1
690+ {
691+ let error = std:: io:: Error :: last_os_error ( ) ;
692+ if Some ( libc:: ENOTSOCK ) == error. raw_os_error ( ) {
693+ // not a socket
694+ return u64:: MAX ;
695+ }
696+ panic ! ( "getsockopt failed: {error}" ) ;
697+ }
683698 let mut time_limit = ( tv. tv_sec as u64 )
684699 . saturating_mul ( 1_000_000_000 )
685700 . saturating_add ( ( tv. tv_usec as u64 ) . saturating_mul ( 1_000 ) ) ;
@@ -700,16 +715,21 @@ pub extern "C" fn recv_time_limit(fd: c_int) -> u64 {
700715 || unsafe {
701716 let mut tv: libc:: timeval = std:: mem:: zeroed ( ) ;
702717 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- ) ;
718+ if libc:: getsockopt (
719+ fd,
720+ libc:: SOL_SOCKET ,
721+ libc:: SO_RCVTIMEO ,
722+ std:: ptr:: from_mut ( & mut tv) . cast ( ) ,
723+ & mut len,
724+ ) == -1
725+ {
726+ let error = std:: io:: Error :: last_os_error ( ) ;
727+ if Some ( libc:: ENOTSOCK ) == error. raw_os_error ( ) {
728+ // not a socket
729+ return u64:: MAX ;
730+ }
731+ panic ! ( "getsockopt failed: {error}" ) ;
732+ }
713733 let mut time_limit = ( tv. tv_sec as u64 )
714734 . saturating_mul ( 1_000_000_000 )
715735 . saturating_add ( ( tv. tv_usec as u64 ) . saturating_mul ( 1_000 ) ) ;
0 commit comments