mirror of
https://github.com/wezterm/wezterm.git
synced 2026-11-08 01:21:45 +08:00
filedescriptor: Replace winapi with windows-sys (#8026)
This commit is contained in:
parent
e70244973c
commit
1300cfb9b2
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1744,7 +1744,7 @@ version = "0.8.3"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"thiserror 1.0.69",
|
||||
"winapi",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@ -266,6 +266,7 @@ winapi = "0.3.9"
|
||||
window = {path="window"}
|
||||
window-funcs = { path = "lua-api-crates/window-funcs" }
|
||||
windows = "0.33.0"
|
||||
windows-sys = { version = "0.61.2", default-features = false }
|
||||
winreg = "0.10"
|
||||
x11 = "2.21"
|
||||
xcb = "1.3"
|
||||
|
||||
@ -15,12 +15,13 @@ thiserror.workspace = true
|
||||
libc.workspace = true
|
||||
|
||||
[target."cfg(windows)".dependencies]
|
||||
winapi = { workspace=true, features = [
|
||||
"winuser",
|
||||
"handleapi",
|
||||
"fileapi",
|
||||
"namedpipeapi",
|
||||
"processthreadsapi",
|
||||
"winsock2",
|
||||
"processenv"
|
||||
windows-sys = { workspace=true, features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_Networking_WinSock",
|
||||
"Win32_Security",
|
||||
"Win32_Storage_FileSystem",
|
||||
"Win32_System_Console",
|
||||
"Win32_System_IO",
|
||||
"Win32_System_Pipes",
|
||||
"Win32_System_Threading",
|
||||
]}
|
||||
|
||||
@ -8,21 +8,25 @@ use std::os::windows::prelude::*;
|
||||
use std::ptr;
|
||||
use std::sync::Once;
|
||||
use std::time::Duration;
|
||||
use winapi::shared::ws2def::{AF_INET, INADDR_LOOPBACK, SOCKADDR_IN};
|
||||
use winapi::um::fileapi::*;
|
||||
use winapi::um::handleapi::*;
|
||||
use winapi::um::minwinbase::SECURITY_ATTRIBUTES;
|
||||
use winapi::um::namedpipeapi::{CreatePipe, GetNamedPipeInfo};
|
||||
use winapi::um::processenv::{GetStdHandle, SetStdHandle};
|
||||
use winapi::um::processthreadsapi::*;
|
||||
use winapi::um::winbase::{FILE_TYPE_CHAR, FILE_TYPE_DISK, FILE_TYPE_PIPE};
|
||||
use winapi::um::winnt::HANDLE;
|
||||
use winapi::um::winsock2::{
|
||||
accept, bind, closesocket, connect, getsockname, getsockopt, htonl, ioctlsocket, listen, recv,
|
||||
send, WSAGetLastError, WSAPoll, WSASocketW, WSAStartup, INVALID_SOCKET, SOCKET, SOCK_STREAM,
|
||||
SOL_SOCKET, SO_ERROR, WSADATA, WSAENOTSOCK, WSA_FLAG_NO_HANDLE_INHERIT,
|
||||
use windows_sys::Win32::Foundation::{
|
||||
CloseHandle, DuplicateHandle, DUPLICATE_SAME_ACCESS, HANDLE, INVALID_HANDLE_VALUE,
|
||||
};
|
||||
pub use winapi::um::winsock2::{POLLERR, POLLHUP, POLLIN, POLLOUT, WSAPOLLFD as pollfd};
|
||||
use windows_sys::Win32::Networking::WinSock::{
|
||||
accept, bind, closesocket, connect, getsockname, getsockopt, htonl, ioctlsocket, listen, recv,
|
||||
send, WSAGetLastError, WSAPoll, WSASocketW, WSAStartup, ADDRESS_FAMILY, AF_INET, FIONBIO,
|
||||
INADDR_LOOPBACK, INVALID_SOCKET, SOCKADDR_IN, SOCKET, SOCK_STREAM, SOL_SOCKET, SO_ERROR,
|
||||
WSADATA, WSAENOTSOCK, WSA_FLAG_NO_HANDLE_INHERIT,
|
||||
};
|
||||
pub use windows_sys::Win32::Networking::WinSock::{
|
||||
POLLERR, POLLHUP, POLLIN, POLLOUT, WSAPOLLFD as pollfd,
|
||||
};
|
||||
use windows_sys::Win32::Security::SECURITY_ATTRIBUTES;
|
||||
use windows_sys::Win32::Storage::FileSystem::{
|
||||
GetFileType, ReadFile, WriteFile, FILE_TYPE_CHAR, FILE_TYPE_DISK, FILE_TYPE_PIPE,
|
||||
};
|
||||
use windows_sys::Win32::System::Console::{GetStdHandle, SetStdHandle};
|
||||
use windows_sys::Win32::System::Pipes::{CreatePipe, GetNamedPipeInfo};
|
||||
use windows_sys::Win32::System::Threading::GetCurrentProcess;
|
||||
|
||||
/// `RawFileDescriptor` is a platform independent type alias for the
|
||||
/// underlying platform file descriptor type. It is primarily useful
|
||||
@ -126,7 +130,7 @@ impl OwnedHandle {
|
||||
handle as _,
|
||||
SOL_SOCKET,
|
||||
SO_ERROR,
|
||||
&mut err as *mut _ as *mut i8,
|
||||
&mut err as *mut _ as *mut _,
|
||||
&mut errsize,
|
||||
) != 0
|
||||
&& WSAGetLastError() == WSAENOTSOCK
|
||||
@ -196,7 +200,7 @@ impl OwnedHandle {
|
||||
&mut duped,
|
||||
0,
|
||||
0, // not inheritable
|
||||
winapi::um::winnt::DUPLICATE_SAME_ACCESS,
|
||||
DUPLICATE_SAME_ACCESS,
|
||||
)
|
||||
};
|
||||
if ok == 0 {
|
||||
@ -254,13 +258,7 @@ impl FileDescriptor {
|
||||
}
|
||||
|
||||
let mut on = if non_blocking { 1 } else { 0 };
|
||||
let res = unsafe {
|
||||
ioctlsocket(
|
||||
self.as_raw_socket() as SOCKET,
|
||||
winapi::um::winsock2::FIONBIO,
|
||||
&mut on,
|
||||
)
|
||||
};
|
||||
let res = unsafe { ioctlsocket(self.as_raw_socket() as SOCKET, FIONBIO, &mut on) };
|
||||
if res != 0 {
|
||||
Err(Error::FionBio(std::io::Error::last_os_error()))
|
||||
} else {
|
||||
@ -327,7 +325,7 @@ impl AsRawSocket for FileDescriptor {
|
||||
}
|
||||
|
||||
impl AsSocket for FileDescriptor {
|
||||
fn as_socket(&self) -> BorrowedSocket {
|
||||
fn as_socket(&self) -> BorrowedSocket<'_> {
|
||||
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
|
||||
}
|
||||
}
|
||||
@ -465,10 +463,10 @@ fn init_winsock() {
|
||||
});
|
||||
}
|
||||
|
||||
fn socket(af: i32, sock_type: i32, proto: i32) -> Result<FileDescriptor> {
|
||||
fn socket(af: ADDRESS_FAMILY, sock_type: i32, proto: i32) -> Result<FileDescriptor> {
|
||||
let s = unsafe {
|
||||
WSASocketW(
|
||||
af,
|
||||
af as _,
|
||||
sock_type,
|
||||
proto,
|
||||
ptr::null_mut(),
|
||||
@ -495,10 +493,8 @@ pub fn socketpair_impl() -> Result<(FileDescriptor, FileDescriptor)> {
|
||||
let s = socket(AF_INET, SOCK_STREAM, 0)?;
|
||||
|
||||
let mut in_addr: SOCKADDR_IN = unsafe { std::mem::zeroed() };
|
||||
in_addr.sin_family = AF_INET as _;
|
||||
unsafe {
|
||||
*in_addr.sin_addr.S_un.S_addr_mut() = htonl(INADDR_LOOPBACK);
|
||||
}
|
||||
in_addr.sin_family = AF_INET;
|
||||
in_addr.sin_addr.S_un.S_addr = unsafe { htonl(INADDR_LOOPBACK) };
|
||||
|
||||
unsafe {
|
||||
if bind(
|
||||
|
||||
@ -57,6 +57,7 @@ winapi = { workspace=true, features = [
|
||||
"psapi",
|
||||
"processthreadsapi",
|
||||
"tlhelp32",
|
||||
"winsock2"
|
||||
]}
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@ -33,6 +33,7 @@ winapi = { workspace=true, features = [
|
||||
"handleapi",
|
||||
"fileapi",
|
||||
"namedpipeapi",
|
||||
"processenv",
|
||||
"synchapi",
|
||||
]}
|
||||
winreg.workspace = true
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user