Skip to content

Commit 81588a5

Browse files
authored
Merge pull request #22 from George-Miao/dev/mio_integration
Mio integration
2 parents 63370e5 + c5c38a1 commit 81588a5

File tree

18 files changed

+736
-126
lines changed

18 files changed

+736
-126
lines changed

Cargo.toml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,21 @@ repository = "https://github.com/Berrysoft/compio"
1313
[package.metadata.docs.rs]
1414
all-features = true
1515
default-target = "x86_64-pc-windows-msvc"
16-
targets = ["x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu"]
16+
targets = [
17+
"x86_64-pc-windows-msvc",
18+
"x86_64-unknown-linux-gnu",
19+
"x86_64-apple-darwin",
20+
"aarch64-apple-ios",
21+
"aarch64-linux-android",
22+
"x86_64-unknown-dragonfly",
23+
"x86_64-unknown-freebsd",
24+
"x86_64-unknown-illumos",
25+
"x86_64-unknown-linux-gnu",
26+
"x86_64-unknown-netbsd",
27+
"x86_64-unknown-openbsd",
28+
]
1729

30+
# Shared dependencies for all platforms
1831
[dependencies]
1932
async-task = { version = "4", optional = true }
2033
bytes = { version = "1", optional = true }
@@ -25,12 +38,14 @@ once_cell = "1"
2538
slab = { version = "0.4", optional = true }
2639
socket2 = { version = "0.5", features = ["all"] }
2740

41+
# Shared dev dependencies for all platforms
2842
[dev-dependencies]
2943
criterion = { version = "0.5", features = ["async_tokio"] }
3044
futures-channel = "0.3"
3145
tempfile = "3"
3246
tokio = { version = "1", features = ["fs", "io-util", "macros", "net", "rt"] }
3347

