Skip to content

Commit 59eaacf

Browse files
authored
Dev refactors part17 (#302)
2 parents d238860 + 6ad1361 commit 59eaacf

File tree

13 files changed

+349
-166
lines changed

13 files changed

+349
-166
lines changed

examples/src/lib.rs

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,61 @@ fn crate_task(input: i32) {
1414
);
1515
}
1616

17-
pub fn start_server<A: ToSocketAddrs>(
18-
addr: A,
19-
server_finished: Arc<(Mutex<bool>, Condvar)>,
20-
) -> std::io::Result<()> {
21-
let listener = TcpListener::bind(addr)?;
17+
pub fn start_server<A: ToSocketAddrs>(addr: A, server_finished: Arc<(Mutex<bool>, Condvar)>) {
18+
let listener = TcpListener::bind(addr).expect("start server failed");
2219
for stream in listener.incoming() {
23-
let mut stream = stream?;
20+
let mut socket = stream.expect("accept new connection failed");
2421
let mut buffer1 = [0; 256];
2522
for _ in 0..3 {
26-
assert_eq!(12, stream.read(&mut buffer1)?);
23+
assert_eq!(12, socket.read(&mut buffer1).expect("recv failed"));
2724
println!("Server Received: {}", String::from_utf8_lossy(&buffer1));
28-
assert_eq!(256, stream.write(&buffer1)?);
25+
assert_eq!(256, socket.write(&buffer1).expect("send failed"));
2926
println!("Server Send");
3027
}
3128
let mut buffer2 = [0; 256];
3229
for _ in 0..3 {
3330
let mut buffers = [IoSliceMut::new(&mut buffer1), IoSliceMut::new(&mut buffer2)];
34-
assert_eq!(26, stream.read_vectored(&mut buffers)?);
31+
assert_eq!(
32+
26,
33+
socket.read_vectored(&mut buffers).expect("readv failed")
34+
);
3535
println!(
3636
"Server Received Multiple: {}{}",
3737
String::from_utf8_lossy(&buffer1),
3838
String::from_utf8_lossy(&buffer2)
3939
);
4040
let responses = [IoSlice::new(&buffer1), IoSlice::new(&buffer2)];
41-
assert_eq!(512, stream.write_vectored(&responses)?);
41+
assert_eq!(
42+
512,
43+
socket.write_vectored(&responses).expect("writev failed")
44+
);
4245
println!("Server Send Multiple");
4346
}
4447
println!("Server Shutdown Write");
45-
stream.shutdown(Shutdown::Write).map(|()| {
48+
if socket.shutdown(Shutdown::Write).is_ok() {
4649
println!("Server Closed Connection");
47-
})?;
48-
let (lock, cvar) = &*server_finished;
49-
let mut pending = lock.lock().unwrap();
50-
*pending = false;
51-
cvar.notify_one();
50+
let (lock, cvar) = &*server_finished;
51+
let mut pending = lock.lock().unwrap();
52+
*pending = false;
53+
cvar.notify_one();
54+
println!("Server Closed");
55+
return;
56+
}
5257
}
53-
Ok(())
5458
}
5559

56-
pub fn start_client<A: ToSocketAddrs>(addr: A) -> std::io::Result<()> {
57-
let mut stream = connect_timeout(addr, Duration::from_secs(3))?;
60+
pub fn start_client<A: ToSocketAddrs>(addr: A) {
61+
let mut stream = connect_timeout(addr, Duration::from_secs(3)).expect("connect failed");
5862
let mut buffer1 = [0; 256];
5963
for i in 0..3 {
60-
assert_eq!(12, stream.write(format!("RequestPart{i}").as_ref())?);
64+
assert_eq!(
65+
12,
66+
stream
67+
.write(format!("RequestPart{i}").as_ref())
68+
.expect("send failed")
69+
);
6170
println!("Client Send");
62-
assert_eq!(256, stream.read(&mut buffer1)?);
71+
assert_eq!(256, stream.read(&mut buffer1).expect("recv failed"));
6372
println!("Client Received: {}", String::from_utf8_lossy(&buffer1));
6473
}
6574
let mut buffer2 = [0; 256];
@@ -70,20 +79,22 @@ pub fn start_client<A: ToSocketAddrs>(addr: A) -> std::io::Result<()> {
7079
IoSlice::new(request1.as_ref()),
7180
IoSlice::new(request2.as_ref()),
7281
];
73-
assert_eq!(26, stream.write_vectored(&requests)?);
82+
assert_eq!(26, stream.write_vectored(&requests).expect("writev failed"));
7483
println!("Client Send Multiple");
7584
let mut buffers = [IoSliceMut::new(&mut buffer1), IoSliceMut::new(&mut buffer2)];
76-
assert_eq!(512, stream.read_vectored(&mut buffers)?);
85+
assert_eq!(
86+
512,
87+
stream.read_vectored(&mut buffers).expect("readv failed")
88+
);
7789
println!(
7890
"Client Received Multiple: {}{}",
7991
String::from_utf8_lossy(&buffer1),
8092
String::from_utf8_lossy(&buffer2)
8193
);
8294
}
8395
println!("Client Shutdown Write");
84-
stream.shutdown(Shutdown::Write).map(|()| {
85-
println!("Client Closed");
86-
})
96+
stream.shutdown(Shutdown::Write).expect("shutdown failed");
97+
println!("Client Closed");
8798
}
8899

89100
fn connect_timeout<A: ToSocketAddrs>(addr: A, timeout: Duration) -> std::io::Result<TcpStream> {
@@ -111,7 +122,7 @@ pub fn crate_server(
111122
crate_task(1);
112123
let mut data: [u8; 512] = unsafe { std::mem::zeroed() };
113124
data[511] = b'\n';
114-
let listener = TcpListener::bind(format!("127.0.0.1:{port}"))
125+
let listener = TcpListener::bind((IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port))
115126
.unwrap_or_else(|_| panic!("bind to 127.0.0.1:{port} failed !"));
116127
server_started.store(true, Ordering::Release);
117128
//invoke by libc::accept
@@ -205,7 +216,7 @@ pub fn crate_co_server(
205216
crate_task(11);
206217
let mut data: [u8; 512] = unsafe { std::mem::zeroed() };
207218
data[511] = b'\n';
208-
let listener = TcpListener::bind(format!("127.0.0.1:{port}"))
219+
let listener = TcpListener::bind((IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port))
209220
.unwrap_or_else(|_| panic!("bind to 127.0.0.1:{port} failed !"));
210221
server_started.store(true, Ordering::Release);
211222
//invoke by libc::accept

open-coroutine-core/src/net/selector/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,7 @@ pub(crate) trait Selector<I: Interest, E: Event, S: EventIterator<E>> {
132132
READABLE_RECORDS.remove(&fd).is_some(),
133133
"Clean READABLE_RECORDS failed !"
134134
);
135-
assert!(
136-
READABLE_TOKEN_RECORDS.remove(&fd).is_some(),
137-
"Clean READABLE_TOKEN_RECORDS failed !"
138-
);
135+
_ = READABLE_TOKEN_RECORDS.remove(&fd);
139136
} else {
140137
self.del_event(fd)?;
141138
}
@@ -158,10 +155,7 @@ pub(crate) trait Selector<I: Interest, E: Event, S: EventIterator<E>> {
158155
WRITABLE_RECORDS.remove(&fd).is_some(),
159156
"Clean WRITABLE_RECORDS failed !"
160157
);
161-
assert!(
162-
WRITABLE_TOKEN_RECORDS.remove(&fd).is_some(),
163-
"Clean WRITABLE_TOKEN_RECORDS failed !"
164-
);
158+
_ = WRITABLE_TOKEN_RECORDS.remove(&fd);
165159
} else {
166160
self.del_event(fd)?;
167161
}

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,6 @@ pub extern "C" fn pread(
8787
CHAIN.pread(fn_ptr, fd, buf, count, offset)
8888
}
8989

90-
#[must_use]
91-
pub extern "C" fn readv(
92-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
93-
fd: c_int,
94-
iov: *const iovec,
95-
iovcnt: c_int,
96-
) -> ssize_t {
97-
CHAIN.readv(fn_ptr, fd, iov, iovcnt)
98-
}
99-
10090
#[must_use]
10191
pub extern "C" fn preadv(
10292
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,
@@ -156,16 +146,6 @@ pub extern "C" fn pwrite(
156146
CHAIN.pwrite(fn_ptr, fd, buf, count, offset)
157147
}
158148

159-
#[must_use]
160-
pub extern "C" fn writev(
161-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
162-
fd: c_int,
163-
iov: *const iovec,
164-
iovcnt: c_int,
165-
) -> ssize_t {
166-
CHAIN.writev(fn_ptr, fd, iov, iovcnt)
167-
}
168-
169149
#[must_use]
170150
pub extern "C" fn pwritev(
171151
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ impl<I: UnixSyscall> UnixSyscall for IoUringLinuxSyscall<I> {
9494
impl_io_uring!(self, pread, fn_ptr, fd, buf, count, offset)
9595
}
9696

97-
extern "C" fn readv(
98-
&self,
99-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
100-
fd: c_int,
101-
iov: *const iovec,
102-
iovcnt: c_int,
103-
) -> ssize_t {
104-
impl_io_uring!(self, readv, fn_ptr, fd, iov, iovcnt)
105-
}
106-
10797
extern "C" fn preadv(
10898
&self,
10999
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,
@@ -168,16 +158,6 @@ impl<I: UnixSyscall> UnixSyscall for IoUringLinuxSyscall<I> {
168158
impl_io_uring!(self, pwrite, fn_ptr, fd, buf, count, offset)
169159
}
170160

171-
extern "C" fn writev(
172-
&self,
173-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
174-
fd: c_int,
175-
iov: *const iovec,
176-
iovcnt: c_int,
177-
) -> ssize_t {
178-
impl_io_uring!(self, writev, fn_ptr, fd, iov, iovcnt)
179-
}
180-
181161
extern "C" fn pwritev(
182162
&self,
183163
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,6 @@ pub trait UnixSyscall {
9191
offset: off_t,
9292
) -> ssize_t;
9393

94-
extern "C" fn readv(
95-
&self,
96-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
97-
fd: c_int,
98-
iov: *const iovec,
99-
iovcnt: c_int,
100-
) -> ssize_t;
101-
10294
extern "C" fn preadv(
10395
&self,
10496
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,
@@ -155,14 +147,6 @@ pub trait UnixSyscall {
155147
offset: off_t,
156148
) -> ssize_t;
157149

158-
extern "C" fn writev(
159-
&self,
160-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
161-
fd: c_int,
162-
iov: *const iovec,
163-
iovcnt: c_int,
164-
) -> ssize_t;
165-
166150
extern "C" fn pwritev(
167151
&self,
168152
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -397,16 +397,6 @@ impl<I: UnixSyscall> UnixSyscall for NioLinuxSyscall<I> {
397397
impl_expected_read_hook!(self.inner, pread, fn_ptr, fd, buf, count, offset)
398398
}
399399

400-
extern "C" fn readv(
401-
&self,
402-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
403-
fd: c_int,
404-
iov: *const iovec,
405-
iovcnt: c_int,
406-
) -> ssize_t {
407-
impl_expected_batch_read_hook!(self.inner, readv, fn_ptr, fd, iov, iovcnt,)
408-
}
409-
410400
extern "C" fn preadv(
411401
&self,
412402
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,
@@ -561,16 +551,6 @@ impl<I: UnixSyscall> UnixSyscall for NioLinuxSyscall<I> {
561551
impl_expected_write_hook!(self.inner, pwrite, fn_ptr, fd, buf, count, offset)
562552
}
563553

564-
extern "C" fn writev(
565-
&self,
566-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
567-
fd: c_int,
568-
iov: *const iovec,
569-
iovcnt: c_int,
570-
) -> ssize_t {
571-
impl_expected_batch_write_hook!(self.inner, writev, fn_ptr, fd, iov, iovcnt,)
572-
}
573-
574554
extern "C" fn pwritev(
575555
&self,
576556
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,6 @@ impl UnixSyscall for RawLinuxSyscall {
103103
}
104104
}
105105

106-
extern "C" fn readv(
107-
&self,
108-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
109-
fd: c_int,
110-
iov: *const iovec,
111-
iovcnt: c_int,
112-
) -> ssize_t {
113-
if let Some(f) = fn_ptr {
114-
(f)(fd, iov, iovcnt)
115-
} else {
116-
unsafe { libc::readv(fd, iov, iovcnt) }
117-
}
118-
}
119-
120106
extern "C" fn preadv(
121107
&self,
122108
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,
@@ -203,20 +189,6 @@ impl UnixSyscall for RawLinuxSyscall {
203189
}
204190
}
205191

206-
extern "C" fn writev(
207-
&self,
208-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
209-
fd: c_int,
210-
iov: *const iovec,
211-
iovcnt: c_int,
212-
) -> ssize_t {
213-
if let Some(f) = fn_ptr {
214-
(f)(fd, iov, iovcnt)
215-
} else {
216-
unsafe { libc::writev(fd, iov, iovcnt) }
217-
}
218-
}
219-
220192
extern "C" fn pwritev(
221193
&self,
222194
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,6 @@ impl<I: UnixSyscall> UnixSyscall for StateLinuxSyscall<I> {
104104
syscall_state!(self, pread, fn_ptr, fd, buf, count, offset)
105105
}
106106

107-
extern "C" fn readv(
108-
&self,
109-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
110-
fd: c_int,
111-
iov: *const iovec,
112-
iovcnt: c_int,
113-
) -> ssize_t {
114-
syscall_state!(self, readv, fn_ptr, fd, iov, iovcnt)
115-
}
116-
117107
extern "C" fn preadv(
118108
&self,
119109
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,
@@ -178,16 +168,6 @@ impl<I: UnixSyscall> UnixSyscall for StateLinuxSyscall<I> {
178168
syscall_state!(self, pwrite, fn_ptr, fd, buf, count, offset)
179169
}
180170

181-
extern "C" fn writev(
182-
&self,
183-
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int) -> ssize_t>,
184-
fd: c_int,
185-
iov: *const iovec,
186-
iovcnt: c_int,
187-
) -> ssize_t {
188-
syscall_state!(self, writev, fn_ptr, fd, iov, iovcnt)
189-
}
190-
191171
extern "C" fn pwritev(
192172
&self,
193173
fn_ptr: Option<&extern "C" fn(c_int, *const iovec, c_int, off_t) -> ssize_t>,

0 commit comments

Comments
 (0)