1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//!
//! The main MuOxi Game server. This is where all the game
//! logic exists and interaction to this server will be on
//! port: 4567
//!
use std::str;
use tokio;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;

use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let game_server_addr = "127.0.0.1:4567".to_string();

    let mut game_server = TcpListener::bind(&game_server_addr).await?;

    println!("MuOxi Game Engine listening on: {}", game_server_addr);

    loop {
        let (mut socket, _) = game_server.accept().await?;

        let process = async move {
            let mut buf = [0; 1024];

            loop {
                let n = socket
                    .read(&mut buf)
                    .await
                    .expect("Failed to read from socket");
                if n == 0 {
                    return;
                }
                let msg = str::from_utf8(&buf[0..n]);

                if let Ok(msg) = msg {
                    let resp = format!("Game Server > {}", msg);
                    socket
                        .write_all(resp.as_bytes())
                        .await
                        .expect("Failed to write data to socket");
                } else {
                    println!("Unable to decode UTF-8 string.");
                }
            }
        };

        tokio::spawn(process);
    }
}