b23r0 / rust-raknet

RakNet Protocol implementation by Rust.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

EvilCodeZ opened this issue · comments

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;
        });
    }