Skip to content

Commit a65e119

Browse files
committed
refactor recv syscall
1 parent 99781b1 commit a65e119

File tree

8 files changed

+116
-68
lines changed

8 files changed

+116
-68
lines changed

open-coroutine-core/src/syscall/facade.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,6 @@ pub extern "C" fn select(
5151

5252
/// read
5353
54-
#[must_use]
55-
pub extern "C" fn recv(
56-
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, c_int) -> ssize_t>,
57-
socket: c_int,
58-
buf: *mut c_void,
59-
len: size_t,
60-
flags: c_int,
61-
) -> ssize_t {
62-
CHAIN.recv(fn_ptr, socket, buf, len, flags)
63-
}
64-
6554
#[must_use]
6655
pub extern "C" fn recvfrom(
6756
fn_ptr: Option<

open-coroutine-core/src/syscall/io_uring.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,6 @@ impl<I: UnixSyscall> UnixSyscall for IoUringLinuxSyscall<I> {
5151
unsupported!(self, select, fn_ptr, nfds, readfds, writefds, errorfds, timeout)
5252
}
5353

54-
extern "C" fn recv(
55-
&self,
56-
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, c_int) -> ssize_t>,
57-
socket: c_int,
58-
buf: *mut c_void,
59-
len: size_t,
60-
flags: c_int,
61-
) -> ssize_t {
62-
impl_io_uring!(self, recv, fn_ptr, socket, buf, len, flags)
63-
}
64-
6554
extern "C" fn recvfrom(
6655
&self,
6756
fn_ptr: Option<

open-coroutine-core/src/syscall/mod.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ pub trait UnixSyscall {
5454

5555
/// read
5656
57-
extern "C" fn recv(
58-
&self,
59-
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, c_int) -> ssize_t>,
60-
fd: c_int,
61-
buf: *mut c_void,
62-
len: size_t,
63-
flags: c_int,
64-
) -> ssize_t;
65-
6657
extern "C" fn recvfrom(
6758
&self,
6859
fn_ptr: Option<

open-coroutine-core/src/syscall/nio.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -352,17 +352,6 @@ impl<I: UnixSyscall> UnixSyscall for NioLinuxSyscall<I> {
352352
r
353353
}
354354

355-
extern "C" fn recv(
356-
&self,
357-
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, c_int) -> ssize_t>,
358-
socket: c_int,
359-
buf: *mut c_void,
360-
len: size_t,
361-
flags: c_int,
362-
) -> ssize_t {
363-
impl_expected_read_hook!(self.inner, recv, fn_ptr, socket, buf, len, flags)
364-
}
365-
366355
extern "C" fn recvfrom(
367356
&self,
368357
fn_ptr: Option<

open-coroutine-core/src/syscall/raw.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,6 @@ impl UnixSyscall for RawLinuxSyscall {
4848

4949
/// read
5050
51-
extern "C" fn recv(
52-
&self,
53-
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, c_int) -> ssize_t>,
54-
socket: c_int,
55-
buf: *mut c_void,
56-
len: size_t,
57-
flags: c_int,
58-
) -> ssize_t {
59-
if let Some(f) = fn_ptr {
60-
(f)(socket, buf, len, flags)
61-
} else {
62-
unsafe { libc::send(socket, buf, len, flags) }
63-
}
64-
}
65-
6651
extern "C" fn recvfrom(
6752
&self,
6853
fn_ptr: Option<

open-coroutine-core/src/syscall/state.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,6 @@ impl<I: UnixSyscall> UnixSyscall for StateLinuxSyscall<I> {
6161
syscall_state!(self, select, fn_ptr, nfds, readfds, writefds, errorfds, timeout)
6262
}
6363

64-
extern "C" fn recv(
65-
&self,
66-
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, c_int) -> ssize_t>,
67-
socket: c_int,
68-
buf: *mut c_void,
69-
len: size_t,
70-
flags: c_int,
71-
) -> ssize_t {
72-
syscall_state!(self, recv, fn_ptr, socket, buf, len, flags)
73-
}
74-
7564
extern "C" fn recvfrom(
7665
&self,
7766
fn_ptr: Option<

open-coroutine-core/src/syscall/unix/mod.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub use close::close;
55
pub use connect::connect;
66
pub use listen::listen;
77
pub use nanosleep::nanosleep;
8+
pub use recv::recv;
89
pub use send::send;
910
pub use shutdown::shutdown;
1011
pub use sleep::sleep;
@@ -118,6 +119,69 @@ macro_rules! impl_nio_read {
118119
}
119120
}
120121

122+
macro_rules! impl_nio_expected_read {
123+
( $struct_name:ident, $trait_name: ident, $syscall: ident($fd: ident : $fd_type: ty,
124+
$buf: ident : $buf_type: ty, $len: ident : $len_type: ty, $($arg: ident : $arg_type: ty),*) -> $result: ty ) => {
125+
#[derive(Debug, Default)]
126+
struct $struct_name<I: $trait_name> {
127+
inner: I,
128+
}
129+
130+
impl<I: $trait_name> $trait_name for $struct_name<I> {
131+
extern "C" fn $syscall(
132+
&self,
133+
fn_ptr: Option<&extern "C" fn($fd_type, $buf_type, $len_type, $($arg_type),*) -> $result>,
134+
$fd: $fd_type,
135+
$buf: $buf_type,
136+
$len: $len_type,
137+
$($arg: $arg_type),*
138+
) -> $result {
139+
let blocking = $crate::syscall::common::is_blocking($fd);
140+
if blocking {
141+
$crate::syscall::common::set_non_blocking($fd);
142+
}
143+
let mut received = 0;
144+
let mut r = 0;
145+
while received < $len {
146+
r = self.inner.$syscall(
147+
fn_ptr,
148+
$fd,
149+
($buf as usize + received) as *mut c_void,
150+
$len - received,
151+
$($arg, )*
152+
);
153+
if r != -1 {
154+
$crate::syscall::common::reset_errno();
155+
received += r as size_t;
156+
if received >= $len || r == 0 {
157+
r = received as ssize_t;
158+
break;
159+
}
160+
}
161+
let error_kind = std::io::Error::last_os_error().kind();
162+
if error_kind == std::io::ErrorKind::WouldBlock {
163+
//wait read event
164+
if $crate::net::event_loop::EventLoops::wait_read_event(
165+
$fd,
166+
Some(std::time::Duration::from_millis(10)),
167+
)
168+
.is_err()
169+
{
170+
break;
171+
}
172+
} else if error_kind != std::io::ErrorKind::Interrupted {
173+
break;
174+
}
175+
}
176+
if blocking {
177+
$crate::syscall::common::set_blocking($fd);
178+
}
179+
r
180+
}
181+
}
182+
}
183+
}
184+
121185
macro_rules! impl_nio_expected_write {
122186
( $struct_name:ident, $trait_name: ident, $syscall: ident($fd: ident : $fd_type: ty,
123187
$buf: ident : $buf_type: ty, $len: ident : $len_type: ty, $($arg: ident : $arg_type: ty),*) -> $result: ty ) => {
@@ -209,6 +273,7 @@ mod close;
209273
mod connect;
210274
mod listen;
211275
mod nanosleep;
276+
mod recv;
212277
mod send;
213278
mod shutdown;
214279
mod sleep;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use libc::{size_t, ssize_t};
2+
use once_cell::sync::Lazy;
3+
use std::ffi::{c_int, c_void};
4+
5+
#[must_use]
6+
pub extern "C" fn recv(
7+
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, c_int) -> ssize_t>,
8+
socket: c_int,
9+
buf: *mut c_void,
10+
len: size_t,
11+
flags: c_int,
12+
) -> ssize_t {
13+
cfg_if::cfg_if! {
14+
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
15+
static CHAIN: Lazy<
16+
RecvSyscallFacade<IoUringRecvSyscall<NioRecvSyscall<RawRecvSyscall>>>
17+
> = Lazy::new(Default::default);
18+
} else {
19+
static CHAIN: Lazy<RecvSyscallFacade<NioRecvSyscall<RawRecvSyscall>>> =
20+
Lazy::new(Default::default);
21+
}
22+
}
23+
CHAIN.recv(fn_ptr, socket, buf, len, flags)
24+
}
25+
26+
trait RecvSyscall {
27+
extern "C" fn recv(
28+
&self,
29+
fn_ptr: Option<&extern "C" fn(c_int, *mut c_void, size_t, c_int) -> ssize_t>,
30+
fd: c_int,
31+
buf: *mut c_void,
32+
len: size_t,
33+
flags: c_int,
34+
) -> ssize_t;
35+
}
36+
37+
impl_facade!(RecvSyscallFacade, RecvSyscall,
38+
recv(fd: c_int, buf: *mut c_void, len: size_t, flags: c_int) -> ssize_t
39+
);
40+
41+
impl_io_uring!(IoUringRecvSyscall, RecvSyscall,
42+
recv(fd: c_int, buf: *mut c_void, len: size_t, flags: c_int) -> ssize_t
43+
);
44+
45+
impl_nio_expected_read!(NioRecvSyscall, RecvSyscall,
46+
recv(fd: c_int, buf: *mut c_void, len: size_t, flags: c_int) -> ssize_t
47+
);
48+
49+
impl_raw!(RawRecvSyscall, RecvSyscall,
50+
recv(fd: c_int, buf: *mut c_void, len: size_t, flags: c_int) -> ssize_t
51+
);

0 commit comments

Comments
 (0)