Initialise the Window network stack in one call site

Do it in new_socket so that the higher levels don't have to worry about
it.
This commit is contained in:
Thomas de Zeeuw
2022-11-28 11:37:49 +01:00
parent 427ab37712
commit b7a09f21a9
3 changed files with 7 additions and 13 deletions
+3 -1
View File
@@ -9,7 +9,7 @@ use windows_sys::Win32::Networking::WinSock::{
};
/// Initialise the network stack for Windows.
pub(crate) fn init() {
fn init() {
static INIT: Once = Once::new();
INIT.call_once(|| {
// Let standard library call `WSAStartup` for us, we can't do it
@@ -30,6 +30,8 @@ pub(crate) fn new_ip_socket(addr: SocketAddr, socket_type: u16) -> io::Result<SO
}
pub(crate) fn new_socket(domain: u32, socket_type: u16) -> io::Result<SOCKET> {
init();
syscall!(
socket(domain as i32, socket_type as i32, 0),
PartialEq::eq,
+3 -10
View File
@@ -2,19 +2,12 @@ use std::io;
use std::net::{self, SocketAddr};
use std::os::windows::io::AsRawSocket;
use windows_sys::Win32::Networking::WinSock::{
self, AF_INET, AF_INET6, SOCKET, SOCKET_ERROR, SOCK_STREAM,
};
use windows_sys::Win32::Networking::WinSock::{self, SOCKET, SOCKET_ERROR, SOCK_STREAM};
use crate::sys::windows::net::{init, new_socket, socket_addr};
use crate::sys::windows::net::{new_ip_socket, socket_addr};
pub(crate) fn new_for_addr(address: SocketAddr) -> io::Result<SOCKET> {
init();
let domain = match address {
SocketAddr::V4(_) => AF_INET,
SocketAddr::V6(_) => AF_INET6,
};
new_socket(domain, SOCK_STREAM)
new_ip_socket(address, SOCK_STREAM)
}
pub(crate) fn bind(socket: &net::TcpListener, addr: SocketAddr) -> io::Result<()> {
+1 -2
View File
@@ -4,13 +4,12 @@ use std::net::{self, SocketAddr};
use std::os::windows::io::{AsRawSocket, FromRawSocket};
use std::os::windows::raw::SOCKET as StdSocket; // windows-sys uses usize, stdlib uses u32/u64.
use crate::sys::windows::net::{init, new_ip_socket, socket_addr};
use crate::sys::windows::net::{new_ip_socket, socket_addr};
use windows_sys::Win32::Networking::WinSock::{
bind as win_bind, closesocket, getsockopt, IPPROTO_IPV6, IPV6_V6ONLY, SOCKET_ERROR, SOCK_DGRAM,
};
pub fn bind(addr: SocketAddr) -> io::Result<net::UdpSocket> {
init();
new_ip_socket(addr, SOCK_DGRAM).and_then(|socket| {
let (raw_addr, raw_addr_length) = socket_addr(&addr);
syscall!(