48+
# Windows specific dependencies
3449
[target.'cfg(target_os = "windows")'.dependencies]
3550
widestring = "1"
3651
windows-sys = { version = "0.48", features = [
@@ -45,13 +60,20 @@ windows-sys = { version = "0.48", features = [
4560
"Win32_System_Threading",
4661
] }
4762

63+
# Windows specific dev dependencies
4864
[target.'cfg(target_os = "windows")'.dev-dependencies]
4965
windows-sys = { version = "0.48", features = ["Win32_Security_Authorization"] }
5066

67+
# Linux specific dependencies
5168
[target.'cfg(target_os = "linux")'.dependencies]
5269
io-uring = "0.6"
5370
libc = "0.2"
5471

72+
# Other platform dependencies
73+
[target.'cfg(all(not(target_os = "linux"), unix))'.dependencies]
74+
mio = { version = "0.8.8", features = ["os-ext"] }
75+
libc = "0.2"
76+
5577
[features]
5678
default = ["runtime"]
5779
runtime = ["dep:async-task", "dep:futures-util", "dep:slab"]

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![docs.rs](https://img.shields.io/badge/docs.rs-compio-latest)](https://docs.rs/compio)
66
[![Azure DevOps builds](https://strawberry-vs.visualstudio.com/compio/_apis/build/status/Berrysoft.compio?branch=master)](https://strawberry-vs.visualstudio.com/compio/_build)
77

8-
A thread-per-core Rust runtime with IOCP/io_uring.
8+
A thread-per-core Rust runtime with IOCP/io_uring/mio.
99
The name comes from "completion-based IO".
1010
This crate is inspired by [monoio](https://github.com/bytedance/monoio/).
1111

@@ -22,6 +22,7 @@ and `tokio` won't public APIs to control `mio` before `mio` reaches 1.0.
2222
## Quick start
2323

2424
With `runtime` feature enabled, we can use the high level APIs to perform fs & net IO.
25+
2526
```rust,no_run
2627
use compio::{fs::File, task::block_on};
2728
@@ -36,6 +37,7 @@ println!("{}", buffer);
3637
```
3738

3839
While you can also control the low-level driver manually:
40+
3941
```rust,no_run
4042
use compio::{
4143
buf::IntoInner,

azure-pipelines.yml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,39 @@ jobs:
4545
cargo test --features all
4646
displayName: TestStable
4747
48+
- job: Test_Mac
49+
strategy:
50+
matrix:
51+
ventura:
52+
image: macOS-13
53+
monterey:
54+
image: macOS-12
55+
pool:
56+
vmImage: $(image)
57+
58+
steps:
59+
- script: |
60+
rustup toolchain install nightly
61+
cargo +nightly test --features all,nightly --no-default-features
62+
displayName: TestNightly
63+
- script: |
64+
cargo test --features all
65+
displayName: TestStable
66+
4867
- job: Doc
4968
strategy:
5069
matrix:
5170
windows:
5271
image: windows-latest
5372
linux:
5473
image: ubuntu-latest
74+
macos:
75+
image: macOS-latest
5576
pool:
5677
vmImage: $(image)
5778

5879
steps:
5980
- script: |
6081
rustup toolchain install nightly
61-
cargo +nightly doc --features all --no-deps
82+
cargo +nightly doc --all-features --no-deps
6283
displayName: Build docs

src/driver/iour/op.rs

Lines changed: 6 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use std::io::{IoSlice, IoSliceMut};
2-
31
use io_uring::{
42
opcode,
53
squeue::Entry,
64
types::{Fd, FsyncFlags},
75
};
8-
use libc::{sockaddr_storage, socklen_t};
9-
use socket2::SockAddr;
6+
use libc::sockaddr_storage;
107

8+
pub use crate::driver::unix::op::*;
119
use crate::{
12-
buf::{AsIoSlices, AsIoSlicesMut, IntoInner, IoBuf, IoBufMut, OneOrVec},
13-
driver::{OpCode, RawFd},
10+
buf::{AsIoSlices, AsIoSlicesMut, IoBuf, IoBufMut},
11+
driver::OpCode,
1412
op::*,
1513
};
1614

@@ -44,29 +42,6 @@ impl OpCode for Sync {
4442
}
4543
}
4644

47-
/// Accept a connection.
48-
pub struct Accept {
49-
pub(crate) fd: RawFd,
50-
pub(crate) buffer: sockaddr_storage,
51-
pub(crate) addr_len: socklen_t,
52-
}
53-
54-
impl Accept {
55-
/// Create [`Accept`].
56-
pub fn new(fd: RawFd) -> Self {
57-
Self {
58-
fd,
59-
buffer: unsafe { std::mem::zeroed() },
60-
addr_len: std::mem::size_of::<sockaddr_storage>() as _,
61-
}
62-
}
63-
64-
/// Get the remote address from the inner buffer.
65-
pub fn into_addr(self) -> SockAddr {
66-
unsafe { SockAddr::new(self.buffer, self.addr_len) }
67-
}
68-
}
69-
7045
impl OpCode for Accept {
7146
fn create_entry(&mut self) -> Entry {
7247
opcode::Accept::new(
@@ -108,96 +83,18 @@ impl<T: AsIoSlices> OpCode for SendImpl<T> {
10883
}
10984
}
11085

111-
/// Receive data and source address.
112-
pub struct RecvFromImpl<T: AsIoSlicesMut> {
113-
pub(crate) fd: RawFd,
114-
pub(crate) buffer: T,
115-
pub(crate) addr: sockaddr_storage,
116-
pub(crate) slices: OneOrVec<IoSliceMut<'static>>,
117-
msg: libc::msghdr,
118-
}
119-
120-
impl<T: AsIoSlicesMut> RecvFromImpl<T> {
121-
/// Create [`RecvFrom`] or [`RecvFromVectored`].
122-
pub fn new(fd: RawFd, buffer: T::Inner) -> Self {
123-
Self {
124-
fd,
125-
buffer: T::new(buffer),
126-
addr: unsafe { std::mem::zeroed() },
127-
slices: OneOrVec::One(IoSliceMut::new(&mut [])),
128-
msg: unsafe { std::mem::zeroed() },
129-
}
130-
}
131-
}
132-
133-
impl<T: AsIoSlicesMut> IntoInner for RecvFromImpl<T> {
134-
type Inner = (T, sockaddr_storage, socklen_t);
135-
136-
fn into_inner(self) -> Self::Inner {
137-
(self.buffer, self.addr, self.msg.msg_namelen)
138-
}
139-
}
140-
14186
impl<T: AsIoSlicesMut> OpCode for RecvFromImpl<T> {
14287
#[allow(clippy::no_effect)]
14388
fn create_entry(&mut self) -> Entry {
144-
self.slices = unsafe { self.buffer.as_io_slices_mut() };
145-
self.msg = libc::msghdr {
146-
msg_name: &mut self.addr as *mut _ as _,
147-
msg_namelen: 128,
148-
msg_iov: self.slices.as_mut_ptr() as _,
149-
msg_iovlen: self.slices.len(),
150-
msg_control: std::ptr::null_mut(),
151-
msg_controllen: 0,
152-
msg_flags: 0,
153-
};
89+
self.set_msg();
15490
opcode::RecvMsg::new(Fd(self.fd), &mut self.msg).build()
15591
}
15692
}
15793

158-
/// Send data to specified address.
159-
pub struct SendToImpl<T: AsIoSlices> {
160-
pub(crate) fd: RawFd,
161-
pub(crate) buffer: T,
162-
pub(crate) addr: SockAddr,
163-
pub(crate) slices: OneOrVec<IoSlice<'static>>,
164-
msg: libc::msghdr,
165-
}
166-
167-
impl<T: AsIoSlices> SendToImpl<T> {
168-
/// Create [`SendTo`] or [`SendToVectored`].
169-
pub fn new(fd: RawFd, buffer: T::Inner, addr: SockAddr) -> Self {
170-
Self {
171-
fd,
172-
buffer: T::new(buffer),
173-
addr,
174-
slices: OneOrVec::One(IoSlice::new(&[])),
175-
msg: unsafe { std::mem::zeroed() },
176-
}
177-
}
178-
}
179-
180-
impl<T: AsIoSlices> IntoInner for SendToImpl<T> {
181-
type Inner = T;
182-
183-
fn into_inner(self) -> Self::Inner {
184-
self.buffer
185-
}
186-
}
187-
18894
impl<T: AsIoSlices> OpCode for SendToImpl<T> {
18995
#[allow(clippy::no_effect)]
19096
fn create_entry(&mut self) -> Entry {
191-
self.slices = unsafe { self.buffer.as_io_slices() };
192-
self.msg = libc::msghdr {
193-
msg_name: self.addr.as_ptr() as _,
194-
msg_namelen: self.addr.len(),
195-
msg_iov: self.slices.as_mut_ptr() as _,
196-
msg_iovlen: self.slices.len(),
197-
msg_control: std::ptr::null_mut(),
198-
msg_controllen: 0,
199-
msg_flags: 0,
200-
};
97+
self.set_msg();
20198
opcode::SendMsg::new(Fd(self.fd), &self.msg).build()
20299
}
203100
}

0 commit comments

Comments
 (0)