Skip to content

Commit 0d41c53

Browse files
committed
feat: implement write op on windows
1 parent 46c921f commit 0d41c53

File tree

14 files changed

+243
-83
lines changed

14 files changed

+243
-83
lines changed

.github/workflows/ci.sh

+24-18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ if [ "${CROSS}" = "1" ]; then
1111
CARGO=cross
1212
fi
1313

14+
CARGO_TEST_FLAGS=
15+
if [ "${NO_RUN}" = "1" ]; then
16+
CARGO_TEST_FLAGS=--no-run
17+
fi
18+
1419
# If a test crashes, we want to know which one it was.
1520
export RUST_TEST_THREADS=1
1621
export RUST_BACKTRACE=1
@@ -19,38 +24,39 @@ export RUST_BACKTRACE=1
1924
cd "${PROJECT_DIR}"/monoio
2025

2126
# only enable legacy driver
22-
"${CARGO}" test --target "${TARGET}" --no-default-features --features "async-cancel,bytes,legacy,macros,utils"
23-
"${CARGO}" test --target "${TARGET}" --no-default-features --features "async-cancel,bytes,legacy,macros,utils" --release
27+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --no-default-features --features "async-cancel,bytes,legacy,macros,utils"
28+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --no-default-features --features "async-cancel,bytes,legacy,macros,utils" --release
2429

2530
if [ "${TARGET}" = "x86_64-unknown-linux-gnu" ] || [ "${TARGET}" = "i686-unknown-linux-gnu" ]; then
26-
2731
# only enabled uring driver
28-
"${CARGO}" test --target "${TARGET}" --no-default-features --features "async-cancel,bytes,iouring,macros,utils"
29-
"${CARGO}" test --target "${TARGET}" --no-default-features --features "async-cancel,bytes,iouring,macros,utils" --release
32+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --no-default-features --features "async-cancel,bytes,iouring,macros,utils"
33+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --no-default-features --features "async-cancel,bytes,iouring,macros,utils" --release
34+
fi
3035

36+
if [ "${TARGET}" != "aarch64-unknown-linux-gnu" ] && [ "${TARGET}" != "armv7-unknown-linux-gnueabihf" ] &&
37+
[ "${TARGET}" != "riscv64gc-unknown-linux-gnu" ] && [ "${TARGET}" != "s390x-unknown-linux-gnu" ]; then
3138
# enable uring+legacy driver
32-
"${CARGO}" test --target "${TARGET}"
33-
"${CARGO}" test --target "${TARGET}" --release
34-
35-
if [ "${CHANNEL}" == "nightly" ]; then
36-
"${CARGO}" test --target "${TARGET}" --all-features
37-
"${CARGO}" test --target "${TARGET}" --all-features --release
38-
fi
39+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}"
40+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --release
41+
fi
3942

43+
if [ "${CHANNEL}" == "nightly" ] && ( [ "${TARGET}" = "x86_64-unknown-linux-gnu" ] || [ "${TARGET}" = "i686-unknown-linux-gnu" ] ); then
44+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --all-features
45+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --all-features --release
4046
fi
4147

4248
# test monoio-compat mod
4349
cd "${PROJECT_DIR}"/monoio-compat
4450

45-
"${CARGO}" test --target "${TARGET}"
46-
"${CARGO}" test --target "${TARGET}" --release
51+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}"
52+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --release
4753

48-
"${CARGO}" test --target "${TARGET}" --no-default-features --features hyper
49-
"${CARGO}" test --target "${TARGET}" --no-default-features --features hyper --release
54+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --no-default-features --features hyper
55+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --no-default-features --features hyper --release
5056

5157
if [ "${CHANNEL}" == "nightly" ]; then
52-
"${CARGO}" test --target "${TARGET}" --all-features
53-
"${CARGO}" test --target "${TARGET}" --all-features --release
58+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --all-features
59+
"${CARGO}" test $CARGO_TEST_FLAGS --target "${TARGET}" --all-features --release
5460
fi
5561

5662
# todo maybe we should test examples here ?

.github/workflows/ci.yml

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ jobs:
5151
TARGET: ${{ matrix.target }}
5252
OS: ${{ matrix.os }}
5353
PROJECT_DIR: ${{ github.workspace }}
54+
NO_RUN: ${{ matrix.no_run }}
5455
run: sh .github/workflows/ci.sh
5556

