From d3c4e684ff345619730c4140bc746cbdc1a790fe Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Mon, 2 Mar 2026 23:11:03 +0300 Subject: [PATCH] Splitting codebase to modules --- Cargo.toml | 28 +++++---------- app/Cargo.toml | 11 ++++++ {src => app/src}/data/cycle_buffer.rs | 0 app/src/data/mod.rs | 2 ++ app/src/data/server.rs | 8 +++++ app/src/main.rs | 3 ++ network/abstract/Cargo.toml | 7 ++++ network/abstract/src/lib.rs | 24 +++++++++++++ network/windows/Cargo.toml | 21 +++++++++++ .../windows/src}/address.rs | 4 +-- {src/io => network/windows/src}/allocator.rs | 0 .../windows => network/windows/src}/errors.rs | 0 .../windows/src}/eventloop.rs | 36 +++++++++---------- network/windows/src/lib.rs | 6 ++++ .../windows => network/windows/src}/task.rs | 0 .../windows/src}/winsocks_scope.rs | 2 +- src/data/mod.rs | 1 - src/io/abstract/address.rs | 7 ---- src/io/abstract/context.rs | 0 src/io/abstract/mod.rs | 4 --- src/io/mod.rs | 12 ------- src/io/servers_context.rs | 9 ----- src/io/windows/mod.rs | 8 ----- src/main.rs | 10 ------ 24 files changed, 111 insertions(+), 92 deletions(-) create mode 100644 app/Cargo.toml rename {src => app/src}/data/cycle_buffer.rs (100%) create mode 100644 app/src/data/mod.rs create mode 100644 app/src/data/server.rs create mode 100644 app/src/main.rs create mode 100644 network/abstract/Cargo.toml create mode 100644 network/abstract/src/lib.rs create mode 100644 network/windows/Cargo.toml rename {src/io/windows => network/windows/src}/address.rs (97%) rename {src/io => network/windows/src}/allocator.rs (100%) rename {src/io/windows => network/windows/src}/errors.rs (100%) rename {src/io/windows => network/windows/src}/eventloop.rs (84%) create mode 100644 network/windows/src/lib.rs rename {src/io/windows => network/windows/src}/task.rs (100%) rename {src/io/windows => network/windows/src}/winsocks_scope.rs (86%) delete mode 100644 src/data/mod.rs delete mode 100644 src/io/abstract/address.rs delete mode 100644 src/io/abstract/context.rs delete mode 100644 src/io/abstract/mod.rs delete mode 100644 src/io/mod.rs delete mode 100644 src/io/servers_context.rs delete mode 100644 src/io/windows/mod.rs delete mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 7a3e045..d8e48c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,22 +1,10 @@ -[package] -name = "bgtu-networks-2" -version = "0.0.0" -edition = "2024" +[workspace] +resolver = "3" +members = [ + "app", + "network/abstract", + "network/windows" +] -[lints] +[workspace.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" -] \ No newline at end of file diff --git a/app/Cargo.toml b/app/Cargo.toml new file mode 100644 index 0000000..b6bc145 --- /dev/null +++ b/app/Cargo.toml @@ -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" } diff --git a/src/data/cycle_buffer.rs b/app/src/data/cycle_buffer.rs similarity index 100% rename from src/data/cycle_buffer.rs rename to app/src/data/cycle_buffer.rs diff --git a/app/src/data/mod.rs b/app/src/data/mod.rs new file mode 100644 index 0000000..5111c9f --- /dev/null +++ b/app/src/data/mod.rs @@ -0,0 +1,2 @@ +mod cycle_buffer; +mod server; diff --git a/app/src/data/server.rs b/app/src/data/server.rs new file mode 100644 index 0000000..cda7041 --- /dev/null +++ b/app/src/data/server.rs @@ -0,0 +1,8 @@ +use crate::io::Address; +use crate::data::cycle_buffer::CycledBuffer; + +pub struct Server { + address: Address, + history: CycledBuffer, + label: String, +} diff --git a/app/src/main.rs b/app/src/main.rs new file mode 100644 index 0000000..39f0d73 --- /dev/null +++ b/app/src/main.rs @@ -0,0 +1,3 @@ +mod data; + +fn main() {} \ No newline at end of file diff --git a/network/abstract/Cargo.toml b/network/abstract/Cargo.toml new file mode 100644 index 0000000..f52870b --- /dev/null +++ b/network/abstract/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "bgtu-networks-2-network-abstract" +version = "0.0.0" +edition = "2024" + +[lints] +workspace = true diff --git a/network/abstract/src/lib.rs b/network/abstract/src/lib.rs new file mode 100644 index 0000000..9560461 --- /dev/null +++ b/network/abstract/src/lib.rs @@ -0,0 +1,24 @@ +pub trait Address: Sized { + fn parse(raw: &str) -> Result; + fn to_string(self) -> String; +} + +pub trait NetworkContext { + type Address: Address; + + fn run_ping_eventloop(ctx: impl ServersContext); +} + +pub trait ServersContext { + fn start_measuring(&mut self) -> (u64, impl Iterator); + + 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; +} diff --git a/network/windows/Cargo.toml b/network/windows/Cargo.toml new file mode 100644 index 0000000..8cc0e5d --- /dev/null +++ b/network/windows/Cargo.toml @@ -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" +] \ No newline at end of file diff --git a/src/io/windows/address.rs b/network/windows/src/address.rs similarity index 97% rename from src/io/windows/address.rs rename to network/windows/src/address.rs index e591b0f..074bd41 100644 --- a/src/io/windows/address.rs +++ b/network/windows/src/address.rs @@ -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; diff --git a/src/io/allocator.rs b/network/windows/src/allocator.rs similarity index 100% rename from src/io/allocator.rs rename to network/windows/src/allocator.rs diff --git a/src/io/windows/errors.rs b/network/windows/src/errors.rs similarity index 100% rename from src/io/windows/errors.rs rename to network/windows/src/errors.rs diff --git a/src/io/windows/eventloop.rs b/network/windows/src/eventloop.rs similarity index 84% rename from src/io/windows/eventloop.rs rename to network/windows/src/eventloop.rs index 61054db..975a0bc 100644 --- a/src/io/windows/eventloop.rs +++ b/network/windows/src/eventloop.rs @@ -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: *mut Ctx) { +pub unsafe fn run_eventloop>(ctx: *mut Ctx) { let mut heap = Allocator::>>::new(); let mut socket = WindowsOverlappingIcmpSocket::new(); @@ -47,7 +47,7 @@ pub unsafe fn run_eventloop(ctx: *mut Ctx) { } } -struct CallbackData { +struct CallbackData> { ctx: NonNull, allocator: NonNull>>, address: WindowsAddressKnown, @@ -55,7 +55,7 @@ struct CallbackData { __addrs_size: i32, } -impl CallbackData { +impl> CallbackData { fn init_send( task: &mut Pin<&mut IoTask>>, ctx: NonNull, @@ -85,7 +85,7 @@ impl CallbackData { } } -unsafe extern "system" fn sending_done( +unsafe extern "system" fn sending_done>( dwerror: u32, cbtransferred: u32, lpoverlapped: *mut OVERLAPPED, @@ -100,7 +100,7 @@ unsafe extern "system" fn sending_done( task.get_extra().allocator.as_mut().free(task); } -unsafe extern "system" fn receiving_done( +unsafe extern "system" fn receiving_done>( dwerror: u32, cbtransferred: u32, lpoverlapped: *mut OVERLAPPED, @@ -138,7 +138,7 @@ impl WindowsOverlappingIcmpSocket { } } - unsafe fn send( + unsafe fn send>( &mut self, task: &mut Pin<&mut IoTask>>, cb: LPWSAOVERLAPPED_COMPLETION_ROUTINE, @@ -189,7 +189,7 @@ impl WindowsOverlappingIcmpSocket { } } - unsafe fn receive( + unsafe fn receive>( &mut self, task: &mut Pin<&mut IoTask>>, cb: LPWSAOVERLAPPED_COMPLETION_ROUTINE, diff --git a/network/windows/src/lib.rs b/network/windows/src/lib.rs new file mode 100644 index 0000000..85195a2 --- /dev/null +++ b/network/windows/src/lib.rs @@ -0,0 +1,6 @@ +mod address; +mod errors; +mod task; +mod eventloop; +mod allocator; +mod winsocks_scope; \ No newline at end of file diff --git a/src/io/windows/task.rs b/network/windows/src/task.rs similarity index 100% rename from src/io/windows/task.rs rename to network/windows/src/task.rs diff --git a/src/io/windows/winsocks_scope.rs b/network/windows/src/winsocks_scope.rs similarity index 86% rename from src/io/windows/winsocks_scope.rs rename to network/windows/src/winsocks_scope.rs index f2d2017..7dc284a 100644 --- a/src/io/windows/winsocks_scope.rs +++ b/network/windows/src/winsocks_scope.rs @@ -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(scope: impl FnOnce() -> R) -> R { +pub(in crate) fn winsocks_scope(scope: impl FnOnce() -> R) -> R { unsafe { match WSAStartup(0x0202, &mut uninitialized()) { 0 => {} diff --git a/src/data/mod.rs b/src/data/mod.rs deleted file mode 100644 index ee250e4..0000000 --- a/src/data/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod cycle_buffer; \ No newline at end of file diff --git a/src/io/abstract/address.rs b/src/io/abstract/address.rs deleted file mode 100644 index 56076a1..0000000 --- a/src/io/abstract/address.rs +++ /dev/null @@ -1,7 +0,0 @@ -use std::hash::Hash; - -pub trait Address: Sized + PartialEq + Hash { - fn parse(raw: &str) -> Result; - - fn to_string(self) -> String; -} diff --git a/src/io/abstract/context.rs b/src/io/abstract/context.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/io/abstract/mod.rs b/src/io/abstract/mod.rs deleted file mode 100644 index c5f2726..0000000 --- a/src/io/abstract/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -mod address; -mod context; - -pub use address::Address; \ No newline at end of file diff --git a/src/io/mod.rs b/src/io/mod.rs deleted file mode 100644 index 7cd3c75..0000000 --- a/src/io/mod.rs +++ /dev/null @@ -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(scope: impl FnOnce() -> R) -> R { - return winsocks_scope(scope); -} \ No newline at end of file diff --git a/src/io/servers_context.rs b/src/io/servers_context.rs deleted file mode 100644 index b533023..0000000 --- a/src/io/servers_context.rs +++ /dev/null @@ -1,9 +0,0 @@ -use super::Address; - -pub trait ServersContext { - fn start_measuring(&mut self) -> (u64, impl Iterator); - - fn report_ping(&mut self, addr: Address, id: u64, ping_ms: u64); - - fn report_error(&mut self, addr: Address, id: u64); -} diff --git a/src/io/windows/mod.rs b/src/io/windows/mod.rs deleted file mode 100644 index 2ccf570..0000000 --- a/src/io/windows/mod.rs +++ /dev/null @@ -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; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 7472c61..0000000 --- a/src/main.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::io::network_scope; - -mod data; -mod io; - -fn main() { - network_scope(|| { - println!("Hello, world!"); - }); -}