From f92f9827c65ca900e3fe696846947f36b13de716 Mon Sep 17 00:00:00 2001 From: GIKExe <72767917+GIKExe@users.noreply.github.com> Date: Tue, 29 Apr 2025 22:42:55 +0300 Subject: [PATCH] =?UTF-8?q?=D1=85=D1=83=D0=B9=D0=BD=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 4 ++++ Cargo.lock | 37 +++++++++++++++++++++++++++++ Cargo.toml | 3 ++- src/data.rs | 25 -------------------- src/main.rs | 54 ++++++++++++++++++------------------------- 5 files changed, 66 insertions(+), 57 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 src/data.rs diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a2a5321 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "editor.fontFamily": "Fira Code", + "editor.fontLigatures": true +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 5db86f0..52d9767 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -56,6 +56,25 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "flate2" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ced92e76e966ca2fd84c8f7aa01a4aea65b0eb6648d72f7c8f3e2764a67fece" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + [[package]] name = "gimli" version = "0.31.1" @@ -169,10 +188,22 @@ dependencies = [ "bitflags", ] +[[package]] +name = "rust_mc_proto_tokio" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d60654d16d2a3b9c59dc248cde3864881f6e926a8611280bf0ef1a157bf345b" +dependencies = [ + "flate2", + "tokio", + "uuid", +] + [[package]] name = "rust_minecraft_server" version = "0.1.0" dependencies = [ + "rust_mc_proto_tokio", "tokio", ] @@ -259,6 +290,12 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" +[[package]] +name = "uuid" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index b379c63..ddb084f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2024" [dependencies] -tokio = { version = "1.0", features = ["full"]} \ No newline at end of file +tokio = { version = "1.0", features = ["full"]} +rust_mc_proto_tokio = "0.1.18" \ No newline at end of file diff --git a/src/data.rs b/src/data.rs deleted file mode 100644 index a88663a..0000000 --- a/src/data.rs +++ /dev/null @@ -1,25 +0,0 @@ - -pub struct VarInt; - -impl VarInt { - // Константы объявляются внутри блока impl - pub const MAX_VARINT_SIZE: i32 = 5; - pub const DATA_BITS_MASK: i32 = 127; - pub const CONTINUATION_BIT_MASK: i32 = 128; - pub const DATA_BITS_PER_BYTE: i32 = 7; - - pub fn getByteSize(i: i32) -> i32 { - for j in 1..5 { - if (i & -1 << (j * 7)) == 0 { - return j; - } - } - return 5; - } - - pub fn hasContinuationBit(b0: u8) -> bool { - return (b0 & 128) == 128 - } - - -} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 67c0256..a46b061 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,41 +1,33 @@ -pub(crate) mod data; + +use std::net::SocketAddr; use tokio::io::AsyncReadExt; use tokio::net::{TcpListener, TcpStream}; -use data::VarInt; + +use rust_mc_proto_tokio::{packet, prelude::*, MCConnTcp, MinecraftConnection, Packet, ProtocolError}; + #[tokio::main] -async fn main() -> Result<(), Box> { - // 1. Создаём TCP listener на порту 25565 - let listener = TcpListener::bind("127.0.0.1:25565").await?; - println!("Listening on port 25565..."); +async fn main() { + let listener = match TcpListener::bind("127.0.0.1:25565").await { + Ok(v) => v, + Err(e) => { println!("Не удалось забиндить сервер: {}", e); return; } + }; - // 2. Асинхронно принимаем входящие соединения - while let Ok((stream, _)) = listener.accept().await { - // Для каждого соединения создаём отдельную задачу - tokio::spawn(handle_connection(stream)); + while let Ok((stream, addr)) = listener.accept().await { + tokio::spawn(handle_connection(stream, addr)); } - - Ok(()) } -async fn handle_connection(mut stream: TcpStream) { - let mut firstByte = [0]; - let Ok(n) = stream.read(&mut firstByte).await else { return }; - - // let mut buffer = [0; 1024]; - - // // 3. Читаем данные из потока - // while let Ok(n) = stream.read(&mut buffer).await { - // if n == 0 { - // // Соединение закрыто - // break; - // } - - // // 4. Декодируем байты в UTF-8, пропуская ошибки - // let received = String::from_utf8_lossy(&buffer[..n]); - // print!("{}", received); - // } - - // 5. Соединение автоматически закрывается при выходе из области видимости +async fn handle_connection(stream: TcpStream, addr: SocketAddr) { + let mut conn = MinecraftConnection::new(stream); + println!("Подключение: {}", addr); + loop { + let Ok(mut packet) = conn.read_packet().await else {break;}; + let Ok(x) = packet.read_bytes(packet.len()).await else { + println!("X"); break; + }; + println!("{}", String::from_utf8_lossy(&x)); + } + conn.close().await; } \ No newline at end of file