5657
strategy:
@@ -102,9 +103,13 @@ jobs:
102103
# unsupported yet
103104
# - target: x86_64-pc-windows-msvc
104105
# os: windows-latest
106+
# no_run: 1
105107
# - target: x86_64-pc-windows-gnu
106108
# os: windows-latest
109+
# no_run: 1
107110
# - target: i686-pc-windows-msvc
108111
# os: windows-latest
112+
# no_run: 1
109113
# - target: i686-pc-windows-gnu
110114
# os: windows-latest
115+
# no_run: 1

monoio/Cargo.toml

+6-7
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,22 @@ memchr = "2.7"
2424
bytes = { version = "1", optional = true }
2525
flume = { version = "0.11", optional = true }
2626
mio = { version = "0.8", features = [
27-
"net",
28-
"os-poll",
29-
"os-ext",
27+
"net",
28+
"os-poll",
29+
"os-ext",
3030
], optional = true }
3131
threadpool = { version = "1", optional = true }
3232
tokio = { version = "1", default-features = false, optional = true }
3333
tracing = { version = "0.1", default-features = false, features = [
34-
"std",
34+
"std",
3535
], optional = true }
3636
ctrlc = { version = "3", optional = true }
3737
lazy_static = { version = "1", optional = true }
3838
once_cell = { version = "1.19.0", optional = true }
3939

4040
# windows dependencies(will be added when windows support finished)
41-
[target.'cfg(windows)'.dependencies.windows-sys]
42-
features = ["Win32_Foundation", "Win32_Networking_WinSock"]
43-
version = "0.48.0"
41+
[target.'cfg(windows)'.dependencies]
42+
windows-sys = { version = "0.48.0", features = ["Win32_Foundation", "Win32_Networking_WinSock", "Win32_System_IO"] }
4443

4544
# unix dependencies
4645
[target.'cfg(unix)'.dependencies]

