From b914827122fcbc325864f0821ca008f752e3055f Mon Sep 17 00:00:00 2001 From: Jin Wei Tan <48111396+CarrotzRule123@users.noreply.github.com> Date: Tue, 25 Jul 2023 10:33:46 +0800 Subject: [PATCH] feat: implement poll op on windows (#198) --- README-zh.md | 2 +- README.md | 2 +- monoio/src/driver/op/poll.rs | 44 ++++++++++++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/README-zh.md b/README-zh.md index 6958f1d6..17a5c6ef 100644 --- a/README-zh.md +++ b/README-zh.md @@ -35,7 +35,7 @@ Monoio 就是这样一个 Runtime:它并不像 Tokio 那样通过公平调度 同时,如果你想使用 io_uring,你需要确保你当前的内核版本是较新的([5.6+](docs/zh/platform-support.md));并且 memlock 是一个[合适的配置](docs/zh/memlock.md)。如果你的内核版本不满足需求,可以尝试使用 legacy driver 启动([参考这里](/docs/zh/use-legacy-driver.md)),当前支持 Linux 和 macOS。 -🚧实验性的 windows 系统支持正在开发中,你需要确保你的 windows 版本支持 ([Windows Build 22000](https://docs.microsoft.com/zh-CN/windows/win32/api/ioringapi/ns-ioringapi-ioring_capabilities))。 +🚧实验性的 windows 系统支持正在开发中。 这是一个非常简单的例子,基于 Monoio 实现一个简单的 echo 服务。运行起来之后你可以通过 `nc 127.0.0.1 50002` 来连接它。 diff --git a/README.md b/README.md index 07496275..512fa26c 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ To force using nightly, create a file named `rust-toolchain` and write `nightly` Also, if you want to use io_uring, you must make sure your kernel supports it([5.6+](docs/en/platform-support.md)). And, memlock is [configured as a proper number](docs/en/memlock.md). If your kernel version does not meet the requirements, you can try to use the legacy driver to start, currently supports Linux and macOS([ref here](/docs/en/use-legacy-driver.md)). -🚧Experimental windows support is on the way, if you want to use windows you must make sure your windows supports it([Windows Build 22000](https://docs.microsoft.com/en-us/windows/win32/api/ioringapi/ns-ioringapi-ioring_capabilities)). +🚧Experimental windows support is on the way. Here is a basic example of how to use Monoio. diff --git a/monoio/src/driver/op/poll.rs b/monoio/src/driver/op/poll.rs index 6c777184..4069bb02 100644 --- a/monoio/src/driver/op/poll.rs +++ b/monoio/src/driver/op/poll.rs @@ -1,10 +1,19 @@ use std::io; +#[cfg(windows)] +use std::{ + io::{Error, ErrorKind}, + os::windows::prelude::AsRawSocket, +}; #[cfg(all(target_os = "linux", feature = "iouring"))] use io_uring::{opcode, types}; +#[cfg(windows)] +use windows_sys::Win32::Networking::WinSock::{ + WSAGetLastError, WSAPoll, POLLIN, POLLOUT, SOCKET_ERROR, WSAPOLLFD, +}; use super::{super::shared_fd::SharedFd, Op, OpAble}; -#[cfg(all(unix, feature = "legacy"))] +#[cfg(feature = "legacy")] use crate::driver::legacy::ready::Direction; pub(crate) struct PollAdd { @@ -14,7 +23,7 @@ pub(crate) struct PollAdd { fd: SharedFd, // true: read; false: write is_read: bool, - #[cfg(all(unix, feature = "legacy"))] + #[cfg(feature = "legacy")] relaxed: bool, } @@ -23,7 +32,7 @@ impl Op { Op::submit_with(PollAdd { fd: fd.clone(), is_read: true, - #[cfg(all(unix, feature = "legacy"))] + #[cfg(feature = "legacy")] relaxed: _relaxed, }) } @@ -32,7 +41,7 @@ impl Op { Op::submit_with(PollAdd { fd: fd.clone(), is_read: false, - #[cfg(all(unix, feature = "legacy"))] + #[cfg(feature = "legacy")] relaxed: _relaxed, }) } @@ -57,7 +66,7 @@ impl OpAble for PollAdd { .build() } - #[cfg(all(unix, feature = "legacy"))] + #[cfg(feature = "legacy")] fn legacy_interest(&self) -> Option<(Direction, usize)> { self.fd.registered_index().map(|idx| { ( @@ -92,4 +101,29 @@ impl OpAble for PollAdd { } Ok(0) } + + #[cfg(windows)] + fn legacy_call(&mut self) -> io::Result { + if !self.relaxed { + let mut pollfd = WSAPOLLFD { + fd: self.fd.as_raw_socket(), + events: if self.is_read { + POLLIN as _ + } else { + POLLOUT as _ + }, + revents: 0, + }; + let ret = unsafe { WSAPoll(&mut pollfd as *mut _, 1, 0) }; + match ret { + 0 => return Err(ErrorKind::WouldBlock.into()), + SOCKET_ERROR => { + let error = unsafe { WSAGetLastError() }; + return Err(Error::from_raw_os_error(error)); + } + _ => (), + } + } + Ok(0) + } }