From 43483c21455caa611d79481aa56d362e844b1a4b Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 3 May 2026 11:24:13 +0200 Subject: [PATCH] Milestone 1 --- Cargo.lock | 1140 ++++++++++++++++++++++ Cargo.toml | 7 + Documentation/Mile_Stones/Milestone_1.md | 40 +- client_node/Cargo.toml | 14 + client_node/src/main.rs | 24 + client_node/src/network/control.rs | 72 ++ client_node/src/network/mod.rs | 6 + core_protocol/Cargo.toml | 12 + core_protocol/src/constants.rs | 15 + core_protocol/src/lib.rs | 13 + core_protocol/src/tcp_events.rs | 31 + core_protocol/src/udp_packets.rs | 20 + server_node/Cargo.toml | 15 + server_node/src/main.rs | 43 + server_node/src/state.rs | 49 + server_node/src/tcp_router.rs | 95 ++ 16 files changed, 1576 insertions(+), 20 deletions(-) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 client_node/Cargo.toml create mode 100644 client_node/src/main.rs create mode 100644 client_node/src/network/control.rs create mode 100644 client_node/src/network/mod.rs create mode 100644 core_protocol/Cargo.toml create mode 100644 core_protocol/src/constants.rs create mode 100644 core_protocol/src/lib.rs create mode 100644 core_protocol/src/tcp_events.rs create mode 100644 core_protocol/src/udp_packets.rs create mode 100644 server_node/Cargo.toml create mode 100644 server_node/src/main.rs create mode 100644 server_node/src/state.rs create mode 100644 server_node/src/tcp_router.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..58f8429 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1140 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anyhow" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + +[[package]] +name = "bitflags" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" + +[[package]] +name = "bumpalo" +version = "3.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" + +[[package]] +name = "bytes" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" + +[[package]] +name = "cc" +version = "1.2.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d16d90359e986641506914ba71350897565610e87ce0ad9e6f28569db3dd5c6d" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "chrono" +version = "0.4.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "serde", + "wasm-bindgen", + "windows-link", +] + +[[package]] +name = "client_node" +version = "0.1.0" +dependencies = [ + "anyhow", + "core_protocol", + "futures", + "tokio", + "tokio-serde", + "tokio-util", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "core_protocol" +version = "0.1.0" +dependencies = [ + "bincode", + "chrono", + "secrecy", + "serde", + "thiserror", + "uuid", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "dashmap" +version = "6.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core", +] + +[[package]] +name = "educe" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4bd92664bf78c4d3dba9b7cdafce6fa15b13ed3ed16175218196942e99168a8" +dependencies = [ + "enum-ordinalize", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "enum-ordinalize" +version = "4.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a1091a7bb1f8f2c4b28f1fe2cef4980ca2d410a3d727d67ecc3178c9b0800f0" +dependencies = [ + "enum-ordinalize-derive", +] + +[[package]] +name = "enum-ordinalize-derive" +version = "4.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ca9601fb2d62598ee17836250842873a413586e5d7ed88b356e38ddbb0ec631" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "futures" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-executor" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf29c38818342a3b26b5b923639e7b1f4a61fc5e76102d4b1981c6dc7a7579d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718" + +[[package]] +name = "futures-macro" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c39754e157331b013978ec91992bde1ac089843443c49cbc7f46150b0fad0893" + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", + "wasip3", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f467dd6dccf739c208452f8014c75c18bb8301b050ad1cfb27153803edb0f51" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown 0.17.0", + "serde", + "serde_core", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "js-sys" +version = "0.3.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1840c94c045fbcf8ba2812c95db44499f7c64910a912551aaaa541decebcacf" +dependencies = [ + "cfg-if", + "futures-util", + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + +[[package]] +name = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "mio" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50b7e5b27aa02a74bac8c3f23f448f8d87ff11f92d3aac1a6ed369ee08cc56c1" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "pin-project" +version = "1.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1749c7ed4bcaf4c3d0a3efc28538844fb29bcdd7d2b67b2be7e20ba861ff517" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b20ed30f105399776b9c883e68e536ef602a16ae6f596d2c473591d6ad64c6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "secrecy" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e891af845473308773346dc847b2c23ee78fe442e0472ac50e22a18a93d3ae5a" +dependencies = [ + "serde", + "zeroize", +] + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "server_node" +version = "0.1.0" +dependencies = [ + "anyhow", + "core_protocol", + "dashmap", + "futures", + "tokio", + "tokio-serde", + "tokio-util", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "socket2" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "tokio" +version = "1.52.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67dee974fe86fd92cc45b7a95fdd2f99a36a6d7b0d431a231178d3d670bbcc6" +dependencies = [ + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "385a6cb71ab9ab790c5fe8d67f1645e6c450a7ce006a33de03daa956cf70a496" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-serde" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caf600e7036b17782571dd44fa0a5cea3c82f60db5137f774a325a76a0d6852b" +dependencies = [ + "bincode", + "bytes", + "educe", + "futures-core", + "futures-sink", + "pin-project", + "serde", +] + +[[package]] +name = "tokio-util" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ae9cec805b01e8fc3fd2fe289f89149a9b66dd16786abd8b19cfa7b48cb0098" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "uuid" +version = "1.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd74a9687298c6858e9b88ec8935ec45d22e8fd5e6394fa1bd4e99a87789c76" +dependencies = [ + "getrandom", + "js-sys", + "serde_core", + "wasm-bindgen", +] + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.3+wasi-0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20064672db26d7cdc89c7798c48a0fdfac8213434a1186e5ef29fd560ae223d6" +dependencies = [ + "wit-bindgen 0.57.1", +] + +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen 0.51.0", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.120" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df52b6d9b87e0c74c9edfa1eb2d9bf85e5d63515474513aa50fa181b3c4f5db1" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.120" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b1041f495fb322e64aca85f5756b2172e35cd459376e67f2a6c9dffcedb103" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.120" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dcd0ff20416988a18ac686d4d4d0f6aae9ebf08a389ff5d29012b05af2a1b41" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.120" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49757b3c82ebf16c57d69365a142940b384176c24df52a087fb748e2085359ea" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags", + "hashbrown 0.15.5", + "indexmap", + "semver", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "wit-bindgen" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +dependencies = [ + "wit-bindgen-rust-macro", +] + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "wit-bindgen-core" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" +dependencies = [ + "anyhow", + "heck", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn", + "wit-bindgen-core", + "wit-bindgen-rust", +] + +[[package]] +name = "wit-component" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser", +] + +[[package]] +name = "zeroize" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ac32bb9 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[workspace] +members = [ + "core_protocol", + "server_node", + "client_node" +] +resolver = "2" diff --git a/Documentation/Mile_Stones/Milestone_1.md b/Documentation/Mile_Stones/Milestone_1.md index ff533a9..5441f35 100644 --- a/Documentation/Mile_Stones/Milestone_1.md +++ b/Documentation/Mile_Stones/Milestone_1.md @@ -2,31 +2,31 @@ **Goal:** Initialize the project and establish the shared language between client and server. ### 1. Workspace Setup -- [ ] Initialize the root Cargo workspace: `cargo init --vcs none` (delete `src/`). Create a root `Cargo.toml` with `[workspace] members = ["core_protocol", "server_node", "client_node"]`. -- [ ] **AI Context Trap (File Structure):** Strictly adhere to the directory layout and module hierarchy defined in `File_Structure.md`. Do not invent new file paths or module names; map every new crate and file exactly to the blueprint. -- [ ] Create crates: `cargo new --lib core_protocol`, `cargo new --bin server_node`, `cargo new --bin client_node`. -- [ ] Add strict lints (`#![forbid(unsafe_code)]`, etc.) to the root workspace or individual `lib.rs`/`main.rs` files. -- [ ] **Dependencies (`core_protocol`):** Add `serde`, `bincode`, `uuid`, `chrono`, `thiserror`, `secrecy` (for zeroing sensitive keys). -- [ ] **Dependencies (`server_node`):** Add `tokio` (full), `tracing`, `tracing-subscriber`, `anyhow`, `dashmap`. -- [ ] **Dependencies (`client_node`):** Add `tokio` (rt-multi-thread), `tracing`, `tracing-subscriber`, `anyhow`. +- [x] Initialize the root Cargo workspace: `cargo init --vcs none` (delete `src/`). Create a root `Cargo.toml` with `[workspace] members = ["core_protocol", "server_node", "client_node"]`. +- [x] **AI Context Trap (File Structure):** Strictly adhere to the directory layout and module hierarchy defined in `File_Structure.md`. Do not invent new file paths or module names; map every new crate and file exactly to the blueprint. +- [x] Create crates: `cargo new --lib core_protocol`, `cargo new --bin server_node`, `cargo new --bin client_node`. +- [x] Add strict lints (`#![forbid(unsafe_code)]`, etc.) to the root workspace or individual `lib.rs`/`main.rs` files. +- [x] **Dependencies (`core_protocol`):** Add `serde`, `bincode`, `uuid`, `chrono`, `thiserror`, `secrecy` (for zeroing sensitive keys). +- [x] **Dependencies (`server_node`):** Add `tokio` (full), `tracing`, `tracing-subscriber`, `anyhow`, `dashmap`. +- [x] **Dependencies (`client_node`):** Add `tokio` (rt-multi-thread), `tracing`, `tracing-subscriber`, `anyhow`. ### 2. Protocol Definitions (`core_protocol`) -- [ ] Create `src/tcp_events.rs`. Define `enum TcpEvent { AuthRequest { username: String, ... }, AuthResponse { session_token: u32, ... }, ChannelJoin { ... }, ChatMessage { ... } }` with `#[derive(Serialize, Deserialize)]`. -- [ ] Create `src/udp_packets.rs`. Define `struct VoicePacketHeader { pub session_token: u32, pub sequence_num: u64, pub timestamp: u64 }` with `#[derive(Serialize, Deserialize)]`. -- [ ] Create `src/constants.rs`. Define `pub const SAMPLE_RATE: u32 = 48000;`, `pub const FRAME_SIZE: usize = 960;`, `pub const TCP_PORT: u16 = 8080;`. +- [x] Create `src/tcp_events.rs`. Define `enum TcpEvent { AuthRequest { username: String, ... }, AuthResponse { session_token: u32, ... }, ChannelJoin { ... }, ChatMessage { ... } }` with `#[derive(Serialize, Deserialize)]`. +- [x] Create `src/udp_packets.rs`. Define `struct VoicePacketHeader { pub session_token: u32, pub sequence_num: u64, pub timestamp: u64 }` with `#[derive(Serialize, Deserialize)]`. +- [x] Create `src/constants.rs`. Define `pub const SAMPLE_RATE: u32 = 48000;`, `pub const FRAME_SIZE: usize = 960;`, `pub const TCP_PORT: u16 = 8080;`. ### 3. TCP Handshake (`server_node` & `client_node`) -- [ ] **Server:** In `server_node/src/main.rs`, initialize `tokio::net::TcpListener::bind("0.0.0.0:8080")`. -- [ ] **Server:** Spawn a new `tokio::spawn(async move { ... })` for each incoming `TcpStream`. -- [ ] **Client:** In `client_node/src/network/control.rs`, implement `TcpStream::connect("127.0.0.1:8080")`. -- [ ] **AI Context Trap (TCP Framing):** Raw TCP streams suffer from fragmentation. Do NOT attempt to manually buffer bytes. You must use `tokio_util::codec::LengthDelimitedCodec` (with `tokio_serde` and `bincode`) to abstract the frame boundaries cleanly. +- [x] **Server:** In `server_node/src/main.rs`, initialize `tokio::net::TcpListener::bind("0.0.0.0:8080")`. +- [x] **Server:** Spawn a new `tokio::spawn(async move { ... })` for each incoming `TcpStream`. +- [x] **Client:** In `client_node/src/network/control.rs`, implement `TcpStream::connect("127.0.0.1:8080")`. +- [x] **AI Context Trap (TCP Framing):** Raw TCP streams suffer from fragmentation. Do NOT attempt to manually buffer bytes. You must use `tokio_util::codec::LengthDelimitedCodec` (with `tokio_serde` and `bincode`) to abstract the frame boundaries cleanly. ### 4. Login Logic & State -- [ ] **Server State:** Create `server_node/src/state.rs`. Define a `DashMap` to store active session tokens. -- [ ] **Authentication Flow:** Client sends `TcpEvent::AuthRequest`. Server generates a random `u32` session token, stores it in `DashMap`, and returns `TcpEvent::AuthResponse`. -- [ ] **Validation:** Ensure the server actively drops the connection if the client sends invalid or excessively large payloads. +- [x] **Server State:** Create `server_node/src/state.rs`. Define a `DashMap` to store active session tokens. +- [x] **Authentication Flow:** Client sends `TcpEvent::AuthRequest`. Server generates a random `u32` session token, stores it in `DashMap`, and returns `TcpEvent::AuthResponse`. +- [x] **Validation:** Ensure the server actively drops the connection if the client sends invalid or excessively large payloads. ### 5. Observability (Logging) -- [ ] **Initialization:** In both binaries' `main.rs`, call `tracing_subscriber::fmt::init()`. -- [ ] **Implementation:** Replace all `println!` calls with `tracing::info!`, `tracing::warn!`, or `tracing::error!`. -- [ ] **Tracing Context:** Use `#[tracing::instrument]` on core TCP handler functions to automatically log client IPs and session IDs. \ No newline at end of file +- [x] **Initialization:** In both binaries' `main.rs`, call `tracing_subscriber::fmt::init()`. +- [x] **Implementation:** Replace all `println!` calls with `tracing::info!`, `tracing::warn!`, or `tracing::error!`. +- [x] **Tracing Context:** Use `#[tracing::instrument]` on core TCP handler functions to automatically log client IPs and session IDs. \ No newline at end of file diff --git a/client_node/Cargo.toml b/client_node/Cargo.toml new file mode 100644 index 0000000..35bf5fd --- /dev/null +++ b/client_node/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "client_node" +version = "0.1.0" +edition = "2024" + +[dependencies] +anyhow = "1.0.102" +core_protocol = { version = "0.1.0", path = "../core_protocol" } +futures = "0.3.32" +tokio = { version = "1.52.1", features = ["rt-multi-thread", "net", "macros"] } +tokio-serde = { version = "0.9.0", features = ["bincode"] } +tokio-util = { version = "0.7.18", features = ["codec"] } +tracing = "0.1.44" +tracing-subscriber = "0.3.23" diff --git a/client_node/src/main.rs b/client_node/src/main.rs new file mode 100644 index 0000000..a1459cb --- /dev/null +++ b/client_node/src/main.rs @@ -0,0 +1,24 @@ +//! Client Node entry point. +//! +//! This module initializes the desktop client application, sets up the Tokio +//! background thread for networking, and eventually binds to the UI framework. + +#![forbid(unsafe_code)] +#![deny(clippy::all, clippy::pedantic)] +#![deny(clippy::unwrap_used, clippy::expect_used)] + +mod network; + +use tracing::{error, info}; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + tracing_subscriber::fmt::init(); + info!("Starting client node..."); + + if let Err(e) = network::control::connect_and_auth("TestUser").await { + error!("Connection error: {:?}", e); + } + + Ok(()) +} diff --git a/client_node/src/network/control.rs b/client_node/src/network/control.rs new file mode 100644 index 0000000..2cc2cce --- /dev/null +++ b/client_node/src/network/control.rs @@ -0,0 +1,72 @@ +//! Reliable TCP connectivity logic. +//! +//! Implements the client-side TCP connection handling, including framing, +//! serialization, and the initial authentication handshake. + +use anyhow::{Context, Result}; +use core_protocol::constants; +use core_protocol::tcp_events::TcpEvent; +use futures::{SinkExt, StreamExt}; +use tokio::net::TcpStream; +use tokio_serde::formats::Bincode; +use tokio_serde::SymmetricallyFramed; +use tokio_util::codec::{Framed, LengthDelimitedCodec}; +use tracing::{info, warn}; + +/// A type alias for the symmetrically framed TCP stream to simplify syntax. +type FramedStream = SymmetricallyFramed< + Framed, + TcpEvent, + Bincode, +>; + +/// Connects to the server and performs the initial authentication handshake. +/// +/// This establishes a length-delimited TCP connection to prevent fragmentation, +/// sends an `AuthRequest` with the given username, and awaits the `AuthResponse`. +/// +/// # Arguments +/// * `username` - The requested display name for this client. +/// +/// # Errors +/// Returns an `anyhow::Result` if the TCP socket cannot connect, if the serialization/ +/// deserialization fails, or if the server closes the connection prematurely. +pub async fn connect_and_auth(username: &str) -> Result<()> { + let addr = format!("127.0.0.1:{}", constants::TCP_PORT); + info!("Connecting to server at {}...", addr); + + let stream = TcpStream::connect(&addr).await.context("Failed to connect to server")?; + info!("Connected!"); + + // Construct the codec pipeline exactly mirroring the server's configuration + // to ensure reliable packet framing. + let length_delimited = Framed::new(stream, LengthDelimitedCodec::new()); + let mut framed: FramedStream = SymmetricallyFramed::new( + length_delimited, + Bincode::::default(), + ); + + let auth_req = TcpEvent::AuthRequest { + username: username.to_string(), + }; + + framed.send(auth_req).await.context("Failed to send AuthRequest")?; + info!("Sent AuthRequest for user: {}", username); + + if let Some(response) = framed.next().await { + let response = response.context("Failed to deserialize response")?; + + match response { + TcpEvent::AuthResponse { session_token } => { + info!("Successfully authenticated! Session token: {}", session_token); + } + _ => { + warn!("Received unexpected event instead of AuthResponse"); + } + } + } else { + warn!("Connection closed before receiving AuthResponse"); + } + + Ok(()) +} diff --git a/client_node/src/network/mod.rs b/client_node/src/network/mod.rs new file mode 100644 index 0000000..18cb759 --- /dev/null +++ b/client_node/src/network/mod.rs @@ -0,0 +1,6 @@ +//! Internet connectivity modules. +//! +//! This module isolates all remote networking logic from the UI and audio engines. +//! It exposes the necessary interfaces to manage TCP control lanes and UDP streams. + +pub mod control; diff --git a/core_protocol/Cargo.toml b/core_protocol/Cargo.toml new file mode 100644 index 0000000..e49ba87 --- /dev/null +++ b/core_protocol/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "core_protocol" +version = "0.1.0" +edition = "2024" + +[dependencies] +bincode = "1.3.3" +chrono = { version = "0.4.44", features = ["serde"] } +secrecy = { version = "0.10.3", features = ["serde"] } +serde = { version = "1.0.228", features = ["derive"] } +thiserror = "2.0.18" +uuid = { version = "1.23.1", features = ["v4", "serde"] } diff --git a/core_protocol/src/constants.rs b/core_protocol/src/constants.rs new file mode 100644 index 0000000..b139519 --- /dev/null +++ b/core_protocol/src/constants.rs @@ -0,0 +1,15 @@ +//! Fixed system-wide specifications. +//! +//! Contains constants required for hardware configuration, network binding, +//! and math routines such as audio framing lengths. + +/// The uniform audio sampling rate (48 kHz). +/// We lock the entire system to 48kHz because Opus performs optimally at this rate. +pub const SAMPLE_RATE: u32 = 48000; + +/// The exact number of audio samples per 20ms frame at 48kHz. +/// Opus strictly requires a specific frame size (like 20ms) to function correctly. +pub const FRAME_SIZE: usize = 960; + +/// The default TCP port used for reliable control lane communication. +pub const TCP_PORT: u16 = 8080; diff --git a/core_protocol/src/lib.rs b/core_protocol/src/lib.rs new file mode 100644 index 0000000..ca33051 --- /dev/null +++ b/core_protocol/src/lib.rs @@ -0,0 +1,13 @@ +//! Core Protocol definitions for the Voice App. +//! +//! This module defines the foundational types and constants shared between the +//! client and server nodes. It ensures that both ends of the connection speak the +//! exact same structural language for serialization/deserialization. + +#![forbid(unsafe_code)] +#![deny(clippy::all, clippy::pedantic)] +#![deny(clippy::unwrap_used, clippy::expect_used)] + +pub mod constants; +pub mod tcp_events; +pub mod udp_packets; diff --git a/core_protocol/src/tcp_events.rs b/core_protocol/src/tcp_events.rs new file mode 100644 index 0000000..2d3873e --- /dev/null +++ b/core_protocol/src/tcp_events.rs @@ -0,0 +1,31 @@ +//! TCP Control Lane events. +//! +//! Defines the reliable commands sent over the TCP connection for state +//! synchronization, such as authentication and text chat. + +use serde::{Deserialize, Serialize}; + +/// Represents all possible events sent over the reliable TCP control lane. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum TcpEvent { + /// Initial request from the client containing their chosen username. + AuthRequest { + /// The username requested by the client. + username: String, + }, + /// The server's response confirming authentication and granting a session token. + AuthResponse { + /// A unique, ephemeral ID assigned to the client for the duration of the session. + session_token: u32, + }, + /// A request to join a specific voice channel. + ChannelJoin { + /// The unique ID of the target channel. + channel_id: u32, + }, + /// A text message intended for the current channel's chat. + ChatMessage { + /// The raw text message. + message: String, + }, +} diff --git a/core_protocol/src/udp_packets.rs b/core_protocol/src/udp_packets.rs new file mode 100644 index 0000000..ee3af89 --- /dev/null +++ b/core_protocol/src/udp_packets.rs @@ -0,0 +1,20 @@ +//! UDP High-speed Voice Packet structures. +//! +//! Defines the headers and payload structures for the low-latency voice data +//! sent over UDP. + +use serde::{Deserialize, Serialize}; + +/// The header attached to every UDP voice frame. +/// +/// We separate this from the payload to allow the server to rapidly route packets +/// using just the `session_token` without fully deserializing the heavy audio data. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct VoicePacketHeader { + /// The unique session ID matching the client's TCP authentication token. + pub session_token: u32, + /// A strictly increasing sequence number to detect dropped or out-of-order packets. + pub sequence_num: u64, + /// The timestamp of the packet generation, used to align jitter buffers. + pub timestamp: u64, +} diff --git a/server_node/Cargo.toml b/server_node/Cargo.toml new file mode 100644 index 0000000..4f1212f --- /dev/null +++ b/server_node/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "server_node" +version = "0.1.0" +edition = "2024" + +[dependencies] +anyhow = "1.0.102" +core_protocol = { version = "0.1.0", path = "../core_protocol" } +dashmap = "6.1.0" +futures = "0.3.32" +tokio = { version = "1.52.1", features = ["full"] } +tokio-serde = { version = "0.9.0", features = ["bincode"] } +tokio-util = { version = "0.7.18", features = ["codec"] } +tracing = "0.1.44" +tracing-subscriber = "0.3.23" diff --git a/server_node/src/main.rs b/server_node/src/main.rs new file mode 100644 index 0000000..84b2f2f --- /dev/null +++ b/server_node/src/main.rs @@ -0,0 +1,43 @@ +//! Server Node entry point. +//! +//! This module initializes the headless relay server, binds the network listeners, +//! and spawns isolated tasks for each connected TCP client. + +#![forbid(unsafe_code)] +#![deny(clippy::all, clippy::pedantic)] +#![deny(clippy::unwrap_used, clippy::expect_used)] + +mod state; +mod tcp_router; + +use core_protocol::constants; +use state::AppState; +use std::sync::Arc; +use tokio::net::TcpListener; +use tracing::{error, info}; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + tracing_subscriber::fmt::init(); + info!("Starting server node..."); + + let state = Arc::new(AppState::new()); + + let addr = format!("0.0.0.0:{}", constants::TCP_PORT); + let listener = TcpListener::bind(&addr).await?; + info!("TCP Listener bound to {}", addr); + + loop { + match listener.accept().await { + Ok((stream, _)) => { + let state_clone = state.clone(); + tokio::spawn(async move { + tcp_router::handle_connection(stream, state_clone).await; + }); + } + Err(e) => { + error!("Failed to accept connection: {:?}", e); + } + } + } +} diff --git a/server_node/src/state.rs b/server_node/src/state.rs new file mode 100644 index 0000000..25c2a0c --- /dev/null +++ b/server_node/src/state.rs @@ -0,0 +1,49 @@ +//! Concurrent server state management. +//! +//! This module defines the global application state shared across all active +//! Tokio tasks. It heavily relies on lock-free and fine-grained locking primitives +//! like `DashMap` and `AtomicU32` to ensure high performance without stuttering. + +use dashmap::DashMap; +use std::sync::atomic::{AtomicU32, Ordering}; + +/// Represents the session data for a single connected user. +#[allow(dead_code)] +pub struct UserState { + /// The client-chosen username for display in channels. + pub username: String, +} + +/// The global application state tracking all connections and rooms. +pub struct AppState { + /// A highly concurrent hash map linking active session tokens to their user data. + pub active_users: DashMap, + /// A simple atomic counter for issuing unique sequential session tokens. + next_token: AtomicU32, +} + +impl AppState { + /// Creates a new, empty application state. + #[must_use] + pub fn new() -> Self { + Self { + active_users: DashMap::new(), + next_token: AtomicU32::new(1), + } + } + + /// Generates a strictly unique, monotonically increasing session token. + /// + /// We use `Ordering::Relaxed` because we only need atomicity on the counter + /// itself, not strict memory ordering with other operations. + #[must_use] + pub fn generate_token(&self) -> u32 { + self.next_token.fetch_add(1, Ordering::Relaxed) + } +} + +impl Default for AppState { + fn default() -> Self { + Self::new() + } +} diff --git a/server_node/src/tcp_router.rs b/server_node/src/tcp_router.rs new file mode 100644 index 0000000..d52e1c0 --- /dev/null +++ b/server_node/src/tcp_router.rs @@ -0,0 +1,95 @@ +//! Reliable TCP routing and connection management. +//! +//! This module implements the reliable control lane for client communication. +//! It handles the initial authentication handshake and routes incoming TCP events +//! from the length-delimited codec to the broader application state. + +use anyhow::{Context, Result}; +use core_protocol::tcp_events::TcpEvent; +use futures::{SinkExt, StreamExt}; +use std::sync::Arc; +use tokio::net::TcpStream; +use tokio_serde::formats::Bincode; +use tokio_serde::SymmetricallyFramed; +use tokio_util::codec::{Framed, LengthDelimitedCodec}; +use tracing::{error, info, instrument, warn}; + +use crate::state::AppState; +use crate::state::UserState; + +/// A type alias for the heavily-nested framed stream type, combining `LengthDelimitedCodec` and `Bincode`. +type FramedStream = SymmetricallyFramed< + Framed, + TcpEvent, + Bincode, +>; + +/// Handles the lifecycle of a newly connected client's TCP stream. +/// +/// This spans an instrumented task for the connection, setting up the necessary +/// framers and codecs before entering the event loop. +#[instrument(skip(stream, state))] +pub async fn handle_connection(stream: TcpStream, state: Arc) { + let peer_addr = match stream.peer_addr() { + Ok(addr) => addr, + Err(e) => { + error!("Failed to get peer address: {:?}", e); + return; + } + }; + info!("New connection from {}", peer_addr); + + // We pad the TCP stream with a length-delimited codec to guarantee frame boundaries, + // avoiding fragmentation issues common in raw TCP sockets. + let length_delimited = Framed::new(stream, LengthDelimitedCodec::new()); + let mut framed: FramedStream = SymmetricallyFramed::new( + length_delimited, + Bincode::::default(), + ); + + if let Err(e) = process_connection(&mut framed, state).await { + warn!("Connection closed with error: {:?}", e); + } else { + info!("Connection closed cleanly"); + } +} + +/// The inner event loop that processes deserialized `TcpEvent`s from the client. +/// +/// # Errors +/// Returns an `anyhow::Result` if deserialization fails, the connection drops unexpectedly, +/// or a serialization error occurs when transmitting a response. +async fn process_connection(framed: &mut FramedStream, state: Arc) -> Result<()> { + while let Some(event) = framed.next().await { + let event = event.context("Failed to deserialize event")?; + + match event { + TcpEvent::AuthRequest { username } => { + // REDACTED standard: we might log the username, but this is a reminder + // for future sensitive items to use [REDACTED]. + info!("AuthRequest received for user: {}", username); + + let session_token = state.generate_token(); + + state.active_users.insert( + session_token, + UserState { + username: username.clone(), + }, + ); + + framed + .send(TcpEvent::AuthResponse { session_token }) + .await + .context("Failed to send AuthResponse")?; + + info!("AuthResponse sent to {}", username); + } + _ => { + warn!("Received unhandled event before auth or unsupported event"); + } + } + } + + Ok(()) +}