monoio/src/buf/raw_buf.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ unsafe impl IoVecBufMut for RawBufVectored {
175175

176176
#[cfg(windows)]
177177
#[inline]
178-
fn write_wsabuf_ptr(&self) -> *mut WSABUF {
178+
fn write_wsabuf_ptr(&mut self) -> *mut WSABUF {
179179
self.ptr as *mut WSABUF
180180
}
181181

182182
#[cfg(windows)]
183183
#[inline]
184-
fn write_wsabuf_len(&self) -> usize {
184+
fn write_wsabuf_len(&mut self) -> usize {
185185
self.len
186186
}
187187

monoio/src/driver/op/accept.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
2+
use std::os::unix::prelude::AsRawFd;
13
use std::{
24
io,
35
mem::{size_of, MaybeUninit},
@@ -13,12 +15,12 @@ use {
1315
accept, socklen_t, INVALID_SOCKET, SOCKADDR_STORAGE,
1416
},
1517
};
16-
#[cfg(any(feature = "legacy", feature = "poll-io"))]
17-
use {crate::syscall_u32, std::os::unix::prelude::AsRawFd};
1818

1919
use super::{super::shared_fd::SharedFd, Op, OpAble};
2020
#[cfg(any(feature = "legacy", feature = "poll-io"))]
2121
use crate::driver::ready::Direction;
22+
#[cfg(any(feature = "legacy", feature = "poll-io"))]
23+
use crate::syscall_u32;
2224

2325
/// Accept
2426
pub(crate) struct Accept {
@@ -62,7 +64,7 @@ impl OpAble for Accept {
6264
.build()
6365
}
6466

65-
#[cfg(all(any(feature = "legacy", feature = "poll-io"), not(windows)))]
67+
#[cfg(any(feature = "legacy", feature = "poll-io"))]
6668
#[inline]
6769
fn legacy_interest(&self) -> Option<(Direction, usize)> {
6870
self.fd.registered_index().map(|idx| (Direction::Read, idx))
@@ -77,7 +79,7 @@ impl OpAble for Accept {
7779
syscall!(accept(fd, addr, len), PartialEq::eq, INVALID_SOCKET)
7880
}
7981

80-
#[cfg(any(feature = "legacy", feature = "poll-io"))]
82+
#[cfg(all(any(feature = "legacy", feature = "poll-io"), unix))]
8183
fn legacy_call(&mut self) -> io::Result<u32> {
8284
let fd = self.fd.as_raw_fd();
8385
let addr = self.addr.0.as_mut_ptr() as *mut _;

monoio/src/driver/op/fsync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl OpAble for Fsync {
6363
)
6464
}
6565

66-
#[cfg(any(feature = "legacy", feature = "poll-io"))]
66+
#[cfg(all(any(feature = "legacy", feature = "poll-io"), unix))]
6767
fn legacy_call(&mut self) -> io::Result<u32> {
6868
#[cfg(target_os = "linux")]
6969
if self.data_sync {

monoio/src/driver/op/read.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use std::io;
2+
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
3+
use std::os::unix::prelude::AsRawFd;
4+
#[cfg(all(windows, any(feature = "legacy", feature = "poll-io")))]
5+
use std::os::windows::io::AsRawSocket;
26

37
#[cfg(all(target_os = "linux", feature = "iouring"))]
48
use io_uring::{opcode, types};
5-
#[cfg(any(feature = "legacy", feature = "poll-io"))]
6-
use {
7-
crate::{driver::ready::Direction, syscall_u32},
8-
std::os::unix::prelude::AsRawFd,
9-
};
109

1110
use super::{super::shared_fd::SharedFd, Op, OpAble};
1211
use crate::{
1312
buf::{IoBufMut, IoVecBufMut},
1413
BufResult,
1514
};
15+
#[cfg(any(feature = "legacy", feature = "poll-io"))]
16+
use crate::{driver::ready::Direction, syscall_u32};
1617

1718
pub(crate) struct Read<T> {
1819
/// Holds a strong ref to the FD, preventing the file from being closed

monoio/src/driver/op/recv.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
2+
use std::os::unix::prelude::AsRawFd;
13
use std::{
24
io,
35
mem::{transmute, MaybeUninit},
@@ -6,14 +8,18 @@ use std::{
68

79
#[cfg(all(target_os = "linux", feature = "iouring"))]
810
use io_uring::{opcode, types};
9-
#[cfg(any(feature = "legacy", feature = "poll-io"))]
11+
#[cfg(unix)]
12+
use libc::{socklen_t, AF_INET, AF_INET6};
13+
#[cfg(all(windows, any(feature = "legacy", feature = "poll-io")))]
1014
use {
11-
crate::{driver::ready::Direction, syscall_u32},
12-
std::os::unix::prelude::AsRawFd,
15+
std::os::windows::io::AsRawSocket,
16+
windows_sys::Win32::Networking::WinSock::{socklen_t, AF_INET, AF_INET6},
1317
};
1418

1519
use super::{super::shared_fd::SharedFd, Op, OpAble};
1620
use crate::{buf::IoBufMut, net::unix::SocketAddr as UnixSocketAddr, BufResult};
21+
#[cfg(any(feature = "legacy", feature = "poll-io"))]
22+
use crate::{driver::ready::Direction, syscall_u32};
1723

1824
pub(crate) struct Recv<T> {
1925
/// Holds a strong ref to the FD, preventing the file from being closed
@@ -112,7 +118,7 @@ impl<T: IoBufMut> Op<RecvMsg<T>> {
112118
info.2.msg_iov = info.1.as_mut_ptr();
113119
info.2.msg_iovlen = 1;
114120
info.2.msg_name = &mut info.0 as *mut _ as *mut libc::c_void;
115-
info.2.msg_namelen = std::mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;
121+
info.2.msg_namelen = std::mem::size_of::<libc::sockaddr_storage>() as socklen_t;
116122

117123
Op::submit_with(RecvMsg { fd, buf, info })
118124
}
@@ -127,15 +133,15 @@ impl<T: IoBufMut> Op<RecvMsg<T>> {
127133

128134
let addr = unsafe {
129135
match storage.ss_family as libc::c_int {
130-
libc::AF_INET => {
136+
AF_INET => {
131137
// Safety: if the ss_family field is AF_INET then storage must be a
132138
// sockaddr_in.
133139
let addr: &libc::sockaddr_in = transmute(&storage);
134140
let ip = Ipv4Addr::from(addr.sin_addr.s_addr.to_ne_bytes());
135141
let port = u16::from_be(addr.sin_port);
136142
SocketAddr::V4(SocketAddrV4::new(ip, port))
137143
}
138-
libc::AF_INET6 => {
144+
AF_INET6 => {
139145
// Safety: if the ss_family field is AF_INET6 then storage must be a
140146
// sockaddr_in6.
141147
let addr: &libc::sockaddr_in6 = transmute(&storage);
@@ -214,7 +220,7 @@ impl<T: IoBufMut> Op<RecvMsgUnix<T>> {
214220
info.2.msg_iov = info.1.as_mut_ptr();
215221
info.2.msg_iovlen = 1;
216222
info.2.msg_name = &mut info.0 as *mut _ as *mut libc::c_void;
217-
info.2.msg_namelen = std::mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;
223+
info.2.msg_namelen = std::mem::size_of::<libc::sockaddr_storage>() as socklen_t;
218224

219225
Op::submit_with(RecvMsgUnix { fd, buf, info })
220226
}

monoio/src/driver/op/send.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
2+
use std::os::unix::prelude::AsRawFd;
13
use std::{io, net::SocketAddr};
24

35
#[cfg(all(target_os = "linux", feature = "iouring"))]
46
use io_uring::{opcode, types};
57
use socket2::SockAddr;
6-
#[cfg(any(feature = "legacy", feature = "poll-io"))]
8+
#[cfg(all(windows, any(feature = "legacy", feature = "poll-io")))]
79
use {
8-
crate::{driver::ready::Direction, syscall_u32},
9-
std::os::unix::prelude::AsRawFd,
10+
crate::syscall, std::os::windows::io::AsRawSocket,
11+
windows_sys::Win32::Networking::WinSock::send,
1012
};
1113

1214
use super::{super::shared_fd::SharedFd, Op, OpAble};
1315
use crate::{buf::IoBuf, net::unix::SocketAddr as UnixSocketAddr, BufResult};
16+
#[cfg(any(feature = "legacy", feature = "poll-io"))]
17+
use crate::{driver::ready::Direction, syscall_u32};
1418

1519
pub(crate) struct Send<T> {
1620
/// Holds a strong ref to the FD, preventing the file from being closed
@@ -84,19 +88,31 @@ impl<T: IoBuf> OpAble for Send<T> {
8488

8589
#[cfg(any(feature = "legacy", feature = "poll-io"))]
8690
fn legacy_call(&mut self) -> io::Result<u32> {
91+
#[cfg(unix)]
8792
let fd = self.fd.as_raw_fd();
93+
#[cfg(windows)]
94+
let fd = self.fd.as_raw_socket();
8895
#[cfg(target_os = "linux")]
8996
#[allow(deprecated)]
9097
let flags = libc::MSG_NOSIGNAL as _;
9198
#[cfg(not(target_os = "linux"))]
9299
let flags = 0;
93100

94-
syscall_u32!(send(
101+
#[cfg(windows)]
102+
return syscall!(send(
103+
fd,
104+
self.buf.read_ptr(),
105+
self.buf.bytes_init() as _,
106+
flags
107+
));
108+
109+
#[cfg(unix)]
110+
return syscall_u32!(send(
95111
fd,
96112
self.buf.read_ptr() as _,
97113
self.buf.bytes_init(),
98114
flags
99-
))
115+
));
100116
}
101117
}
102118

@@ -176,7 +192,10 @@ impl<T: IoBuf> OpAble for SendMsg<T> {
176192
const FLAGS: libc::c_int = libc::MSG_NOSIGNAL as libc::c_int;
177193
#[cfg(not(target_os = "linux"))]
178194
const FLAGS: libc::c_int = 0;
195+
#[cfg(unix)]
179196
let fd = self.fd.as_raw_fd();
197+
#[cfg(windows)]
198+
let fd = self.fd.as_raw_socket();
180199
syscall_u32!(sendmsg(fd, &mut self.info.2 as *mut _, FLAGS))
181200
}
182201
}
@@ -258,7 +277,10 @@ impl<T: IoBuf> OpAble for SendMsgUnix<T> {
258277
const FLAGS: libc::c_int = libc::MSG_NOSIGNAL as libc::c_int;
259278
#[cfg(not(target_os = "linux"))]
260279
const FLAGS: libc::c_int = 0;
280+
#[cfg(unix)]
261281
let fd = self.fd.as_raw_fd();
282+
#[cfg(windows)]
283+
let fd = self.fd.as_raw_socket();
262284
syscall_u32!(sendmsg(fd, &mut self.info.2 as *mut _, FLAGS))
263285
}
264286
}

0 commit comments

Comments
 (0)