Splitting codebase to modules

This commit is contained in:
Andrew Golovashevich 2026-03-02 23:11:03 +03:00
parent ffe2cc2f51
commit d3c4e684ff
24 changed files with 111 additions and 92 deletions

View File

@ -1,22 +1,10 @@
[package]
name = "bgtu-networks-2"
version = "0.0.0"
edition = "2024"
[lints]
rust = { nonstandard_style = "allow", unsafe_op_in_unsafe_fn = "allow" }
[dependencies]
eframe = { version = "0.33.3", default-features = false, features = ["default_fonts", "glow"] }
egui_extras = { version = "0.33.3" }
[dependencies.windows]
version = ">=0.41.0, <=0.62.2"
registry = "crates-io"
features = [
"Win32_System_IO",
"Win32_Networking",
"Win32_Networking_WinSock",
"Win32_System_Diagnostics",
"Win32_System_Diagnostics_Debug"
[workspace]
resolver = "3"
members = [
"app",
"network/abstract",
"network/windows"
]
[workspace.lints]
rust = { nonstandard_style = "allow", unsafe_op_in_unsafe_fn = "allow" }

11
app/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "bgtu-networks-2-app"
version = "0.0.0"
edition = "2024"
[lints]
workspace = true
[dependencies]
eframe = { version = "0.33.3", default-features = false, features = ["default_fonts", "glow"] }
egui_extras = { version = "0.33.3" }

2
app/src/data/mod.rs Normal file
View File

@ -0,0 +1,2 @@
mod cycle_buffer;
mod server;

8
app/src/data/server.rs Normal file
View File

@ -0,0 +1,8 @@
use crate::io::Address;
use crate::data::cycle_buffer::CycledBuffer;
pub struct Server {
address: Address,
history: CycledBuffer<u16>,
label: String,
}

3
app/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
mod data;
fn main() {}

View File

@ -0,0 +1,7 @@
[package]
name = "bgtu-networks-2-network-abstract"
version = "0.0.0"
edition = "2024"
[lints]
workspace = true

View File

@ -0,0 +1,24 @@
pub trait Address: Sized {
fn parse(raw: &str) -> Result<Self, String>;
fn to_string(self) -> String;
}
pub trait NetworkContext {
type Address: Address;
fn run_ping_eventloop(ctx: impl ServersContext<Self::Address>);
}
pub trait ServersContext<A: Address> {
fn start_measuring(&mut self) -> (u64, impl Iterator<Item = A>);
fn report_ping(&mut self, addr: A, id: u64, ping_ms: u64);
fn report_error(&mut self, addr: A, id: u64);
}
pub trait NetworkScope {
type R;
fn run(self, network: impl NetworkContext) -> Self::R;
}

View File

@ -0,0 +1,21 @@
[package]
name = "bgtu-networks-2-network-windows"
version = "0.0.0"
edition = "2024"
[lints]
workspace = true
[dependencies]
bgtu-networks-2-network-abstract = { path = "../abstract" }
[dependencies.windows]
version = ">=0.41.0, <=0.62.2"
registry = "crates-io"
features = [
"Win32_System_IO",
"Win32_Networking",
"Win32_Networking_WinSock",
"Win32_System_Diagnostics",
"Win32_System_Diagnostics_Debug"
]

View File

@ -1,5 +1,5 @@
use crate::io::r#abstract::Address;
use crate::io::windows::errors::{format_windows_err_code_result, throw_from_windows_err_code};
use bgtu_networks_2_network_abstract::Address;
use crate::errors::{format_windows_err_code_result, throw_from_windows_err_code};
use std::ffi::OsString;
use std::hash::{Hash, Hasher};
use std::mem::size_of;

View File

@ -1,23 +1,23 @@
use crate::io::allocator::Allocator;
use crate::io::servers_context::ServersContext;
use crate::io::windows::WindowsAddressKnown;
use crate::io::windows::errors::throw_from_windows_err_code;
use crate::io::windows::task::IoTask;
use crate::address::WindowsAddressKnown;
use crate::allocator::Allocator;
use crate::errors::throw_from_windows_err_code;
use crate::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::{
AF_UNSPEC, IPPROTO_ICMP, LPWSAOVERLAPPED_COMPLETION_ROUTINE, SOCK_RAW, SOCKADDR,
SOCKADDR_STORAGE, SOCKET, SOCKET_ERROR, WSA_FLAG_NO_HANDLE_INHERIT,
WSA_FLAG_OVERLAPPED, WSA_IO_PENDING, WSA_OPERATION_ABORTED, WSAEACCES, WSAEADDRNOTAVAIL,
WSAEAFNOSUPPORT, WSAECONNRESET, WSAEDESTADDRREQ, WSAEFAULT, WSAEHOSTUNREACH, WSAEINPROGRESS,
WSAEINTR, WSAEINVAL, WSAEMSGSIZE, WSAENETDOWN, WSAENETRESET, WSAENETUNREACH, WSAENOBUFS,
WSAENOTCONN, WSAENOTSOCK, WSAESHUTDOWN, WSAEWOULDBLOCK, WSAGetLastError, WSANOTINITIALISED,
WSARecvFrom, WSASendTo, WSASocketW,
SOCKADDR_STORAGE, SOCKET, SOCKET_ERROR, WSA_FLAG_NO_HANDLE_INHERIT, WSA_FLAG_OVERLAPPED,
WSA_IO_PENDING, WSA_OPERATION_ABORTED, WSAEACCES, WSAEADDRNOTAVAIL, WSAEAFNOSUPPORT,
WSAECONNRESET, WSAEDESTADDRREQ, WSAEFAULT, WSAEHOSTUNREACH, WSAEINPROGRESS, WSAEINTR,
WSAEINVAL, WSAEMSGSIZE, WSAENETDOWN, WSAENETRESET, WSAENETUNREACH, WSAENOBUFS, WSAENOTCONN,
WSAENOTSOCK, WSAESHUTDOWN, WSAEWOULDBLOCK, WSAGetLastError, WSANOTINITIALISED, WSARecvFrom,
WSASendTo, WSASocketW,
};
use windows::Win32::System::IO::OVERLAPPED;
pub unsafe fn run_eventloop<Ctx: ServersContext>(ctx: *mut Ctx) {
pub unsafe fn run_eventloop<Ctx: ServersContext<WindowsAddressKnown>>(ctx: *mut Ctx) {
let mut heap = Allocator::<IoTask<CallbackData<_>>>::new();
let mut socket = WindowsOverlappingIcmpSocket::new();
@ -47,7 +47,7 @@ pub unsafe fn run_eventloop<Ctx: ServersContext>(ctx: *mut Ctx) {
}
}
struct CallbackData<Ctx: ServersContext> {
struct CallbackData<Ctx: ServersContext<WindowsAddressKnown>> {
ctx: NonNull<Ctx>,
allocator: NonNull<Allocator<IoTask<Self>>>,
address: WindowsAddressKnown,
@ -55,7 +55,7 @@ struct CallbackData<Ctx: ServersContext> {
__addrs_size: i32,
}
impl<Ctx: ServersContext> CallbackData<Ctx> {
impl<Ctx: ServersContext<WindowsAddressKnown>> CallbackData<Ctx> {
fn init_send(
task: &mut Pin<&mut IoTask<CallbackData<Ctx>>>,
ctx: NonNull<Ctx>,
@ -85,7 +85,7 @@ impl<Ctx: ServersContext> CallbackData<Ctx> {
}
}
unsafe extern "system" fn sending_done<Ctx: ServersContext>(
unsafe extern "system" fn sending_done<Ctx: ServersContext<WindowsAddressKnown>>(
dwerror: u32,
cbtransferred: u32,
lpoverlapped: *mut OVERLAPPED,
@ -100,7 +100,7 @@ unsafe extern "system" fn sending_done<Ctx: ServersContext>(
task.get_extra().allocator.as_mut().free(task);
}
unsafe extern "system" fn receiving_done<Ctx: ServersContext>(
unsafe extern "system" fn receiving_done<Ctx: ServersContext<WindowsAddressKnown>>(
dwerror: u32,
cbtransferred: u32,
lpoverlapped: *mut OVERLAPPED,
@ -138,7 +138,7 @@ impl WindowsOverlappingIcmpSocket {
}
}
unsafe fn send<Ctx: ServersContext>(
unsafe fn send<Ctx: ServersContext<WindowsAddressKnown>>(
&mut self,
task: &mut Pin<&mut IoTask<CallbackData<Ctx>>>,
cb: LPWSAOVERLAPPED_COMPLETION_ROUTINE,
@ -189,7 +189,7 @@ impl WindowsOverlappingIcmpSocket {
}
}
unsafe fn receive<Ctx: ServersContext>(
unsafe fn receive<Ctx: ServersContext<WindowsAddressKnown>>(
&mut self,
task: &mut Pin<&mut IoTask<CallbackData<Ctx>>>,
cb: LPWSAOVERLAPPED_COMPLETION_ROUTINE,

View File

@ -0,0 +1,6 @@
mod address;
mod errors;
mod task;
mod eventloop;
mod allocator;
mod winsocks_scope;

View File

@ -2,7 +2,7 @@ use super::errors::throw_from_windows_err_code;
use std::mem::uninitialized;
use windows::Win32::Networking::WinSock::{WSACleanup, WSAStartup};
pub(in super::super) fn winsocks_scope<R>(scope: impl FnOnce() -> R) -> R {
pub(in crate) fn winsocks_scope<R>(scope: impl FnOnce() -> R) -> R {
unsafe {
match WSAStartup(0x0202, &mut uninitialized()) {
0 => {}

View File

@ -1 +0,0 @@
mod cycle_buffer;

View File

@ -1,7 +0,0 @@
use std::hash::Hash;
pub trait Address: Sized + PartialEq + Hash {
fn parse(raw: &str) -> Result<Self, String>;
fn to_string(self) -> String;
}

View File

@ -1,4 +0,0 @@
mod address;
mod context;
pub use address::Address;

View File

@ -1,12 +0,0 @@
pub mod r#abstract;
mod allocator;
mod windows;
mod servers_context;
pub type Address = windows::WindowsAddressKnown;
use allocator::Allocator;
use crate::io::windows::winsocks_scope;
pub(crate) fn network_scope<R>(scope: impl FnOnce() -> R) -> R {
return winsocks_scope(scope);
}

View File

@ -1,9 +0,0 @@
use super::Address;
pub trait ServersContext {
fn start_measuring(&mut self) -> (u64, impl Iterator<Item = Address>);
fn report_ping(&mut self, addr: Address, id: u64, ping_ms: u64);
fn report_error(&mut self, addr: Address, id: u64);
}

View File

@ -1,8 +0,0 @@
mod address;
mod errors;
mod eventloop;
mod task;
mod winsocks_scope;
pub use address::{WindowsAddressAny, WindowsAddressKnown};
pub(super) use winsocks_scope::winsocks_scope;

View File

@ -1,10 +0,0 @@
use crate::io::network_scope;
mod data;
mod io;
fn main() {
network_scope(|| {
println!("Hello, world!");
});
}