Skip to content

Commit a09e2c1

Browse files
committed
simplify code
1 parent 4e3857a commit a09e2c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+486
-1048
lines changed

core/src/net/operator/windows/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use windows_sys::Win32::Networking::WinSock::{
1414
getsockopt, setsockopt, AcceptEx, WSAGetLastError, WSARecv, WSASend, WSASocketW,
1515
INVALID_SOCKET, LPCONDITIONPROC, LPWSAOVERLAPPED_COMPLETION_ROUTINE, SEND_RECV_FLAGS, SOCKADDR,
1616
SOCKADDR_IN, SOCKET, SOCKET_ERROR, SOL_SOCKET, SO_PROTOCOL_INFO, SO_UPDATE_ACCEPT_CONTEXT,
17-
WSABUF, WSAPROTOCOL_INFOW, WSA_FLAG_OVERLAPPED, WSA_IO_PENDING,
17+
WSABUF, WSAEINPROGRESS, WSAENETDOWN, WSAPROTOCOL_INFOW, WSA_FLAG_OVERLAPPED, WSA_IO_PENDING,
1818
};
1919
use windows_sys::Win32::Storage::FileSystem::SetFileCompletionNotificationModes;
2020
use windows_sys::Win32::System::WindowsProgramming::FILE_SKIP_SET_EVENT_ON_HANDLE;
@@ -159,7 +159,6 @@ impl<'o> Operator<'o> {
159159
for entry in entries {
160160
let mut cqe = *Box::from_raw(entry.lpOverlapped.cast::<Overlapped>());
161161
// resolve completed read/write tasks
162-
// todo refactor IOCP impl
163162
cqe.result = match cqe.syscall_name {
164163
SyscallName::accept => {
165164
if setsockopt(
@@ -172,7 +171,7 @@ impl<'o> Operator<'o> {
172171
{
173172
cqe.socket.try_into().expect("result overflow")
174173
} else {
175-
-c_longlong::from(windows_sys::Win32::Foundation::GetLastError())
174+
-c_longlong::from(WSAENETDOWN)
176175
}
177176
}
178177
SyscallName::recv
@@ -183,7 +182,7 @@ impl<'o> Operator<'o> {
183182
if r > 0 {
184183
r
185184
} else {
186-
-c_longlong::from(windows_sys::Win32::Foundation::GetLastError())
185+
-c_longlong::from(WSAEINPROGRESS)
187186
}
188187
}
189188
_ => panic!("unsupported"),
@@ -273,6 +272,7 @@ impl<'o> Operator<'o> {
273272
overlapped.token = user_data;
274273
overlapped.syscall_name = syscall_name;
275274
overlapped.socket = socket;
275+
overlapped.result = -c_longlong::from(WSAENETDOWN);
276276
let mut buf: Vec<u8> = Vec::with_capacity(size as usize * 2);
277277
while AcceptEx(
278278
fd,
@@ -364,6 +364,7 @@ impl<'o> Operator<'o> {
364364
overlapped.from_fd = fd;
365365
overlapped.token = user_data;
366366
overlapped.syscall_name = syscall_name;
367+
overlapped.result = -c_longlong::from(WSAEINPROGRESS);
367368
if WSARecv(
368369
fd,
369370
buf,
@@ -457,6 +458,7 @@ impl<'o> Operator<'o> {
457458
overlapped.from_fd = fd;
458459
overlapped.token = user_data;
459460
overlapped.syscall_name = syscall_name;
461+
overlapped.result = -c_longlong::from(WSAEINPROGRESS);
460462
if WSASend(
461463
fd,
462464
buf,

core/src/syscall/unix/accept.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
11
use libc::{sockaddr, socklen_t};
2-
use once_cell::sync::Lazy;
32
use std::ffi::c_int;
43

5-
#[must_use]
6-
pub extern "C" fn accept(
7-
fn_ptr: Option<&extern "C" fn(c_int, *mut sockaddr, *mut socklen_t) -> c_int>,
8-
fd: c_int,
9-
address: *mut sockaddr,
10-
address_len: *mut socklen_t,
11-
) -> c_int {
12-
cfg_if::cfg_if! {
13-
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
14-
static CHAIN: Lazy<
15-
AcceptSyscallFacade<IoUringAcceptSyscall<NioAcceptSyscall<RawAcceptSyscall>>>
16-
> = Lazy::new(Default::default);
17-
} else {
18-
static CHAIN: Lazy<AcceptSyscallFacade<NioAcceptSyscall<RawAcceptSyscall>>> =
19-
Lazy::new(Default::default);
20-
}
21-
}
22-
CHAIN.accept(fn_ptr, fd, address, address_len)
23-
}
24-
254
trait AcceptSyscall {
265
extern "C" fn accept(
276
&self,
@@ -32,6 +11,10 @@ trait AcceptSyscall {
3211
) -> c_int;
3312
}
3413

14+
impl_syscall!(AcceptSyscallFacade, IoUringAcceptSyscall, NioAcceptSyscall, RawAcceptSyscall,
15+
accept(fd: c_int, address: *mut sockaddr, address_len: *mut socklen_t) -> c_int
16+
);
17+
3518
impl_facade!(AcceptSyscallFacade, AcceptSyscall,
3619
accept(fd: c_int, address: *mut sockaddr, address_len: *mut socklen_t) -> c_int
3720
);

core/src/syscall/unix/accept4.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
11
use libc::{sockaddr, socklen_t};
2-
use once_cell::sync::Lazy;
32
use std::ffi::c_int;
43

5-
#[must_use]
6-
pub extern "C" fn accept4(
7-
fn_ptr: Option<&extern "C" fn(c_int, *mut sockaddr, *mut socklen_t, c_int) -> c_int>,
8-
fd: c_int,
9-
addr: *mut sockaddr,
10-
len: *mut socklen_t,
11-
flg: c_int,
12-
) -> c_int {
13-
cfg_if::cfg_if! {
14-
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
15-
static CHAIN: Lazy<
16-
Accept4SyscallFacade<IoUringAccept4Syscall<NioAccept4Syscall<RawAccept4Syscall>>>,
17-
> = Lazy::new(Default::default);
18-
} else {
19-
static CHAIN: Lazy<Accept4SyscallFacade<NioAccept4Syscall<RawAccept4Syscall>>> =
20-
Lazy::new(Default::default);
21-
}
22-
}
23-
CHAIN.accept4(fn_ptr, fd, addr, len, flg)
24-
}
25-
264
trait Accept4Syscall {
275
extern "C" fn accept4(
286
&self,
@@ -34,6 +12,10 @@ trait Accept4Syscall {
3412
) -> c_int;
3513
}
3614

15+
impl_syscall!(Accept4SyscallFacade, IoUringAccept4Syscall, NioAccept4Syscall, RawAccept4Syscall,
16+
accept4(fd: c_int, address: *mut sockaddr, address_len: *mut socklen_t, flg: c_int) -> c_int
17+
);
18+
3719
impl_facade!(Accept4SyscallFacade, Accept4Syscall,
3820
accept4(fd: c_int, address: *mut sockaddr, address_len: *mut socklen_t, flg: c_int) -> c_int
3921
);

core/src/syscall/unix/close.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
use crate::net::EventLoops;
2-
use once_cell::sync::Lazy;
32
use std::ffi::c_int;
43

5-
#[must_use]
6-
pub extern "C" fn close(fn_ptr: Option<&extern "C" fn(c_int) -> c_int>, fd: c_int) -> c_int {
7-
cfg_if::cfg_if! {
8-
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
9-
static CHAIN: Lazy<
10-
CloseSyscallFacade<IoUringCloseSyscall<NioCloseSyscall<RawCloseSyscall>>>
11-
> = Lazy::new(Default::default);
12-
} else {
13-
static CHAIN: Lazy<CloseSyscallFacade<NioCloseSyscall<RawCloseSyscall>>> =
14-
Lazy::new(Default::default);
15-
}
16-
}
17-
CHAIN.close(fn_ptr, fd)
18-
}
19-
204
trait CloseSyscall {
215
extern "C" fn close(&self, fn_ptr: Option<&extern "C" fn(c_int) -> c_int>, fd: c_int) -> c_int;
226
}
237

8+
impl_syscall!(CloseSyscallFacade, IoUringCloseSyscall, NioCloseSyscall, RawCloseSyscall,
9+
close(fd: c_int) -> c_int
10+
);
11+
2412
impl_facade!(CloseSyscallFacade, CloseSyscall, close(fd: c_int) -> c_int);
2513

2614
impl_io_uring!(IoUringCloseSyscall, CloseSyscall, close(fd: c_int) -> c_int);

core/src/syscall/unix/connect.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,9 @@ use crate::common::now;
22
use crate::net::EventLoops;
33
use crate::syscall::{is_blocking, reset_errno, send_time_limit, set_blocking, set_errno, set_non_blocking};
44
use libc::{sockaddr, socklen_t};
5-
use once_cell::sync::Lazy;
65
use std::ffi::{c_int, c_void};
76
use std::io::Error;
87

9-
#[must_use]
10-
pub extern "C" fn connect(
11-
fn_ptr: Option<&extern "C" fn(c_int, *const sockaddr, socklen_t) -> c_int>,
12-
socket: c_int,
13-
address: *const sockaddr,
14-
len: socklen_t,
15-
) -> c_int {
16-
cfg_if::cfg_if! {
17-
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
18-
static CHAIN: Lazy<
19-
ConnectSyscallFacade<IoUringConnectSyscall<NioConnectSyscall<RawConnectSyscall>>>
20-
> = Lazy::new(Default::default);
21-
} else {
22-
static CHAIN: Lazy<ConnectSyscallFacade<NioConnectSyscall<RawConnectSyscall>>> =
23-
Lazy::new(Default::default);
24-
}
25-
}
26-
CHAIN.connect(fn_ptr, socket, address, len)
27-
}
28-
298
trait ConnectSyscall {
309
extern "C" fn connect(
3110
&self,
@@ -36,6 +15,10 @@ trait ConnectSyscall {
3615
) -> c_int;
3716
}
3817

18+
impl_syscall!(ConnectSyscallFacade, IoUringConnectSyscall, NioConnectSyscall, RawConnectSyscall,
19+
connect(fd: c_int, address: *const sockaddr, len: socklen_t) -> c_int
20+
);
21+
3922
impl_facade!(ConnectSyscallFacade, ConnectSyscall,
4023
connect(fd: c_int, address: *const sockaddr, len: socklen_t) -> c_int
4124
);

core/src/syscall/unix/fsync.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
1-
use once_cell::sync::Lazy;
21
use std::ffi::c_int;
32

4-
#[must_use]
5-
pub extern "C" fn fsync(
6-
fn_ptr: Option<&extern "C" fn(c_int) -> c_int>,
7-
fd: c_int,
8-
) -> c_int {
9-
cfg_if::cfg_if! {
10-
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
11-
static CHAIN: Lazy<FsyncSyscallFacade<IoUringFsyncSyscall<RawFsyncSyscall>>> =
12-
Lazy::new(Default::default);
13-
} else {
14-
static CHAIN: Lazy<FsyncSyscallFacade<RawFsyncSyscall>> = Lazy::new(Default::default);
15-
}
16-
}
17-
CHAIN.fsync(fn_ptr, fd)
18-
}
19-
203
trait FsyncSyscall {
214
extern "C" fn fsync(
225
&self,
@@ -25,6 +8,8 @@ trait FsyncSyscall {
258
) -> c_int;
269
}
2710

11+
impl_syscall2!(FsyncSyscallFacade, IoUringFsyncSyscall, RawFsyncSyscall, fsync(fd: c_int) -> c_int);
12+
2813
impl_facade!(FsyncSyscallFacade, FsyncSyscall, fsync(fd: c_int) -> c_int);
2914

3015
impl_io_uring!(IoUringFsyncSyscall, FsyncSyscall, fsync(fd: c_int) -> c_int);

core/src/syscall/unix/link.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
use std::ffi::{c_char, c_int};
2-
use once_cell::sync::Lazy;
3-
4-
#[must_use]
5-
pub extern "C" fn link(
6-
fn_ptr: Option<&extern "C" fn(*const c_char, *const c_char) -> c_int>,
7-
src: *const c_char,
8-
dst: *const c_char
9-
) -> c_int{
10-
static CHAIN: Lazy<LinkSyscallFacade<RawLinkSyscall>> = Lazy::new(Default::default);
11-
CHAIN.link(fn_ptr, src, dst)
12-
}
132

143
trait LinkSyscall {
154
extern "C" fn link(
@@ -20,6 +9,10 @@ trait LinkSyscall {
209
) -> c_int;
2110
}
2211

12+
impl_syscall!(LinkSyscallFacade, RawLinkSyscall,
13+
link(src: *const c_char, dst: *const c_char) -> c_int
14+
);
15+
2316
impl_facade!(LinkSyscallFacade, LinkSyscall,
2417
link(src: *const c_char, dst: *const c_char) -> c_int
2518
);

core/src/syscall/unix/listen.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
use once_cell::sync::Lazy;
21
use std::ffi::c_int;
32

4-
#[must_use]
5-
pub extern "C" fn listen(
6-
fn_ptr: Option<&extern "C" fn(c_int, c_int) -> c_int>,
7-
fd: c_int,
8-
backlog: c_int,
9-
) -> c_int {
10-
static CHAIN: Lazy<ListenSyscallFacade<RawListenSyscall>> = Lazy::new(Default::default);
11-
CHAIN.listen(fn_ptr, fd, backlog)
12-
}
13-
143
trait ListenSyscall {
154
extern "C" fn listen(
165
&self,
@@ -20,6 +9,8 @@ trait ListenSyscall {
209
) -> c_int;
2110
}
2211

12+
impl_syscall!(ListenSyscallFacade, RawListenSyscall, listen(fd: c_int, backlog: c_int) -> c_int);
13+
2314
impl_facade!(ListenSyscallFacade, ListenSyscall, listen(fd: c_int, backlog: c_int) -> c_int);
2415

2516
impl_raw!(RawListenSyscall, ListenSyscall, listen(fd: c_int, backlog: c_int) -> c_int);

core/src/syscall/unix/lseek.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
use libc::off_t;
22
use std::ffi::c_int;
3-
use once_cell::sync::Lazy;
4-
5-
#[must_use]
6-
pub extern "C" fn lseek(
7-
fn_ptr: Option<&extern "C" fn(c_int, off_t, c_int) -> off_t>,
8-
fd: c_int,
9-
offset: off_t,
10-
whence: c_int,
11-
) -> off_t{
12-
static CHAIN: Lazy<LseekSyscallFacade<RawLseekSyscall>> = Lazy::new(Default::default);
13-
CHAIN.lseek(fn_ptr, fd, offset,whence)
14-
}
153

164
trait LseekSyscall {
175
extern "C" fn lseek(
@@ -23,6 +11,10 @@ trait LseekSyscall {
2311
) -> off_t;
2412
}
2513

14+
impl_syscall!(LseekSyscallFacade, RawLseekSyscall,
15+
lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t
16+
);
17+
2618
impl_facade!(LseekSyscallFacade, LseekSyscall,
2719
lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t
2820
);

core/src/syscall/unix/mkdir.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
use libc::mode_t;
2-
use once_cell::sync::Lazy;
32
use std::ffi::{c_char, c_int};
43

5-
#[must_use]
6-
pub extern "C" fn mkdir(
7-
fn_ptr: Option<&extern "C" fn(*const c_char, mode_t) -> c_int>,
8-
path: *const c_char,
9-
mode: mode_t,
10-
) -> c_int {
11-
static CHAIN: Lazy<MkdirSyscallFacade<RawMkdirSyscall>> = Lazy::new(Default::default);
12-
CHAIN.mkdir(fn_ptr, path, mode)
13-
}
14-
154
trait MkdirSyscall {
165
extern "C" fn mkdir(
176
&self,
@@ -21,6 +10,10 @@ trait MkdirSyscall {
2110
) -> c_int;
2211
}
2312

13+
impl_syscall!(MkdirSyscallFacade, RawMkdirSyscall,
14+
mkdir(path: *const c_char, mode: mode_t) -> c_int
15+
);
16+
2417
impl_facade!(MkdirSyscallFacade, MkdirSyscall,
2518
mkdir(path: *const c_char, mode: mode_t) -> c_int
2619
);

0 commit comments

Comments
 (0)