|
| 1 | +use crate::net::EventLoops; |
| 2 | +use crate::syscall::common::{is_blocking, reset_errno, set_blocking, set_errno, set_non_blocking}; |
| 3 | +use once_cell::sync::Lazy; |
| 4 | +use std::ffi::c_int; |
| 5 | +use std::io::Error; |
| 6 | +use windows_sys::Win32::Networking::WinSock::{getpeername, getsockopt, SO_ERROR, SOCKADDR, SOCKET, SOL_SOCKET, WSAEALREADY, WSAEINPROGRESS, WSAEINTR, WSAETIMEDOUT}; |
| 7 | + |
| 8 | +#[must_use] |
| 9 | +pub extern "system" fn connect( |
| 10 | + fn_ptr: Option<&extern "system" fn(SOCKET, *const SOCKADDR, c_int) -> c_int>, |
| 11 | + socket: SOCKET, |
| 12 | + address: *const SOCKADDR, |
| 13 | + len: c_int, |
| 14 | +) -> c_int { |
| 15 | + static CHAIN: Lazy<ConnectSyscallFacade<NioConnectSyscall<RawConnectSyscall>>> = |
| 16 | + Lazy::new(Default::default); |
| 17 | + CHAIN.connect(fn_ptr, socket, address, len) |
| 18 | +} |
| 19 | + |
| 20 | +trait ConnectSyscall { |
| 21 | + extern "system" fn connect( |
| 22 | + &self, |
| 23 | + fn_ptr: Option<&extern "system" fn(SOCKET, *const SOCKADDR, c_int) -> c_int>, |
| 24 | + fd: SOCKET, |
| 25 | + address: *const SOCKADDR, |
| 26 | + len: c_int, |
| 27 | + ) -> c_int; |
| 28 | +} |
| 29 | + |
| 30 | +impl_facade!(ConnectSyscallFacade, ConnectSyscall, |
| 31 | + connect(fd: SOCKET, address: *const SOCKADDR, len: c_int) -> c_int |
| 32 | +); |
| 33 | + |
| 34 | +#[repr(C)] |
| 35 | +#[derive(Debug, Default)] |
| 36 | +struct NioConnectSyscall<I: ConnectSyscall> { |
| 37 | + inner: I, |
| 38 | +} |
| 39 | + |
| 40 | +impl<I: ConnectSyscall> ConnectSyscall for NioConnectSyscall<I> { |
| 41 | + extern "system" fn connect( |
| 42 | + &self, |
| 43 | + fn_ptr: Option<&extern "system" fn(SOCKET, *const SOCKADDR, c_int) -> c_int>, |
| 44 | + fd: SOCKET, |
| 45 | + address: *const SOCKADDR, |
| 46 | + len: c_int, |
| 47 | + ) -> c_int { |
| 48 | + let blocking = is_blocking(fd); |
| 49 | + if blocking { |
| 50 | + set_non_blocking(fd); |
| 51 | + } |
| 52 | + let mut r = self.inner.connect(fn_ptr, fd, address, len); |
| 53 | + loop { |
| 54 | + if r == 0 { |
| 55 | + reset_errno(); |
| 56 | + break; |
| 57 | + } |
| 58 | + let errno = Error::last_os_error().raw_os_error(); |
| 59 | + if errno == Some(WSAEINPROGRESS) || errno == Some(WSAEALREADY) { |
| 60 | + //阻塞,直到写事件发生 |
| 61 | + if EventLoops::wait_write_event( |
| 62 | + fd as _, |
| 63 | + Some(crate::common::constants::SLICE) |
| 64 | + ).is_err() { |
| 65 | + break; |
| 66 | + } |
| 67 | + let mut err = 0; |
| 68 | + unsafe { |
| 69 | + let mut len: c_int = std::mem::zeroed(); |
| 70 | + r = getsockopt( |
| 71 | + fd, |
| 72 | + SOL_SOCKET, |
| 73 | + SO_ERROR, |
| 74 | + std::ptr::addr_of_mut!(err).cast::<u8>(), |
| 75 | + &mut len, |
| 76 | + ); |
| 77 | + } |
| 78 | + if r != 0 { |
| 79 | + r = -1; |
| 80 | + break; |
| 81 | + } |
| 82 | + if err != 0 { |
| 83 | + set_errno(err); |
| 84 | + r = -1; |
| 85 | + break; |
| 86 | + }; |
| 87 | + unsafe { |
| 88 | + let mut address = std::mem::zeroed(); |
| 89 | + let mut address_len = std::mem::zeroed(); |
| 90 | + r = getpeername(fd, &mut address, &mut address_len); |
| 91 | + } |
| 92 | + } else if errno != Some(WSAEINTR) { |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + if r == -1 && Error::last_os_error().raw_os_error() == Some(WSAETIMEDOUT) { |
| 97 | + set_errno(WSAEINPROGRESS.try_into().expect("overflow")); |
| 98 | + } |
| 99 | + if blocking { |
| 100 | + set_blocking(fd); |
| 101 | + } |
| 102 | + r |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl_raw!(RawConnectSyscall, ConnectSyscall, windows_sys::Win32::Networking::WinSock, |
| 107 | + connect(fd: SOCKET, address: *const SOCKADDR, len: c_int) -> c_int |
| 108 | +); |
0 commit comments