@@ -4,8 +4,8 @@ use std::os::raw::c_int;
44
55use crate :: socket:: { self , get_loop, sockaddr_from_string, Emitter , HandleData , UvRefence } ;
66use crate :: util:: {
7- addr_to_string, buf_into_vec, error, get_err, resolve_libc_err, resolve_uv_err,
8- set_clo_exec , set_non_block, socket_addr_to_string, uv_err_msg,
7+ addr_to_string, buf_into_vec, error, get_err, resolve_libc_err, resolve_uv_err, set_clo_exec ,
8+ set_non_block, socket_addr_to_string, uv_err_msg,
99} ;
1010use libc:: { sockaddr, sockaddr_un, EAGAIN , EINTR , EINVAL , ENOBUFS , EWOULDBLOCK } ;
1111use napi:: { Env , JsBuffer , JsFunction , JsNumber , JsObject , JsString , JsUnknown , Ref , Result } ;
@@ -120,6 +120,11 @@ impl SeqpacketSocketWrap {
120120 Ok ( ( ) )
121121 }
122122
123+ #[ napi]
124+ pub fn state ( & self ) -> i32 {
125+ self . state as i32
126+ }
127+
123128 #[ napi]
124129 pub fn close ( & mut self ) -> Result < ( ) > {
125130 if self . state == State :: Closed {
@@ -534,15 +539,13 @@ impl SeqpacketSocketWrap {
534539
535540 let err = errno ( ) ;
536541
537- if ret == -1 && err != 0 {
538- if err == libc:: EINPROGRESS {
539- // not an error
540- } else if err == libc:: ECONNRESET || err == EINVAL {
542+ // libc::EINPROGRESS is not an error
543+ if ret == -1 && err != 0 && err != libc:: EINPROGRESS {
544+ if err == libc:: ECONNRESET || err == EINVAL {
541545 // TODO should we delay error?
542- resolve_libc_err ( ret) ?;
543- } else {
544- resolve_libc_err ( ret) ?;
545546 }
547+ self . close ( ) ?;
548+ resolve_libc_err ( ret) ?;
546549 }
547550
548551 unsafe {
@@ -600,11 +603,13 @@ impl SeqpacketSocketWrap {
600603}
601604
602605extern "C" fn on_close ( handle : * mut sys:: uv_handle_t ) {
603- unsafe { assert ! ( !( * handle) . data. is_null( ) , "unexpected null handle data" ) ; } ;
606+ unsafe {
607+ assert ! ( !( * handle) . data. is_null( ) , "unexpected null handle data" ) ;
608+ } ;
604609 unsafe {
605610 let mut data = Box :: from_raw ( ( * handle) . data as * mut HandleData ) ;
606611 data. unref ( ) . unwrap ( ) ;
607- Box :: from_raw ( handle) ;
612+ let _ = Box :: from_raw ( handle) ;
608613 } ;
609614}
610615
@@ -615,7 +620,9 @@ macro_rules! on_event {
615620 return ;
616621 }
617622
618- unsafe { assert!( !( * handle) . data. is_null( ) , "unexpected null handle data" ) ; } ;
623+ unsafe {
624+ assert!( !( * handle) . data. is_null( ) , "unexpected null handle data" ) ;
625+ } ;
619626 let data = unsafe { Box :: from_raw( ( * handle) . data as * mut HandleData ) } ;
620627 let wrap = data. inner_mut_ref:: <& mut SeqpacketSocketWrap >( ) . unwrap( ) ;
621628 wrap. $fn( status, events) ;
0 commit comments