Restructuring some windows files

This commit is contained in:
Andrew Golovashevich 2026-03-03 17:11:18 +03:00
parent f015f54760
commit bdecf3a6cf
5 changed files with 130 additions and 121 deletions

View File

@ -3,7 +3,7 @@ use std::mem::uninitialized;
use std::pin::Pin;
use std::ptr;
pub(super) struct Allocator<T> {
pub(crate) struct Allocator<T> {
map: HashMap<usize, Pin<Box<T>>>,
}

View File

@ -0,0 +1,123 @@
mod socket;
mod task;
mod allocator;
use socket::WindowsOverlappingIcmpSocket;
use crate::address::WindowsAddressKnown;
use allocator::Allocator;
use task::IoTask;
use bgtu_networks_2_network_abstract::ServersContext;
use std::mem::uninitialized;
use std::pin::Pin;
use std::ptr::NonNull;
use windows::Win32::System::IO::OVERLAPPED;
use windows::Win32::System::Threading::SleepEx;
pub unsafe fn run_eventloop<Ctx: ServersContext<WindowsAddressKnown>>(ctx: *mut Ctx) {
let mut heap = Allocator::<IoTask<CallbackData<_>>>::new();
let mut socket = WindowsOverlappingIcmpSocket::new();
let mut recv_task = NonNull::from_mut(&mut heap).as_mut().alloc();
CallbackData::init_recv(
&mut recv_task,
NonNull::new_unchecked(ctx),
NonNull::from_mut(&mut heap),
);
let mut is_receive_enabled = false;
loop {
let (id, it) = (*ctx).start_measuring();
let mut should_recv_being_enabled = false;
for address in it {
let mut task = NonNull::from_mut(&mut heap).as_mut().alloc();
CallbackData::init_send(
&mut task,
NonNull::new_unchecked(ctx),
NonNull::from_mut(&mut heap),
address,
);
socket.send(&mut task, Some(sending_done::<Ctx>));
should_recv_being_enabled = true;
}
if (!is_receive_enabled && should_recv_being_enabled) {
socket.receive(&mut recv_task, Some(receiving_done::<Ctx>));
is_receive_enabled = true;
}
SleepEx(1000, true);
}
}
struct CallbackData<Ctx: ServersContext<WindowsAddressKnown>> {
ctx: NonNull<Ctx>,
allocator: NonNull<Allocator<IoTask<Self>>>,
address: WindowsAddressKnown,
__flags: u32,
__addrs_size: i32,
}
impl<Ctx: ServersContext<WindowsAddressKnown>> CallbackData<Ctx> {
fn init_send(
task: &mut Pin<&mut IoTask<CallbackData<Ctx>>>,
ctx: NonNull<Ctx>,
allocator: NonNull<Allocator<IoTask<Self>>>,
address: WindowsAddressKnown,
) {
task.init(Self {
ctx,
allocator,
address,
__flags: unsafe { uninitialized() },
__addrs_size: unsafe { uninitialized() },
});
}
fn init_recv(
task: &mut Pin<&mut IoTask<CallbackData<Ctx>>>,
ctx: NonNull<Ctx>,
allocator: NonNull<Allocator<IoTask<Self>>>,
) {
task.init(Self {
ctx,
allocator,
address: unsafe { uninitialized() },
__flags: unsafe { uninitialized() },
__addrs_size: unsafe { uninitialized() },
});
}
}
unsafe extern "system" fn sending_done<Ctx: ServersContext<WindowsAddressKnown>>(
dwerror: u32,
cbtransferred: u32,
lpoverlapped: *mut OVERLAPPED,
dwflags: u32,
) {
let mut task = Pin::new_unchecked(
lpoverlapped
.cast::<IoTask<CallbackData<Ctx>>>()
.as_mut()
.unwrap(),
);
task.get_extra().allocator.as_mut().free(task);
}
unsafe extern "system" fn receiving_done<Ctx: ServersContext<WindowsAddressKnown>>(
dwerror: u32,
cbtransferred: u32,
lpoverlapped: *mut OVERLAPPED,
dwflags: u32,
) {
let mut task = Pin::new_unchecked(
lpoverlapped
.cast::<IoTask<CallbackData<Ctx>>>()
.as_mut()
.unwrap(),
);
task.get_extra().allocator.as_mut().free(task);
}

View File

@ -1,9 +1,8 @@
use crate::address::WindowsAddressKnown;
use crate::allocator::Allocator;
use crate::errors::throw_from_windows_err_code;
use crate::task::IoTask;
use crate::eventloop::CallbackData;
use crate::eventloop::task::IoTask;
use bgtu_networks_2_network_abstract::ServersContext;
use std::mem::uninitialized;
use std::pin::Pin;
use std::ptr::NonNull;
use windows::Win32::Networking::WinSock::{
@ -15,123 +14,12 @@ use windows::Win32::Networking::WinSock::{
WSAENOTSOCK, WSAESHUTDOWN, WSAEWOULDBLOCK, WSAGetLastError, WSANOTINITIALISED, WSARecvFrom,
WSASendTo, WSASocketW,
};
use windows::Win32::System::IO::OVERLAPPED;
use windows::Win32::System::Threading::SleepEx;
pub unsafe fn run_eventloop<Ctx: ServersContext<WindowsAddressKnown>>(ctx: *mut Ctx) {
let mut heap = Allocator::<IoTask<CallbackData<_>>>::new();
let mut socket = WindowsOverlappingIcmpSocket::new();
let mut recv_task = NonNull::from_mut(&mut heap).as_mut().alloc();
CallbackData::init_recv(
&mut recv_task,
NonNull::new_unchecked(ctx),
NonNull::from_mut(&mut heap),
);
let mut is_receive_enabled = false;
loop {
let (id, it) = (*ctx).start_measuring();
let mut should_recv_being_enabled = false;
for address in it {
let mut task = NonNull::from_mut(&mut heap).as_mut().alloc();
CallbackData::init_send(
&mut task,
NonNull::new_unchecked(ctx),
NonNull::from_mut(&mut heap),
address,
);
socket.send(&mut task, Some(sending_done::<Ctx>));
should_recv_being_enabled = true;
}
if (!is_receive_enabled && should_recv_being_enabled) {
socket.receive(&mut recv_task, Some(receiving_done::<Ctx>));
is_receive_enabled = true;
}
SleepEx(1000, true);
}
}
struct CallbackData<Ctx: ServersContext<WindowsAddressKnown>> {
ctx: NonNull<Ctx>,
allocator: NonNull<Allocator<IoTask<Self>>>,
address: WindowsAddressKnown,
__flags: u32,
__addrs_size: i32,
}
impl<Ctx: ServersContext<WindowsAddressKnown>> CallbackData<Ctx> {
fn init_send(
task: &mut Pin<&mut IoTask<CallbackData<Ctx>>>,
ctx: NonNull<Ctx>,
allocator: NonNull<Allocator<IoTask<Self>>>,
address: WindowsAddressKnown,
) {
task.init(Self {
ctx,
allocator,
address,
__flags: unsafe { uninitialized() },
__addrs_size: unsafe { uninitialized() },
});
}
fn init_recv(
task: &mut Pin<&mut IoTask<CallbackData<Ctx>>>,
ctx: NonNull<Ctx>,
allocator: NonNull<Allocator<IoTask<Self>>>,
) {
task.init(Self {
ctx,
allocator,
address: unsafe { uninitialized() },
__flags: unsafe { uninitialized() },
__addrs_size: unsafe { uninitialized() },
});
}
}
unsafe extern "system" fn sending_done<Ctx: ServersContext<WindowsAddressKnown>>(
dwerror: u32,
cbtransferred: u32,
lpoverlapped: *mut OVERLAPPED,
dwflags: u32,
) {
let mut task = Pin::new_unchecked(
lpoverlapped
.cast::<IoTask<CallbackData<Ctx>>>()
.as_mut()
.unwrap(),
);
task.get_extra().allocator.as_mut().free(task);
}
unsafe extern "system" fn receiving_done<Ctx: ServersContext<WindowsAddressKnown>>(
dwerror: u32,
cbtransferred: u32,
lpoverlapped: *mut OVERLAPPED,
dwflags: u32,
) {
let mut task = Pin::new_unchecked(
lpoverlapped
.cast::<IoTask<CallbackData<Ctx>>>()
.as_mut()
.unwrap(),
);
task.get_extra().allocator.as_mut().free(task);
}
struct WindowsOverlappingIcmpSocket {
pub(super) struct WindowsOverlappingIcmpSocket {
s: SOCKET,
}
impl WindowsOverlappingIcmpSocket {
fn new() -> Self {
pub fn new() -> Self {
let result = unsafe {
WSASocketW(
AF_UNSPEC.0 as i32,
@ -149,7 +37,7 @@ impl WindowsOverlappingIcmpSocket {
}
}
unsafe fn send<Ctx: ServersContext<WindowsAddressKnown>>(
pub unsafe fn send<Ctx: ServersContext<WindowsAddressKnown>>(
&mut self,
task: &mut Pin<&mut IoTask<CallbackData<Ctx>>>,
cb: LPWSAOVERLAPPED_COMPLETION_ROUTINE,
@ -200,7 +88,7 @@ impl WindowsOverlappingIcmpSocket {
}
}
unsafe fn receive<Ctx: ServersContext<WindowsAddressKnown>>(
pub unsafe fn receive<Ctx: ServersContext<WindowsAddressKnown>>(
&mut self,
task: &mut Pin<&mut IoTask<CallbackData<Ctx>>>,
cb: LPWSAOVERLAPPED_COMPLETION_ROUTINE,

View File

@ -1,8 +1,6 @@
mod address;
mod errors;
mod task;
mod eventloop;
mod allocator;
mod entry_point;
mod network_context;