I love working with Network things specially for game development , for a long time i had played a pretty old MMORPG game that was released in 2003.
I found the old Game client somewhere on the internet and i also found a working server emulator for that game written in C# but it’s modern and using Asynchronous programming Which i loved it.
But as i said i’m trying to improve my skills in Rust and also in Asynchronous programming.
So after some time i got how the game server works and how the communication between Server and client is done.
The game server is sending and receiving packets over a tcp connection, and it uses a structured data with prefixed length of type
The structure is really simple
Packet Length: u16, Packet ID: u16, Packet Body: Vec<u8>,
And of course the length of the Packet body is equal to packet length - 4 Bytes.
And as all of us knows that tcp is a strem protocol and it doesn’t have mechanism to handle Packet fragments (slices) so you need to implement that at the application layer.
The way that provided in C# is that after receiving the connection it creates an Async Task (aka: Future) and in that task it reads the packet length form the buffer (stream) and then get a slice of that buffer equal to that length and it makes a new task to process that packet and sends the result (response) back To the socket (game client).
That way it makes sure that it process every segment individually without overlapping.
I’m trying to figure out how can i implement such a thing in Rust.
tokio as a Runtime also I’m using
bytesorder crate to deal with Bytes.
I already have read the docs of tokio-rs and also the chat example seems interesting…
If someone could guide me with a simple example to make a simple tcp server that receiving a
prefixed length messages and process each message individually in an Asynchronous way that will be great and very helpful.
I have to mention that it’s just for learning purpose.