b23r0 / rust-raknet

RakNet Protocol implementation by Rust.
MIT License
218 stars 18 forks source link

RaknetListener#accept() freezes for ever and don't accept any new connections. #13

Open EvilCodeZ opened 1 year ago

EvilCodeZ commented 1 year ago

I've made normal client server application that uses rust-raknet, but once the first client is accepted by the RaknetListener, the listener stop accepting new connections.

So I made a test program that binds a RaknetListener and accepts clients in a loop and reads data from them and prints it. And i've added loop that connects clients to the listener. This test program also stop accepting new connections after few clients that got accepted. Code below:

let mut server = rust_raknet::RaknetListener::bind(&"0.0.0.0:1337".parse().unwrap()).await.unwrap();
    server.listen().await;
    log::info!("Listening on {}", server.local_addr().unwrap());

    tokio::spawn(async move {
        loop {
            log::info!("Accepting...");
            let client = server.accept().await.unwrap();
            log::info!("Accepted: {}", client.peer_addr().unwrap());
            tokio::spawn(async move {
                loop {
                    let msg = client.recv().await;
                    log::info!("Received: {:?}", msg);
                    if msg.is_err() {
                        break; // Stop the task
                    }
                }
            });
        }
    });

    tokio::time::sleep(std::time::Duration::from_secs(2)).await; // Wait some time for the server to start accepting connections

    loop {
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        tokio::spawn(async move {
            log::info!("Connecting new client...");
            let client = rust_raknet::RaknetSocket::connect(&"127.0.0.1:1337".parse().unwrap()).await.unwrap();
            log::info!("Connected to {}", client.peer_addr().unwrap());
            client.send(&[0xFE, 0x02, 0x03, 0x04], rust_raknet::Reliability::ReliableOrdered).await.unwrap();
            let _ = client.close().await;
        });
    }