Skip to content

Commit 37e7853

Browse files
committed
test accept IOCP
1 parent 054174b commit 37e7853

File tree

8 files changed

+104
-64
lines changed

8 files changed

+104
-64
lines changed

core/src/net/operator/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#[allow(clippy::too_many_arguments)]
21
#[cfg(all(target_os = "linux", feature = "io_uring"))]
32
mod linux;
43
#[cfg(all(target_os = "linux", feature = "io_uring"))]
54
pub(crate) use linux::*;
65

7-
#[allow(non_snake_case, clippy::too_many_arguments)]
6+
#[allow(non_snake_case)]
87
#[cfg(all(windows, feature = "iocp"))]
98
mod windows;
109
#[cfg(all(windows, feature = "iocp"))]

core/src/net/operator/windows.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,47 @@ impl<'o> Operator<'o> {
346346
Ok(())
347347
}
348348

349+
pub(crate) fn write(
350+
&self,
351+
user_data: usize,
352+
fd: c_int,
353+
buf: *const c_void,
354+
count: c_uint,
355+
) -> std::io::Result<()> {
356+
self.add_handle(fd as HANDLE)?;
357+
unsafe {
358+
if SOCKET_CONTEXT.get(&(fd as SOCKET)).is_some() {
359+
let overlapped: &'o mut Overlapped = Box::leak(Box::default());
360+
overlapped.from_fd = fd as _;
361+
overlapped.token = user_data;
362+
overlapped.syscall = Syscall::write;
363+
let buf = [WSABUF {
364+
len: count,
365+
buf: buf.cast_mut().cast(),
366+
}];
367+
if WSASend(
368+
fd as _,
369+
buf.as_ptr(),
370+
buf.len().try_into().expect("len overflow"),
371+
std::ptr::null_mut(),
372+
0,
373+
std::ptr::from_mut(overlapped).cast(),
374+
None,
375+
) == SOCKET_ERROR
376+
&& WSA_IO_PENDING != WSAGetLastError()
377+
{
378+
return Err(Error::new(
379+
ErrorKind::WouldBlock,
380+
"add write operation failed",
381+
));
382+
}
383+
eprintln!("add write operation Overlapped:{overlapped}");
384+
return Ok(());
385+
}
386+
}
387+
todo!()
388+
}
389+
349390
pub(crate) fn WSASend(
350391
&self,
351392
user_data: usize,

core/src/syscall/windows/WSARecv.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ pub extern "system" fn WSARecv(
2727
lpoverlapped: *mut OVERLAPPED,
2828
lpcompletionroutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE,
2929
) -> c_int {
30-
cfg_if::cfg_if! {
31-
if #[cfg(all(windows, feature = "iocp"))] {
32-
static CHAIN: Lazy<
33-
WSARecvSyscallFacade<IocpWSARecvSyscall<NioWSARecvSyscall<RawWSARecvSyscall>>>
34-
> = Lazy::new(Default::default);
35-
} else {
36-
static CHAIN: Lazy<WSARecvSyscallFacade<NioWSARecvSyscall<RawWSARecvSyscall>>> =
37-
Lazy::new(Default::default);
38-
}
39-
}
30+
// cfg_if::cfg_if! {
31+
// if #[cfg(all(windows, feature = "iocp"))] {
32+
// static CHAIN: Lazy<
33+
// WSARecvSyscallFacade<IocpWSARecvSyscall<NioWSARecvSyscall<RawWSARecvSyscall>>>
34+
// > = Lazy::new(Default::default);
35+
// } else {
36+
// static CHAIN: Lazy<WSARecvSyscallFacade<NioWSARecvSyscall<RawWSARecvSyscall>>> =
37+
// Lazy::new(Default::default);
38+
// }
39+
// }
40+
static CHAIN: Lazy<WSARecvSyscallFacade<NioWSARecvSyscall<RawWSARecvSyscall>>> =
41+
Lazy::new(Default::default);
4042
CHAIN.WSARecv(
4143
fn_ptr,
4244
fd,

core/src/syscall/windows/WSASend.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ pub extern "system" fn WSASend(
2727
lpoverlapped: *mut OVERLAPPED,
2828
lpcompletionroutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE,
2929
) -> c_int {
30-
cfg_if::cfg_if! {
31-
if #[cfg(all(windows, feature = "iocp"))] {
32-
static CHAIN: Lazy<
33-
WSASendSyscallFacade<IocpWSASendSyscall<NioWSASendSyscall<RawWSASendSyscall>>>
34-
> = Lazy::new(Default::default);
35-
} else {
36-
static CHAIN: Lazy<WSASendSyscallFacade<NioWSASendSyscall<RawWSASendSyscall>>> =
37-
Lazy::new(Default::default);
38-
}
39-
}
30+
// cfg_if::cfg_if! {
31+
// if #[cfg(all(windows, feature = "iocp"))] {
32+
// static CHAIN: Lazy<
33+
// WSASendSyscallFacade<IocpWSASendSyscall<NioWSASendSyscall<RawWSASendSyscall>>>
34+
// > = Lazy::new(Default::default);
35+
// } else {
36+
// static CHAIN: Lazy<WSASendSyscallFacade<NioWSASendSyscall<RawWSASendSyscall>>> =
37+
// Lazy::new(Default::default);
38+
// }
39+
// }
40+
static CHAIN: Lazy<WSASendSyscallFacade<NioWSASendSyscall<RawWSASendSyscall>>> =
41+
Lazy::new(Default::default);
4042
CHAIN.WSASend(
4143
fn_ptr,
4244
fd,

core/src/syscall/windows/accept.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ pub extern "system" fn accept(
99
address: *mut SOCKADDR,
1010
address_len: *mut c_int,
1111
) -> SOCKET {
12-
// cfg_if::cfg_if! {
13-
// if #[cfg(feature = "iocp")] {
14-
// static CHAIN: Lazy<
15-
// AcceptSyscallFacade<IocpAcceptSyscall<NioAcceptSyscall<RawAcceptSyscall>>>
16-
// > = Lazy::new(Default::default);
17-
// } else {
18-
// static CHAIN: Lazy<AcceptSyscallFacade<NioAcceptSyscall<RawAcceptSyscall>>> =
19-
// Lazy::new(Default::default);
20-
// }
21-
// }
22-
static CHAIN: Lazy<AcceptSyscallFacade<NioAcceptSyscall<RawAcceptSyscall>>> =
23-
Lazy::new(Default::default);
12+
cfg_if::cfg_if! {
13+
if #[cfg(feature = "iocp")] {
14+
static CHAIN: Lazy<
15+
AcceptSyscallFacade<IocpAcceptSyscall<NioAcceptSyscall<RawAcceptSyscall>>>
16+
> = Lazy::new(Default::default);
17+
} else {
18+
static CHAIN: Lazy<AcceptSyscallFacade<NioAcceptSyscall<RawAcceptSyscall>>> =
19+
Lazy::new(Default::default);
20+
}
21+
}
2422
CHAIN.accept(fn_ptr, fd, address, address_len)
2523
}
2624

@@ -38,9 +36,9 @@ impl_facade!(AcceptSyscallFacade, AcceptSyscall,
3836
accept(fd: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> SOCKET
3937
);
4038

41-
// impl_iocp!(IocpAcceptSyscall, AcceptSyscall,
42-
// accept(fd: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> SOCKET
43-
// );
39+
impl_iocp!(IocpAcceptSyscall, AcceptSyscall,
40+
accept(fd: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> SOCKET
41+
);
4442

4543
impl_nio_read!(NioAcceptSyscall, AcceptSyscall,
4644
accept(fd: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> SOCKET

core/src/syscall/windows/recv.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ pub extern "system" fn recv(
1111
len: c_int,
1212
flags: SEND_RECV_FLAGS,
1313
) -> c_int {
14-
cfg_if::cfg_if! {
15-
if #[cfg(all(windows, feature = "iocp"))] {
16-
static CHAIN: Lazy<
17-
RecvSyscallFacade<IocpRecvSyscall<NioRecvSyscall<RawRecvSyscall>>>
18-
> = Lazy::new(Default::default);
19-
} else {
20-
static CHAIN: Lazy<RecvSyscallFacade<NioRecvSyscall<RawRecvSyscall>>> =
21-
Lazy::new(Default::default);
22-
}
23-
}
14+
// cfg_if::cfg_if! {
15+
// if #[cfg(all(windows, feature = "iocp"))] {
16+
// static CHAIN: Lazy<
17+
// RecvSyscallFacade<IocpRecvSyscall<NioRecvSyscall<RawRecvSyscall>>>
18+
// > = Lazy::new(Default::default);
19+
// } else {
20+
// static CHAIN: Lazy<RecvSyscallFacade<NioRecvSyscall<RawRecvSyscall>>> =
21+
// Lazy::new(Default::default);
22+
// }
23+
// }
24+
static CHAIN: Lazy<RecvSyscallFacade<NioRecvSyscall<RawRecvSyscall>>> =
25+
Lazy::new(Default::default);
2426
CHAIN.recv(fn_ptr, fd, buf, len, flags)
2527
}
2628

core/src/syscall/windows/send.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ pub extern "system" fn send(
1111
len: c_int,
1212
flags: SEND_RECV_FLAGS,
1313
) -> c_int {
14-
cfg_if::cfg_if! {
15-
if #[cfg(all(windows, feature = "iocp"))] {
16-
static CHAIN: Lazy<
17-
SendSyscallFacade<IocpSendSyscall<NioSendSyscall<RawSendSyscall>>>
18-
> = Lazy::new(Default::default);
19-
} else {
20-
static CHAIN: Lazy<SendSyscallFacade<NioSendSyscall<RawSendSyscall>>> =
21-
Lazy::new(Default::default);
22-
}
23-
}
14+
// cfg_if::cfg_if! {
15+
// if #[cfg(all(windows, feature = "iocp"))] {
16+
// static CHAIN: Lazy<
17+
// SendSyscallFacade<IocpSendSyscall<NioSendSyscall<RawSendSyscall>>>
18+
// > = Lazy::new(Default::default);
19+
// } else {
20+
// static CHAIN: Lazy<SendSyscallFacade<NioSendSyscall<RawSendSyscall>>> =
21+
// Lazy::new(Default::default);
22+
// }
23+
// }
24+
static CHAIN: Lazy<SendSyscallFacade<NioSendSyscall<RawSendSyscall>>> =
25+
Lazy::new(Default::default);
2426
CHAIN.send(fn_ptr, fd, buf, len, flags)
2527
}
2628

hook/src/syscall/windows.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ unsafe fn attach() -> std::io::Result<()> {
143143
nfds: c_uint,
144144
timeout: c_int
145145
) -> c_int);
146-
#[cfg(not(feature = "iocp"))]
147146
impl_hook!("kernel32.dll", SLEEP, Sleep(dw_milliseconds: u32) -> ());
148147
impl_hook!("kernel32.dll", CREATEFILEW, CreateFileW(
149148
lpfilename: PCWSTR,
@@ -160,18 +159,13 @@ unsafe fn attach() -> std::io::Result<()> {
160159
lpnewfilepointer : *mut c_longlong,
161160
dwmovemethod : SET_FILE_POINTER_MOVE_METHOD
162161
) -> BOOL);
163-
// NOTE: unhook WaitOnAddress/connect due to stack overflow or bug
162+
// NOTE: unhook WaitOnAddress due to stack overflow or bug
164163
// impl_hook!("api-ms-win-core-synch-l1-2-0.dll", WAITONADDRESS, WaitOnAddress(
165164
// address: *const c_void,
166165
// compareaddress: *const c_void,
167166
// addresssize: usize,
168167
// dwmilliseconds: c_uint
169168
// ) -> BOOL);
170-
// impl_hook!("ws2_32.dll", CONNECT, connect(
171-
// fd: SOCKET,
172-
// address: *const SOCKADDR,
173-
// len: c_int
174-
// ) -> c_int);
175169
// Enable the hook
176170
minhook::MinHook::enable_all_hooks()
177171
.map_err(|_| Error::new(ErrorKind::Other, "init all hooks failed !"))

0 commit comments

Comments
 (0)