From 20ce06657d451cfa49d84232a31e85ed1bf0c5bf Mon Sep 17 00:00:00 2001 From: tomoron Date: Sun, 10 May 2026 18:13:04 +0200 Subject: [PATCH] reorganize file structure, define rpc protocol --- Cargo.lock | 467 +++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/main.rs | 60 ++-- src/{ => minecraft}/client.rs | 33 ++- src/{ => minecraft}/handshake.rs | 2 +- src/minecraft/mod.rs | 7 + src/minecraft/socket.rs | 33 +++ src/{ => minecraft}/varint.rs | 0 src/rpc/actions/mod.rs | 2 + src/rpc/actions/ping.rs | 5 + src/rpc/mod.rs | 5 + src/rpc/socket.rs | 22 ++ 12 files changed, 591 insertions(+), 48 deletions(-) rename src/{ => minecraft}/client.rs (67%) rename src/{ => minecraft}/handshake.rs (95%) create mode 100644 src/minecraft/mod.rs create mode 100644 src/minecraft/socket.rs rename src/{ => minecraft}/varint.rs (100%) create mode 100644 src/rpc/actions/mod.rs create mode 100644 src/rpc/actions/ping.rs create mode 100644 src/rpc/mod.rs create mode 100644 src/rpc/socket.rs diff --git a/Cargo.lock b/Cargo.lock index 3ce30e8..183440a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,22 +2,349 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "async-channel" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35" +dependencies = [ + "concurrent-queue", + "event-listener 2.5.3", + "futures-core", +] + +[[package]] +name = "async-channel" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "924ed96dd52d1b75e9c1a3e6275715fd320f5f9439fb5a4a11fa51f4221158d2" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-executor" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c96bf972d85afc50bf5ab8fe2d54d1586b4e0b46c97c50a0c9e71e2f7bcd812a" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "pin-project-lite", + "slab", +] + +[[package]] +name = "async-global-executor" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05b1b633a2115cd122d73b955eadd9916c18c8f510ec9cd1686404c60ad1c29c" +dependencies = [ + "async-channel 2.5.0", + "async-executor", + "async-io", + "async-lock", + "blocking", + "futures-lite", + "once_cell", +] + +[[package]] +name = "async-io" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456b8a8feb6f42d237746d4b3e9a178494627745c3c56c6ea55d92ba50d026fc" +dependencies = [ + "autocfg", + "cfg-if", + "concurrent-queue", + "futures-io", + "futures-lite", + "parking", + "polling", + "rustix", + "slab", + "windows-sys", +] + +[[package]] +name = "async-lock" +version = "3.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f7f2596bd5b78a9fec8088ccd89180d7f9f55b94b0576823bbbdc72ee8311" +dependencies = [ + "event-listener 5.4.1", + "event-listener-strategy", + "pin-project-lite", +] + +[[package]] +name = "async-std" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c8e079a4ab67ae52b7403632e4618815d6db36d2a010cfe41b02c1b1578f93b" +dependencies = [ + "async-channel 1.9.0", + "async-global-executor", + "async-io", + "async-lock", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite", + "gloo-timers", + "kv-log-macro", + "log", + "memchr", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + +[[package]] +name = "async-task" +version = "4.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "bitflags" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" + +[[package]] +name = "blocking" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e83f8d02be6967315521be875afa792a316e28d57b5a2d401897e2a7921b7f21" +dependencies = [ + "async-channel 2.5.0", + "async-task", + "futures-io", + "futures-lite", + "piper", +] + +[[package]] +name = "bumpalo" +version = "3.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[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 = "event-listener" +version = "2.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" + +[[package]] +name = "event-listener" +version = "5.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13b66accf52311f30a0db42147dadea9850cb48cd070028831ae5f5d4b856ab" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93" +dependencies = [ + "event-listener 5.4.1", + "pin-project-lite", +] + +[[package]] +name = "fastrand" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" + +[[package]] +name = "futures-channel" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-io" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718" + +[[package]] +name = "futures-lite" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "parking", + "pin-project-lite", +] + +[[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-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "gloo-timers" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbb143cf96099802033e0d4f4963b19fd2e0b728bcf076cd9cf7f6634f092994" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + +[[package]] +name = "js-sys" +version = "0.3.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67df7112613f8bfd9150013a0314e196f4800d3201ae742489d999db2f979f08" +dependencies = [ + "cfg-if", + "futures-util", + "once_cell", + "wasm-bindgen", +] + [[package]] name = "json" version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + [[package]] name = "libc" version = "0.2.186" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" +dependencies = [ + "value-bag", +] + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + [[package]] name = "minecraft_proxy" version = "0.1.0" dependencies = [ + "async-std", "json", "tokio", ] @@ -33,12 +360,55 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + [[package]] name = "pin-project-lite" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "piper" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c835479a4443ded371d6c535cbfd8d31ad92c5d23ae9770a61bc155e4992a3c1" +dependencies = [ + "atomic-waker", + "fastrand", + "futures-io", +] + +[[package]] +name = "polling" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0e4f59085d47d8241c88ead0f274e8a0cb551f3625263c05eb8dd897c34218" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi", + "pin-project-lite", + "rustix", + "windows-sys", +] + [[package]] name = "proc-macro2" version = "1.0.106" @@ -57,6 +427,41 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rustix" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[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 = "socket2" version = "0.6.3" @@ -87,6 +492,7 @@ dependencies = [ "libc", "mio", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys", @@ -109,12 +515,73 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "value-bag" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ba6f5989077681266825251a52748b8c1d8a4ad098cc37e440103d0ea717fc0" + [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "wasm-bindgen" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49ace1d07c165b0864824eee619580c4689389afa9dc9ed3a4c75040d82e6790" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96492d0d3ffba25305a7dc88720d250b1401d7edca02cc3bcd50633b424673b8" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e68e6f4afd367a562002c05637acb8578ff2dea1943df76afb9e83d177c8578" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d95a9ec35c64b2a7cb35d3fead40c4238d0940c86d107136999567a4703259f2" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4e0100b01e9f0d03189a92b96772a1fb998639d981193d7dbab487302513441" +dependencies = [ + "unicode-ident", +] + [[package]] name = "windows-link" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 5018221..f83aee4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,6 @@ version = "0.1.0" edition = "2024" [dependencies] +async-std = "1.13.2" json = "0.12.4" -tokio = { version = "1.52.1", features = ["net", "rt", "macros"] } +tokio = { version = "1.52.1", features = ["net", "rt", "macros", "signal"] } diff --git a/src/main.rs b/src/main.rs index e397be2..44db682 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,48 +1,34 @@ -use tokio::net::{TcpListener, TcpStream}; -mod client; -use client::Client; +use tokio::net::TcpListener; use std::io; -pub mod varint; -pub mod handshake; +mod minecraft; +use minecraft::process_mc_socket; - -async fn process_socket(stream: TcpStream) -> io::Result<()> { - let mut buf = vec![0 as u8; 1024]; - - let mut client = Client::create(stream); - - loop { - client.in_stream.readable().await?; - - match client.in_stream.try_read(&mut buf) { - Ok(n) => { - let _ = client.buffer_append((&buf[..n]).to_vec()).await; - if n == 0 { - break; - } - } - Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { - continue; - } - Err(e) => { - return Err(e.into()); - } - } - } - Ok(()) -} +mod rpc; +use rpc::process_rpc_socket; #[tokio::main(flavor = "current_thread")] async fn main() -> io::Result<()> { - let listener = TcpListener::bind("0.0.0.0:25565").await?; + let mc_listener = TcpListener::bind("0.0.0.0:25565").await?; + let rpc_listener = TcpListener::bind("0.0.0.0:8080").await?; loop { - let (socket, _) = listener.accept().await?; - tokio::spawn(async move { - if let Err(e) = process_socket(socket).await { - eprintln!("error: {:?}", e); + tokio::select! { + Ok((socket, _)) = mc_listener.accept() => { + tokio::spawn(async move { + if let Err(e) = process_mc_socket(socket).await { + eprintln!("mc error: {:?}", e); + } + }); } - }); + + Ok((socket, _)) = rpc_listener.accept() => { + tokio::spawn(async move { + if let Err(e) = process_rpc_socket(socket).await { + eprintln!("rpc error: {:?}", e); + } + }); + } + } } } diff --git a/src/client.rs b/src/minecraft/client.rs similarity index 67% rename from src/client.rs rename to src/minecraft/client.rs index fcfbd75..6eda7ab 100644 --- a/src/client.rs +++ b/src/minecraft/client.rs @@ -1,8 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* client.rs :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/05/07 17:23:09 by tomoron #+# #+# */ +/* Updated: 2026/05/07 17:38:13 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + use json::object; use tokio::net::TcpStream; use std::collections::VecDeque; -use crate::handshake::Handshake; -use crate::varint::{varint_read, varint_write}; +use crate::minecraft::handshake::Handshake; +use crate::minecraft::varint::{varint_read, varint_write}; pub struct Client { pub in_stream: TcpStream, @@ -34,7 +46,11 @@ impl Client { return Ok(()); } - let _ = self.buffer.append(&mut (data.clone())); + if self.buffer.len() + data.len() > 65536 { + return Err("buffer full".to_string()); + } + + let _ = self.buffer.extend(data); if self.buffer[0] == 0 { return Err("invalid packet".to_string()); @@ -53,8 +69,8 @@ impl Client { sent_data.extend(data); match self.in_stream.try_write(sent_data.as_slice()) { - Ok(len) => { println!("sent {} bytes to the client", len); }, - Err(e) => { println!("error while sending response {:?}", e); } + Err(e) => { eprintln!("error while sending response {:?}", e); }, + _ => { } } } @@ -63,7 +79,6 @@ impl Client { } fn handle_packet(&mut self, mut packet: VecDeque) -> Result<(), String> { - println!("len : {}" , packet.len()); let packet_id = varint_read(&mut packet)?; if self.handshake.is_none() { if packet_id != 0 { @@ -83,17 +98,17 @@ impl Client { Ok(()) } - fn status_intent_handle(&self, mut packet: &mut VecDeque, packet_id: i32) -> Result<(),String> { + fn status_intent_handle(&self, packet: &mut VecDeque, packet_id: i32) -> Result<(),String> { let status_response = object! { "version": { - "name": "1.21.6", + "name": "idk", "protocol" : self.handshake.as_ref().unwrap().protocol_version, }, "players": { "max": 420, "online": 69 }, - "description": self.handshake.as_ref().unwrap().server_address.clone(), + "description": "§c".to_string() + &self.handshake.as_ref().unwrap().server_address.clone() + "§r:§a" + &self.handshake.as_ref().unwrap().server_port.to_string(), }; if packet_id == 0 { let mut response : Vec = vec![]; diff --git a/src/handshake.rs b/src/minecraft/handshake.rs similarity index 95% rename from src/handshake.rs rename to src/minecraft/handshake.rs index e3022df..905f92a 100644 --- a/src/handshake.rs +++ b/src/minecraft/handshake.rs @@ -1,5 +1,5 @@ use std::collections::VecDeque; -use crate::varint::varint_read; +use crate::minecraft::varint::varint_read; pub struct Handshake { pub protocol_version: i32, diff --git a/src/minecraft/mod.rs b/src/minecraft/mod.rs new file mode 100644 index 0000000..990b33c --- /dev/null +++ b/src/minecraft/mod.rs @@ -0,0 +1,7 @@ +pub mod varint; +pub mod handshake; +pub mod client; + +mod socket; + +pub use socket::process_mc_socket; diff --git a/src/minecraft/socket.rs b/src/minecraft/socket.rs new file mode 100644 index 0000000..72370fe --- /dev/null +++ b/src/minecraft/socket.rs @@ -0,0 +1,33 @@ +use std::io::{self, Error}; +use crate::minecraft::client::Client; +use tokio::net::TcpStream; + +pub async fn process_mc_socket(stream: TcpStream) -> io::Result<()> { + let mut buf = vec![0 as u8; 1024]; + + let mut client = Client::create(stream); + + loop { + client.in_stream.readable().await?; + + match client.in_stream.try_read(&mut buf) { + Ok(n) => { + let result = client.buffer_append((&buf[..n]).to_vec()).await; + if let Err(error) = result { + eprintln!("mc error : {}", error); + break; + } + if n == 0 { + break; + } + } + Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { + continue; + } + Err(e) => { + return Err(e.into()); + } + } + } + Ok(()) +} diff --git a/src/varint.rs b/src/minecraft/varint.rs similarity index 100% rename from src/varint.rs rename to src/minecraft/varint.rs diff --git a/src/rpc/actions/mod.rs b/src/rpc/actions/mod.rs new file mode 100644 index 0000000..93fa0d3 --- /dev/null +++ b/src/rpc/actions/mod.rs @@ -0,0 +1,2 @@ +mod ping; +pub use ping::ping; diff --git a/src/rpc/actions/ping.rs b/src/rpc/actions/ping.rs new file mode 100644 index 0000000..8f02d22 --- /dev/null +++ b/src/rpc/actions/ping.rs @@ -0,0 +1,5 @@ +use json::object; + +pub fn ping(data: json::object) -> json::object { + +} diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs new file mode 100644 index 0000000..ed01017 --- /dev/null +++ b/src/rpc/mod.rs @@ -0,0 +1,5 @@ +mod socket; + +pub use socket::process_rpc_socket; + +pub mod actions; diff --git a/src/rpc/socket.rs b/src/rpc/socket.rs new file mode 100644 index 0000000..9c2db11 --- /dev/null +++ b/src/rpc/socket.rs @@ -0,0 +1,22 @@ +use std::io; +use tokio::net::TcpStream; + +use crate::rpc::actions; + +/* + * request_format : + * - "action" : 1 byte + * - length : 4 bytes (no more than 1MB) + * - json object : `length` bytes + * containing the parameters for this specific action (will vary + * depending on the action) + * + * response format : + * - response packet : may be more than one depending on the action + * - length: 4 bytes (no more than 1MB) + * - data: json object containing the response data + */ + +pub async fn process_rpc_socket(stream: TcpStream) -> io::Result<()> { + panic!("not implemented"); +}