From fe3e140dbb8838986b24966bf1ffdcc1bd0eb2e0 Mon Sep 17 00:00:00 2001 From: ReCore-sys Date: Wed, 26 Jun 2024 21:50:18 +0930 Subject: [PATCH 1/4] Pain --- Cargo.lock | 649 +++++++++++++++- Cargo.toml | 2 + src/crates/ferrumc_macros/src/lib.rs | 9 +- src/crates/ferrumc_net/Cargo.lock | 691 +++++++++++++++++- src/crates/ferrumc_net/Cargo.toml | 4 +- src/crates/ferrumc_net/src/lib.rs | 72 +- .../src/packets/incoming/handshake.rs | 15 +- .../ferrumc_net/src/packets/incoming/ping.rs | 6 +- .../src/packets/incoming/status.rs | 9 +- src/crates/ferrumc_net/src/packets/mod.rs | 9 +- src/main.rs | 1 + 11 files changed, 1401 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d8b7fe9d..e2cde756 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,6 +81,28 @@ version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +[[package]] +name = "async-stream" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd56dd203fef61ac097dd65721a419ddccb106b2d2b70ba60a6b529f03961a51" +dependencies = [ + "async-stream-impl", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-stream-impl" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "async-trait" version = "0.1.77" @@ -107,6 +129,51 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "axum" +version = "0.6.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" +dependencies = [ + "async-trait", + "axum-core", + "bitflags 1.3.2", + "bytes", + "futures-util", + "http", + "http-body", + "hyper", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "sync_wrapper", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "759fa577a247914fd3f7f76d62972792636412fbfd634cd452f6a385a74d2d2c" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "mime", + "rustversion", + "tower-layer", + "tower-service", +] + [[package]] name = "backtrace" version = "0.3.69" @@ -217,6 +284,44 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "console-api" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a257c22cd7e487dd4a13d413beabc512c5052f0bc048db0da6a84c3d8a6142fd" +dependencies = [ + "futures-core", + "prost", + "prost-types", + "tonic", + "tracing-core", +] + +[[package]] +name = "console-subscriber" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31c4cc54bae66f7d9188996404abdf7fdfa23034ef8e43478c8810828abad758" +dependencies = [ + "console-api", + "crossbeam-channel", + "crossbeam-utils", + "futures-task", + "hdrhistogram", + "humantime", + "prost", + "prost-types", + "serde", + "serde_json", + "thread_local", + "tokio", + "tokio-stream", + "tonic", + "tracing", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "const-random" version = "0.1.18" @@ -255,6 +360,30 @@ dependencies = [ "libc", ] +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + [[package]] name = "crunchy" version = "0.2.2" @@ -273,11 +402,12 @@ dependencies = [ [[package]] name = "dashmap" -version = "5.5.3" +version = "6.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" +checksum = "804c8821570c3f8b70230c2ba75ffa5c0f9a4189b9a432b6656c536712acae28" dependencies = [ "cfg-if", + "crossbeam-utils", "hashbrown 0.14.3", "lock_api", "once_cell", @@ -303,6 +433,12 @@ dependencies = [ "const-random", ] +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "env_filter" version = "0.1.0" @@ -339,12 +475,14 @@ dependencies = [ "anyhow", "byteorder", "config", + "console-subscriber", "env_logger", "ferrumc_macros", "ferrumc_net", "ferrumc_utils", "lazy_static", "log", + "parking_lot", "serde", "thiserror", "tokio", @@ -368,12 +506,14 @@ dependencies = [ "atomic", "base64 0.22.1", "byteorder", + "console-subscriber", "dashmap", "ferrumc_macros", "ferrumc_utils", "lariv", "lazy_static", "log", + "parking_lot", "rand", "serde", "serde_json", @@ -396,6 +536,61 @@ dependencies = [ "toml", ] +[[package]] +name = "flate2" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f54427cfd1c7829e2a139fcefea601bf088ebca651d2bf53ebc600eac295dae" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -423,6 +618,31 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "h2" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap 2.1.0", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hashbrown" version = "0.13.2" @@ -435,18 +655,111 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "hdrhistogram" +version = "7.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "765c9198f173dd59ce26ff9f95ef0aafd0a0fe01fb9d72841bc5066a4c06511d" +dependencies = [ + "base64 0.21.7", + "byteorder", + "flate2", + "nom", + "num-traits", +] + [[package]] name = "hermit-abi" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + [[package]] name = "humantime" version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "hyper" +version = "0.14.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f361cde2f109281a220d4307746cdfd5ee3f410da58a70377762396775634b33" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper-timeout" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1" +dependencies = [ + "hyper", + "pin-project-lite", + "tokio", + "tokio-io-timeout", +] + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", +] + [[package]] name = "indexmap" version = "2.1.0" @@ -463,6 +776,15 @@ version = "1.70.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.10" @@ -520,12 +842,33 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + +[[package]] +name = "matchit" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" + [[package]] name = "memchr" version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -562,6 +905,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -599,9 +951,9 @@ dependencies = [ [[package]] name = "parking_lot" -version = "0.12.1" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" dependencies = [ "lock_api", "parking_lot_core", @@ -626,6 +978,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + [[package]] name = "pest" version = "2.7.5" @@ -671,12 +1029,38 @@ dependencies = [ "sha2", ] +[[package]] +name = "pin-project" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "pin-project-lite" version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -692,6 +1076,38 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "prost" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "deb1435c188b76130da55f17a466d252ff7b1418b2ad3e037d127b94e3411f29" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-derive" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81bddcdb20abf9501610992b6759a4c888aef7d1a7247ef75e2404275ac24af1" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "prost-types" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9091c90b0a32608e984ff2fa4091273cbdd755d54935c51d520887f4a1dbd5b0" +dependencies = [ + "prost", +] + [[package]] name = "quote" version = "1.0.36" @@ -748,8 +1164,17 @@ checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" dependencies = [ "aho-corasick", "memchr", - "regex-automata", - "regex-syntax", + "regex-automata 0.4.3", + "regex-syntax 0.8.2", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", ] [[package]] @@ -760,9 +1185,15 @@ checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.8.2", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.8.2" @@ -797,6 +1228,12 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + [[package]] name = "ryu" version = "1.0.16" @@ -860,6 +1297,15 @@ dependencies = [ "digest", ] +[[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 = "signal-hook-registry" version = "1.4.1" @@ -869,6 +1315,15 @@ dependencies = [ "libc", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "smallvec" version = "1.11.2" @@ -896,6 +1351,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + [[package]] name = "thiserror" version = "1.0.61" @@ -916,6 +1377,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tiny-keccak" version = "2.0.2" @@ -941,9 +1412,20 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", + "tracing", "windows-sys 0.48.0", ] +[[package]] +name = "tokio-io-timeout" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30b74022ada614a1b4834de765f9bb43877f910cc8ce4be40e89042c9223a8bf" +dependencies = [ + "pin-project-lite", + "tokio", +] + [[package]] name = "tokio-macros" version = "2.3.0" @@ -955,6 +1437,30 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-stream" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "267ac89e0bec6e691e5813911606935d77c476ff49024f98abcea3e7b15e37af" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + [[package]] name = "toml" version = "0.8.14" @@ -982,13 +1488,125 @@ version = "0.22.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38" dependencies = [ - "indexmap", + "indexmap 2.1.0", "serde", "serde_spanned", "toml_datetime", "winnow", ] +[[package]] +name = "tonic" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76c4eb7a4e9ef9d4763600161f12f5070b92a578e1b634db88a6887844c91a13" +dependencies = [ + "async-stream", + "async-trait", + "axum", + "base64 0.21.7", + "bytes", + "h2", + "http", + "http-body", + "hyper", + "hyper-timeout", + "percent-encoding", + "pin-project", + "prost", + "tokio", + "tokio-stream", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "futures-core", + "futures-util", + "indexmap 1.9.3", + "pin-project", + "pin-project-lite", + "rand", + "slab", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" + +[[package]] +name = "tower-service" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "once_cell", + "regex", + "sharded-slab", + "thread_local", + "tracing", + "tracing-core", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + [[package]] name = "typenum" version = "1.17.0" @@ -1029,12 +1647,27 @@ dependencies = [ "rand", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 5b3e6d9b..762dd9bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] # Async tokio = {version = "1.38.0", features = ["full"]} +parking_lot = "0.12.3" # Error handling anyhow = "1.0.86" @@ -16,6 +17,7 @@ thiserror = "1.0.61" # Logging log = "0.4.21" env_logger = "0.11.3" +console-subscriber = "0.3.0" # config / ser / de serde = { version = "1.0.203", features = ["derive"]} diff --git a/src/crates/ferrumc_macros/src/lib.rs b/src/crates/ferrumc_macros/src/lib.rs index bbcb4243..fa242630 100644 --- a/src/crates/ferrumc_macros/src/lib.rs +++ b/src/crates/ferrumc_macros/src/lib.rs @@ -260,7 +260,7 @@ pub fn bake_packet_registry(input: TokenStream) -> TokenStream { match_arms.push(quote! { (#packet_id, #state) => { let packet = crate::packets::incoming::#struct_name_lowercase::#struct_name::decode(cursor).await?; - packet.handle(conn_owned).await?; + packet.handle(&mut conn_owned.clone()).await?; }, }); } @@ -273,11 +273,10 @@ pub fn bake_packet_registry(input: TokenStream) -> TokenStream { let match_arms = match_arms.into_iter(); let output = quote! { - pub async fn handle_packet(packet_id: u8, state: String, conn_owned: &mut crate::Connection, cursor: &mut std::io::Cursor>) -> ferrumc_utils::prelude::Result<()> { - let state = state.as_str(); - match (packet_id, state) { + pub async fn handle_packet(packet_id: u8, conn_owned: &mut Arc>, cursor: &mut std::io::Cursor>) -> ferrumc_utils::prelude::Result<()> { + match (packet_id, conn_owned.read().await.state.as_str()) { #(#match_arms)* - _ => println!("No packet found for ID: 0x{:02X} in state: {}", packet_id, state), + _ => println!("No packet found for ID: 0x{:02X} in state: {}", packet_id, conn_owned.read().await.state.as_str()), } Ok(()) diff --git a/src/crates/ferrumc_net/Cargo.lock b/src/crates/ferrumc_net/Cargo.lock index d20f8b49..f54e481f 100644 --- a/src/crates/ferrumc_net/Cargo.lock +++ b/src/crates/ferrumc_net/Cargo.lock @@ -17,12 +17,43 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anyhow" version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +[[package]] +name = "async-stream" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd56dd203fef61ac097dd65721a419ddccb106b2d2b70ba60a6b529f03961a51" +dependencies = [ + "async-stream-impl", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-stream-impl" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "async-trait" version = "0.1.80" @@ -49,6 +80,51 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +[[package]] +name = "axum" +version = "0.6.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" +dependencies = [ + "async-trait", + "axum-core", + "bitflags 1.3.2", + "bytes", + "futures-util", + "http", + "http-body", + "hyper", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "sync_wrapper", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "759fa577a247914fd3f7f76d62972792636412fbfd634cd452f6a385a74d2d2c" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "mime", + "rustversion", + "tower-layer", + "tower-service", +] + [[package]] name = "backtrace" version = "0.3.73" @@ -76,6 +152,12 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.5.0" @@ -144,6 +226,44 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "console-api" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a257c22cd7e487dd4a13d413beabc512c5052f0bc048db0da6a84c3d8a6142fd" +dependencies = [ + "futures-core", + "prost", + "prost-types", + "tonic", + "tracing-core", +] + +[[package]] +name = "console-subscriber" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31c4cc54bae66f7d9188996404abdf7fdfa23034ef8e43478c8810828abad758" +dependencies = [ + "console-api", + "crossbeam-channel", + "crossbeam-utils", + "futures-task", + "hdrhistogram", + "humantime", + "prost", + "prost-types", + "serde", + "serde_json", + "thread_local", + "tokio", + "tokio-stream", + "tonic", + "tracing", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "const-random" version = "0.1.18" @@ -182,6 +302,30 @@ dependencies = [ "libc", ] +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + [[package]] name = "crunchy" version = "0.2.2" @@ -200,11 +344,12 @@ dependencies = [ [[package]] name = "dashmap" -version = "5.5.3" +version = "6.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" +checksum = "804c8821570c3f8b70230c2ba75ffa5c0f9a4189b9a432b6656c536712acae28" dependencies = [ "cfg-if", + "crossbeam-utils", "hashbrown 0.14.5", "lock_api", "once_cell", @@ -230,6 +375,12 @@ dependencies = [ "const-random", ] +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "equivalent" version = "1.0.1" @@ -253,12 +404,14 @@ dependencies = [ "atomic", "base64 0.22.1", "byteorder", + "console-subscriber", "dashmap", "ferrumc_macros", "ferrumc_utils", "lariv", "lazy_static", "log", + "parking_lot", "rand", "serde", "serde_json", @@ -281,6 +434,61 @@ dependencies = [ "toml", ] +[[package]] +name = "flate2" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f54427cfd1c7829e2a139fcefea601bf088ebca651d2bf53ebc600eac295dae" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -308,6 +516,31 @@ version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" +[[package]] +name = "h2" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap 2.2.6", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hashbrown" version = "0.13.2" @@ -320,12 +553,111 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +[[package]] +name = "hdrhistogram" +version = "7.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "765c9198f173dd59ce26ff9f95ef0aafd0a0fe01fb9d72841bc5066a4c06511d" +dependencies = [ + "base64 0.21.7", + "byteorder", + "flate2", + "nom", + "num-traits", +] + [[package]] name = "hermit-abi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "hyper" +version = "0.14.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f361cde2f109281a220d4307746cdfd5ee3f410da58a70377762396775634b33" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper-timeout" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1" +dependencies = [ + "hyper", + "pin-project-lite", + "tokio", + "tokio-io-timeout", +] + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", +] + [[package]] name = "indexmap" version = "2.2.6" @@ -336,6 +668,15 @@ dependencies = [ "hashbrown 0.14.5", ] +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.11" @@ -393,12 +734,33 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + +[[package]] +name = "matchit" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" + [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -435,6 +797,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -499,6 +870,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + [[package]] name = "pest" version = "2.7.10" @@ -544,12 +921,38 @@ dependencies = [ "sha2", ] +[[package]] +name = "pin-project" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "pin-project-lite" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -565,6 +968,38 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "prost" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "deb1435c188b76130da55f17a466d252ff7b1418b2ad3e037d127b94e3411f29" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-derive" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81bddcdb20abf9501610992b6759a4c888aef7d1a7247ef75e2404275ac24af1" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "prost-types" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9091c90b0a32608e984ff2fa4091273cbdd755d54935c51d520887f4a1dbd5b0" +dependencies = [ + "prost", +] + [[package]] name = "quote" version = "1.0.36" @@ -610,9 +1045,53 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd" dependencies = [ - "bitflags", + "bitflags 2.5.0", +] + +[[package]] +name = "regex" +version = "1.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax 0.8.4", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + [[package]] name = "ron" version = "0.8.1" @@ -620,7 +1099,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94" dependencies = [ "base64 0.21.7", - "bitflags", + "bitflags 2.5.0", "serde", "serde_derive", ] @@ -641,6 +1120,12 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + [[package]] name = "ryu" version = "1.0.18" @@ -704,6 +1189,15 @@ dependencies = [ "digest", ] +[[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 = "signal-hook-registry" version = "1.4.2" @@ -713,6 +1207,15 @@ dependencies = [ "libc", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "smallvec" version = "1.13.2" @@ -740,6 +1243,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + [[package]] name = "thiserror" version = "1.0.61" @@ -760,6 +1269,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tiny-keccak" version = "2.0.2" @@ -785,9 +1304,20 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", + "tracing", "windows-sys 0.48.0", ] +[[package]] +name = "tokio-io-timeout" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30b74022ada614a1b4834de765f9bb43877f910cc8ce4be40e89042c9223a8bf" +dependencies = [ + "pin-project-lite", + "tokio", +] + [[package]] name = "tokio-macros" version = "2.3.0" @@ -799,6 +1329,30 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-stream" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "267ac89e0bec6e691e5813911606935d77c476ff49024f98abcea3e7b15e37af" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + [[package]] name = "toml" version = "0.8.14" @@ -826,13 +1380,125 @@ version = "0.22.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38" dependencies = [ - "indexmap", + "indexmap 2.2.6", "serde", "serde_spanned", "toml_datetime", "winnow", ] +[[package]] +name = "tonic" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76c4eb7a4e9ef9d4763600161f12f5070b92a578e1b634db88a6887844c91a13" +dependencies = [ + "async-stream", + "async-trait", + "axum", + "base64 0.21.7", + "bytes", + "h2", + "http", + "http-body", + "hyper", + "hyper-timeout", + "percent-encoding", + "pin-project", + "prost", + "tokio", + "tokio-stream", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "futures-core", + "futures-util", + "indexmap 1.9.3", + "pin-project", + "pin-project-lite", + "rand", + "slab", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" + +[[package]] +name = "tower-service" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "once_cell", + "regex", + "sharded-slab", + "thread_local", + "tracing", + "tracing-core", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + [[package]] name = "typenum" version = "1.17.0" @@ -867,12 +1533,27 @@ dependencies = [ "rand", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/src/crates/ferrumc_net/Cargo.toml b/src/crates/ferrumc_net/Cargo.toml index dc0de425..8324e7e2 100644 --- a/src/crates/ferrumc_net/Cargo.toml +++ b/src/crates/ferrumc_net/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -dashmap = "5.5.3" +dashmap = "6.0.1" lariv = "0.3.2" lazy_static = "1.4.0" tokio = { version = "1.38.0", features = ["full"] } @@ -18,3 +18,5 @@ ferrumc_macros = { path = "../ferrumc_macros" } serde = { version = "1.0.117", features = ["derive"] } serde_json = "1.0.117" base64 = "0.22.1" +console-subscriber = "0.3.0" +parking_lot = "0.12.3" diff --git a/src/crates/ferrumc_net/src/lib.rs b/src/crates/ferrumc_net/src/lib.rs index 485900f4..03551a2a 100644 --- a/src/crates/ferrumc_net/src/lib.rs +++ b/src/crates/ferrumc_net/src/lib.rs @@ -3,17 +3,19 @@ use std::cmp::PartialEq; use std::fmt::Display; use std::io::Cursor; -use std::sync::{atomic, OnceLock}; +use std::sync::{Arc, atomic, OnceLock}; use std::sync::atomic::AtomicU32; use dashmap::DashMap; use ferrumc_utils::encoding::varint::read_varint; use ferrumc_utils::prelude::*; use lariv::Lariv; +use lazy_static::lazy_static; use log::{debug, trace}; use rand::random; use tokio::io::AsyncReadExt; use tokio::io::AsyncWriteExt; +use tokio::sync::RwLock; use crate::packets::{handle_packet}; @@ -41,12 +43,18 @@ pub enum State { impl Display for State { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.as_str()) + } +} + +impl State { + pub fn as_str(&self) -> &str { match self { - State::Unknown => write!(f, "unknown"), - State::Handshake => write!(f, "handshake"), - State::Status => write!(f, "status"), - State::Login => write!(f, "login"), - State::Play => write!(f, "play"), + State::Unknown => "unknown", + State::Handshake => "handshake", + State::Status => "status", + State::Login => "login", + State::Play => "play", } } } @@ -54,7 +62,7 @@ impl Display for State { pub struct ConnectionList { // The connections, keyed with random values. The value also contains the connection id for ease of access. // pub connections: DashMap, - pub connections: DashMap, + pub connections: DashMap>>, // The number of connections. pub connection_count: AtomicU32, // The queue of connections to be purged. This is used to store the connections to be dropped at the end of every tick. @@ -80,19 +88,24 @@ pub struct ConnectionMetadata { pub protocol_version: i32, } +pub fn setup_tracer() { + console_subscriber::init(); +} + pub async fn handle_connection(socket: tokio::net::TcpStream) -> Result<()> { let mut id = random(); // check if we have a collision (1 in 4.2 billion chance) and if so, generate a new id while CONNECTIONS().connections.contains_key(&id) { id = random(); } - let conn = Connection { + let conn = Arc::new(RwLock::new( + Connection { id, socket, player_uuid: None, - state: State::Unknown, + state: State::Handshake, metadata: ConnectionMetadata::default(), - }; + })); CONNECTIONS().connections.insert(id, conn); CONNECTIONS() @@ -101,35 +114,29 @@ pub async fn handle_connection(socket: tokio::net::TcpStream) -> Result<()> { debug!("Connection established with id: {}. Current connection count: {}", id, CONNECTIONS().connection_count.load(atomic::Ordering::Relaxed)); - let mut conn_ref = CONNECTIONS().connections.get_mut(&id).ok_or(Error::ConnectionNotFound(id))?; - conn_ref.start_connection().await?; + let mut conn_ref = CONNECTIONS().connections.view(&id, |_k, v| {v.clone()}).unwrap(); + manage_conn(&mut conn_ref).await?; Ok(()) } -impl Connection { - pub async fn start_connection(&mut self) -> Result<()> { - self.state = State::Handshake; - trace!("Starting connection with id: {}", self.id); - - self.manage_conn().await?; - - Ok(()) - } - - pub async fn manage_conn(&mut self) -> Result<()> { - trace!("Starting receiver for the same addy: {:?}", self.socket.peer_addr()?); + pub async fn manage_conn(conn: &mut Arc>) -> Result<()> { + debug!("Starting receiver for the same addr: {:?}", conn.read().await.socket.peer_addr()?); loop { let mut length_buffer = vec![0u8; 1]; - self.socket.read_exact(&mut length_buffer).await?; - + { + let mut conn_write = conn.write().await; + conn_write.socket.read_exact(&mut length_buffer).await?; + } let length = length_buffer[0] as usize; let mut buffer = vec![0u8; length]; - self.socket.read_exact(&mut buffer).await?; - + { + let mut conn_write = conn.write().await; + conn_write.socket.read_exact(&mut buffer).await?; + } let buffer = vec![length_buffer, buffer].concat(); let mut cursor = Cursor::new(buffer); @@ -140,7 +147,7 @@ impl Connection { trace!("Packet Length: {}", packet_length); trace!("Packet ID: {}", packet_id); - handle_packet(packet_id.get_val() as u8, self.state.to_string(), self, &mut cursor).await?; + handle_packet(packet_id.get_val() as u8, conn, &mut cursor).await?; // TODO: Check if we need to drop the connection } @@ -148,11 +155,6 @@ impl Connection { Ok(()) } - pub async fn send_packet(&mut self, bytes: Vec) -> Result<()> { - self.socket.write_all(&bytes).await?; - Ok(()) - } - #[allow(dead_code)] async fn drop_conn(connection: &mut Connection) -> Result<()> { trace!("Dropping connection with id: {}", connection.id); @@ -161,6 +163,6 @@ impl Connection { CONNECTIONS().connection_count.fetch_sub(1, atomic::Ordering::Relaxed); Ok(()) } -} + diff --git a/src/crates/ferrumc_net/src/packets/incoming/handshake.rs b/src/crates/ferrumc_net/src/packets/incoming/handshake.rs index b58f052a..8ef2a768 100644 --- a/src/crates/ferrumc_net/src/packets/incoming/handshake.rs +++ b/src/crates/ferrumc_net/src/packets/incoming/handshake.rs @@ -1,4 +1,5 @@ -use log::info; +use std::sync::Arc; +use log::{debug, info}; use ferrumc_macros::{Decode, packet}; use ferrumc_utils::encoding::varint::VarInt; use crate::{Connection, State}; @@ -14,11 +15,17 @@ pub struct Handshake { } impl IncomingPacket for Handshake { - async fn handle(&self, conn: &mut Connection) -> Result<(), Error> { + async fn handle(&self, conn: &mut Arc>) -> Result<(), Error> { info!("Handling handshake packet"); - conn.metadata.protocol_version = self.protocol_version.get_val(); - conn.state = match self.next_state.get_val() { + debug!("Obtaining connection lock for protocol version and state"); + + let mut conn_write = conn.write().await; + + debug!("Successfully obtained connection lock for protocol version and state"); + + conn_write.metadata.protocol_version = 763; + conn_write.state = match self.next_state.get_val() { 1 => State::Status, 2 => State::Login, s => {return Err(Error::InvalidState(s as u32))} diff --git a/src/crates/ferrumc_net/src/packets/incoming/ping.rs b/src/crates/ferrumc_net/src/packets/incoming/ping.rs index b369c7b2..7e4f7df5 100644 --- a/src/crates/ferrumc_net/src/packets/incoming/ping.rs +++ b/src/crates/ferrumc_net/src/packets/incoming/ping.rs @@ -1,4 +1,6 @@ +use std::sync::Arc; use log::info; +use tokio::io::AsyncWriteExt; use ferrumc_macros::{Decode, packet}; use ferrumc_utils::encoding::varint::VarInt; use crate::Connection; @@ -12,7 +14,7 @@ pub struct Ping { } impl IncomingPacket for Ping { - async fn handle(&self, conn: &mut Connection) -> Result<(), ferrumc_utils::error::Error> { + async fn handle(&self, conn: &mut Arc>) -> Result<(), ferrumc_utils::error::Error> { info!("Handling ping packet"); let response = OutgoingPing { @@ -22,6 +24,6 @@ impl IncomingPacket for Ping { let response = response.encode().await?; - conn.send_packet(response).await + conn.write().await.socket.write_all(&response).await.map_err(|e| e.into()) } } \ No newline at end of file diff --git a/src/crates/ferrumc_net/src/packets/incoming/status.rs b/src/crates/ferrumc_net/src/packets/incoming/status.rs index e1276983..bd6d18f2 100644 --- a/src/crates/ferrumc_net/src/packets/incoming/status.rs +++ b/src/crates/ferrumc_net/src/packets/incoming/status.rs @@ -1,3 +1,4 @@ +use std::sync::Arc; use tokio::sync::OnceCell; use log::info; use serde::Serialize; @@ -6,7 +7,7 @@ use ferrumc_utils::encoding::varint::VarInt; use crate::Connection; use crate::packets::IncomingPacket; use crate::packets::outgoing::status::OutgoingStatusResponse; -use tokio::io::AsyncReadExt; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; use ferrumc_utils::config; use base64::{Engine}; @@ -47,7 +48,7 @@ struct Description { } impl IncomingPacket for Status { - async fn handle(&self, conn: &mut Connection) -> Result<(), ferrumc_utils::error::Error> { + async fn handle(&self, conn: &mut Arc>) -> Result<(), ferrumc_utils::error::Error> { info!("Handling status request packet"); let config = config::get_global_config(); @@ -57,7 +58,7 @@ impl IncomingPacket for Status { version: Version { name: "1.20.6".to_string(), // Allow any protocol version for now. To check the ping and stuff - protocol: conn.metadata.protocol_version.clone() as u32, + protocol: conn.read().await.metadata.protocol_version.clone() as u32, }, players: Players { max: config.max_players, @@ -82,7 +83,7 @@ impl IncomingPacket for Status { let response = response.encode().await?; - conn.send_packet(response).await + conn.write().await.socket.write_all(&response).await.map_err(|e| e.into()) } } async fn get_encoded_favicon() -> &'static String { diff --git a/src/crates/ferrumc_net/src/packets/mod.rs b/src/crates/ferrumc_net/src/packets/mod.rs index 3a392ecf..9df28bd6 100644 --- a/src/crates/ferrumc_net/src/packets/mod.rs +++ b/src/crates/ferrumc_net/src/packets/mod.rs @@ -1,13 +1,18 @@ +use std::sync::Arc; + +use tokio::sync::RwLock; + use ferrumc_macros::bake_packet_registry; use ferrumc_utils::error::Error; + use crate::Connection; pub mod incoming; pub mod outgoing; pub trait IncomingPacket { - async fn handle(&self, conn: &mut Connection) -> Result<(), Error>; + async fn handle(&self, conn: &mut std::sync::Arc>) -> Result<(), Error>; } -bake_packet_registry!("\\src\\packets\\incoming"); \ No newline at end of file +bake_packet_registry!("\\src\\packets\\incoming"); diff --git a/src/main.rs b/src/main.rs index 1d0fc821..58757afa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ type SafeConfig = Arc>; #[tokio::main] async fn main() -> Result<()> { + ferrumc_net::setup_tracer(); utils::setup_logger(); info!("Initializing server..."); From 1ffba0b442078a520885e05aad992fe9b32ebaa5 Mon Sep 17 00:00:00 2001 From: ReCore-sys Date: Wed, 26 Jun 2024 22:13:43 +0930 Subject: [PATCH 2/4] Now freezing up somewhere else --- src/crates/ferrumc_macros/src/lib.rs | 3 ++- src/crates/ferrumc_net/src/lib.rs | 7 +++++++ .../ferrumc_net/src/packets/incoming/handshake.rs | 13 +++---------- src/crates/ferrumc_net/src/packets/incoming/ping.rs | 4 ++-- .../ferrumc_net/src/packets/incoming/status.rs | 6 +++--- src/crates/ferrumc_net/src/packets/mod.rs | 4 ++-- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/crates/ferrumc_macros/src/lib.rs b/src/crates/ferrumc_macros/src/lib.rs index fa242630..d0239a1f 100644 --- a/src/crates/ferrumc_macros/src/lib.rs +++ b/src/crates/ferrumc_macros/src/lib.rs @@ -260,7 +260,7 @@ pub fn bake_packet_registry(input: TokenStream) -> TokenStream { match_arms.push(quote! { (#packet_id, #state) => { let packet = crate::packets::incoming::#struct_name_lowercase::#struct_name::decode(cursor).await?; - packet.handle(&mut conn_owned.clone()).await?; + packet.handle(&mut conn).await?; }, }); } @@ -274,6 +274,7 @@ pub fn bake_packet_registry(input: TokenStream) -> TokenStream { let output = quote! { pub async fn handle_packet(packet_id: u8, conn_owned: &mut Arc>, cursor: &mut std::io::Cursor>) -> ferrumc_utils::prelude::Result<()> { + let mut conn = conn_owned.write().await; match (packet_id, conn_owned.read().await.state.as_str()) { #(#match_arms)* _ => println!("No packet found for ID: 0x{:02X} in state: {}", packet_id, conn_owned.read().await.state.as_str()), diff --git a/src/crates/ferrumc_net/src/lib.rs b/src/crates/ferrumc_net/src/lib.rs index 03551a2a..af35db46 100644 --- a/src/crates/ferrumc_net/src/lib.rs +++ b/src/crates/ferrumc_net/src/lib.rs @@ -124,11 +124,13 @@ pub async fn handle_connection(socket: tokio::net::TcpStream) -> Result<()> { debug!("Starting receiver for the same addr: {:?}", conn.read().await.socket.peer_addr()?); loop { + debug!("1"); let mut length_buffer = vec![0u8; 1]; { let mut conn_write = conn.write().await; conn_write.socket.read_exact(&mut length_buffer).await?; } + debug!("2"); let length = length_buffer[0] as usize; let mut buffer = vec![0u8; length]; @@ -137,17 +139,22 @@ pub async fn handle_connection(socket: tokio::net::TcpStream) -> Result<()> { let mut conn_write = conn.write().await; conn_write.socket.read_exact(&mut buffer).await?; } + debug!("3"); let buffer = vec![length_buffer, buffer].concat(); let mut cursor = Cursor::new(buffer); let packet_length = read_varint(&mut cursor).await?; let packet_id = read_varint(&mut cursor).await?; + debug!("4"); trace!("Packet Length: {}", packet_length); trace!("Packet ID: {}", packet_id); + debug!("5"); handle_packet(packet_id.get_val() as u8, conn, &mut cursor).await?; + + debug!("6"); // TODO: Check if we need to drop the connection } diff --git a/src/crates/ferrumc_net/src/packets/incoming/handshake.rs b/src/crates/ferrumc_net/src/packets/incoming/handshake.rs index 8ef2a768..bc5f8cbb 100644 --- a/src/crates/ferrumc_net/src/packets/incoming/handshake.rs +++ b/src/crates/ferrumc_net/src/packets/incoming/handshake.rs @@ -15,17 +15,10 @@ pub struct Handshake { } impl IncomingPacket for Handshake { - async fn handle(&self, conn: &mut Arc>) -> Result<(), Error> { - info!("Handling handshake packet"); + async fn handle(&self, conn: &mut tokio::sync::RwLockWriteGuard<'_, Connection>) -> Result<(), Error> { - debug!("Obtaining connection lock for protocol version and state"); - - let mut conn_write = conn.write().await; - - debug!("Successfully obtained connection lock for protocol version and state"); - - conn_write.metadata.protocol_version = 763; - conn_write.state = match self.next_state.get_val() { + conn.metadata.protocol_version = 763; + conn.state = match self.next_state.get_val() { 1 => State::Status, 2 => State::Login, s => {return Err(Error::InvalidState(s as u32))} diff --git a/src/crates/ferrumc_net/src/packets/incoming/ping.rs b/src/crates/ferrumc_net/src/packets/incoming/ping.rs index 7e4f7df5..8216e8b6 100644 --- a/src/crates/ferrumc_net/src/packets/incoming/ping.rs +++ b/src/crates/ferrumc_net/src/packets/incoming/ping.rs @@ -14,7 +14,7 @@ pub struct Ping { } impl IncomingPacket for Ping { - async fn handle(&self, conn: &mut Arc>) -> Result<(), ferrumc_utils::error::Error> { + async fn handle(&self, conn: &mut tokio::sync::RwLockWriteGuard<'_, Connection>) -> Result<(), ferrumc_utils::error::Error> { info!("Handling ping packet"); let response = OutgoingPing { @@ -24,6 +24,6 @@ impl IncomingPacket for Ping { let response = response.encode().await?; - conn.write().await.socket.write_all(&response).await.map_err(|e| e.into()) + conn.socket.write_all(&response).await.map_err(|e| e.into()) } } \ No newline at end of file diff --git a/src/crates/ferrumc_net/src/packets/incoming/status.rs b/src/crates/ferrumc_net/src/packets/incoming/status.rs index bd6d18f2..c056ce8c 100644 --- a/src/crates/ferrumc_net/src/packets/incoming/status.rs +++ b/src/crates/ferrumc_net/src/packets/incoming/status.rs @@ -48,7 +48,7 @@ struct Description { } impl IncomingPacket for Status { - async fn handle(&self, conn: &mut Arc>) -> Result<(), ferrumc_utils::error::Error> { + async fn handle(&self, conn: &mut tokio::sync::RwLockWriteGuard<'_, Connection>) -> Result<(), ferrumc_utils::error::Error> { info!("Handling status request packet"); let config = config::get_global_config(); @@ -58,7 +58,7 @@ impl IncomingPacket for Status { version: Version { name: "1.20.6".to_string(), // Allow any protocol version for now. To check the ping and stuff - protocol: conn.read().await.metadata.protocol_version.clone() as u32, + protocol: conn.metadata.protocol_version.clone() as u32, }, players: Players { max: config.max_players, @@ -83,7 +83,7 @@ impl IncomingPacket for Status { let response = response.encode().await?; - conn.write().await.socket.write_all(&response).await.map_err(|e| e.into()) + conn.socket.write_all(&response).await.map_err(|e| e.into()) } } async fn get_encoded_favicon() -> &'static String { diff --git a/src/crates/ferrumc_net/src/packets/mod.rs b/src/crates/ferrumc_net/src/packets/mod.rs index 9df28bd6..110a7e35 100644 --- a/src/crates/ferrumc_net/src/packets/mod.rs +++ b/src/crates/ferrumc_net/src/packets/mod.rs @@ -11,8 +11,8 @@ pub mod incoming; pub mod outgoing; pub trait IncomingPacket { - async fn handle(&self, conn: &mut std::sync::Arc>) -> Result<(), Error>; + async fn handle(&self, conn: &mut tokio::sync::RwLockWriteGuard) -> Result<(), Error>; } -bake_packet_registry!("\\src\\packets\\incoming"); +bake_packet_registry!("\\src\\packets\\incoming"); \ No newline at end of file From 5427346781ba8cb6a81f0a37cf7cd4b6b36b6ee8 Mon Sep 17 00:00:00 2001 From: ReCore-sys Date: Wed, 26 Jun 2024 22:19:52 +0930 Subject: [PATCH 3/4] I was stupid but fixed it anyway --- src/crates/ferrumc_net/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crates/ferrumc_net/src/lib.rs b/src/crates/ferrumc_net/src/lib.rs index af35db46..6a17d766 100644 --- a/src/crates/ferrumc_net/src/lib.rs +++ b/src/crates/ferrumc_net/src/lib.rs @@ -153,7 +153,7 @@ pub async fn handle_connection(socket: tokio::net::TcpStream) -> Result<()> { debug!("5"); handle_packet(packet_id.get_val() as u8, conn, &mut cursor).await?; - + debug!("6"); // TODO: Check if we need to drop the connection From 17dbd9fd2eadfbcdcf924b1849884d1356bf205b Mon Sep 17 00:00:00 2001 From: ReCore-sys Date: Wed, 26 Jun 2024 22:43:25 +0930 Subject: [PATCH 4/4] Fixed + Clean up --- Cargo.lock | 2 - Cargo.toml | 1 - src/crates/ferrumc_macros/src/lib.rs | 2 +- src/crates/ferrumc_net/Cargo.lock | 1 - src/crates/ferrumc_net/Cargo.toml | 1 - src/crates/ferrumc_net/src/lib.rs | 104 ++++++++++-------- .../ferrumc_net/src/packets/incoming/ping.rs | 2 + 7 files changed, 60 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e2cde756..c712ac17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -482,7 +482,6 @@ dependencies = [ "ferrumc_utils", "lazy_static", "log", - "parking_lot", "serde", "thiserror", "tokio", @@ -513,7 +512,6 @@ dependencies = [ "lariv", "lazy_static", "log", - "parking_lot", "rand", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 762dd9bf..b780b016 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ edition = "2021" [dependencies] # Async tokio = {version = "1.38.0", features = ["full"]} -parking_lot = "0.12.3" # Error handling anyhow = "1.0.86" diff --git a/src/crates/ferrumc_macros/src/lib.rs b/src/crates/ferrumc_macros/src/lib.rs index d0239a1f..d9720081 100644 --- a/src/crates/ferrumc_macros/src/lib.rs +++ b/src/crates/ferrumc_macros/src/lib.rs @@ -275,7 +275,7 @@ pub fn bake_packet_registry(input: TokenStream) -> TokenStream { let output = quote! { pub async fn handle_packet(packet_id: u8, conn_owned: &mut Arc>, cursor: &mut std::io::Cursor>) -> ferrumc_utils::prelude::Result<()> { let mut conn = conn_owned.write().await; - match (packet_id, conn_owned.read().await.state.as_str()) { + match (packet_id, conn.state.as_str()) { #(#match_arms)* _ => println!("No packet found for ID: 0x{:02X} in state: {}", packet_id, conn_owned.read().await.state.as_str()), } diff --git a/src/crates/ferrumc_net/Cargo.lock b/src/crates/ferrumc_net/Cargo.lock index f54e481f..d99dac6d 100644 --- a/src/crates/ferrumc_net/Cargo.lock +++ b/src/crates/ferrumc_net/Cargo.lock @@ -411,7 +411,6 @@ dependencies = [ "lariv", "lazy_static", "log", - "parking_lot", "rand", "serde", "serde_json", diff --git a/src/crates/ferrumc_net/Cargo.toml b/src/crates/ferrumc_net/Cargo.toml index 8324e7e2..648958c8 100644 --- a/src/crates/ferrumc_net/Cargo.toml +++ b/src/crates/ferrumc_net/Cargo.toml @@ -19,4 +19,3 @@ serde = { version = "1.0.117", features = ["derive"] } serde_json = "1.0.117" base64 = "0.22.1" console-subscriber = "0.3.0" -parking_lot = "0.12.3" diff --git a/src/crates/ferrumc_net/src/lib.rs b/src/crates/ferrumc_net/src/lib.rs index 6a17d766..293e6c70 100644 --- a/src/crates/ferrumc_net/src/lib.rs +++ b/src/crates/ferrumc_net/src/lib.rs @@ -15,7 +15,7 @@ use log::{debug, trace}; use rand::random; use tokio::io::AsyncReadExt; use tokio::io::AsyncWriteExt; -use tokio::sync::RwLock; +use tokio::sync::{RwLock, RwLockWriteGuard}; use crate::packets::{handle_packet}; @@ -81,6 +81,8 @@ pub struct Connection { pub state: State, // Metadata pub metadata: ConnectionMetadata, + // Whether to drop and clean up the connection + pub drop: bool, } #[derive(Debug, Default)] @@ -100,13 +102,15 @@ pub async fn handle_connection(socket: tokio::net::TcpStream) -> Result<()> { } let conn = Arc::new(RwLock::new( Connection { - id, - socket, - player_uuid: None, - state: State::Handshake, - metadata: ConnectionMetadata::default(), - })); - + id, + socket, + player_uuid: None, + state: State::Handshake, + metadata: ConnectionMetadata::default(), + drop: false, + })); + + // Add the connection to the connections list CONNECTIONS().connections.insert(id, conn); CONNECTIONS() .connection_count @@ -114,62 +118,68 @@ pub async fn handle_connection(socket: tokio::net::TcpStream) -> Result<()> { debug!("Connection established with id: {}. Current connection count: {}", id, CONNECTIONS().connection_count.load(atomic::Ordering::Relaxed)); - let mut conn_ref = CONNECTIONS().connections.view(&id, |_k, v| {v.clone()}).unwrap(); + // Get a reference to the connection + let mut conn_ref = CONNECTIONS().connections.view(&id, |_k, v| { v.clone() }).unwrap(); manage_conn(&mut conn_ref).await?; Ok(()) } - pub async fn manage_conn(conn: &mut Arc>) -> Result<()> { - debug!("Starting receiver for the same addr: {:?}", conn.read().await.socket.peer_addr()?); +pub async fn manage_conn(conn: &mut Arc>) -> Result<()> { + debug!("Starting receiver for the same addr: {:?}", conn.read().await.socket.peer_addr()?); + + loop { + // Get the length of the packet + let mut length_buffer = vec![0u8; 1]; + { + let mut conn_write = conn.write().await; + conn_write.socket.read_exact(&mut length_buffer).await?; + } - loop { - debug!("1"); - let mut length_buffer = vec![0u8; 1]; - { - let mut conn_write = conn.write().await; - conn_write.socket.read_exact(&mut length_buffer).await?; - } - debug!("2"); - let length = length_buffer[0] as usize; + let length = length_buffer[0] as usize; - let mut buffer = vec![0u8; length]; + // Get the rest of the packet + let mut buffer = vec![0u8; length]; + + { + let mut conn_write = conn.write().await; + conn_write.socket.read_exact(&mut buffer).await?; + } - { - let mut conn_write = conn.write().await; - conn_write.socket.read_exact(&mut buffer).await?; - } - debug!("3"); - let buffer = vec![length_buffer, buffer].concat(); + let buffer = vec![length_buffer, buffer].concat(); - let mut cursor = Cursor::new(buffer); + let mut cursor = Cursor::new(buffer); - let packet_length = read_varint(&mut cursor).await?; - let packet_id = read_varint(&mut cursor).await?; - debug!("4"); + // Get the packet length and id + let packet_length = read_varint(&mut cursor).await?; + let packet_id = read_varint(&mut cursor).await?; - trace!("Packet Length: {}", packet_length); - trace!("Packet ID: {}", packet_id); - debug!("5"); + trace!("Packet Length: {}", packet_length); + trace!("Packet ID: {}", packet_id); - handle_packet(packet_id.get_val() as u8, conn, &mut cursor).await?; + // Handle the packet + handle_packet(packet_id.get_val() as u8, conn, &mut cursor).await?; - debug!("6"); + // Check if we need to drop the connection + let do_drop = conn.read().await.drop; + let id = conn.read().await.id; - // TODO: Check if we need to drop the connection + // Drop the connection if needed + if do_drop { + drop_conn(id).await?; + conn.write().await.socket.shutdown().await?; + break; } - #[allow(unreachable_code)] - Ok(()) } + Ok(()) +} - #[allow(dead_code)] - async fn drop_conn(connection: &mut Connection) -> Result<()> { - trace!("Dropping connection with id: {}", connection.id); - let id = connection.id; - CONNECTIONS().connections.remove(&id); - CONNECTIONS().connection_count.fetch_sub(1, atomic::Ordering::Relaxed); - Ok(()) - } +async fn drop_conn(connection_id: u32) -> Result<()> { + debug!("Dropping connection with id: {}", connection_id); + CONNECTIONS().connections.remove(&connection_id); + CONNECTIONS().connection_count.fetch_sub(1, atomic::Ordering::Relaxed); + Ok(()) +} diff --git a/src/crates/ferrumc_net/src/packets/incoming/ping.rs b/src/crates/ferrumc_net/src/packets/incoming/ping.rs index 8216e8b6..d7e21773 100644 --- a/src/crates/ferrumc_net/src/packets/incoming/ping.rs +++ b/src/crates/ferrumc_net/src/packets/incoming/ping.rs @@ -23,6 +23,8 @@ impl IncomingPacket for Ping { }; let response = response.encode().await?; + + conn.drop = true; conn.socket.write_all(&response).await.map_err(|e| e.into()) }