1
\$\begingroup\$

I have been developing a game lately, my first multiplayer game. I am using Netty and LibGDX.

So previously, before this update I added, I was only queuing incoming packets, I had to do this because I did not want to process packets on the netty thread, and instead process them on the game logic thread. So I had to add the incoming Packet to a queue, and then every game service tick execute them.

Now, I was not queuing outgoing packets server->client, it will just instant write and flush no matter where. On localhost, it worked very well, but once it was live with a few players, my game was having big delays, and mostly disconnects because of timeouts.

After adding an outgoing packet queuing, if absolutely fixed the lag, example:

Player#sendPacket(OutgoingPacket p):

/**
 * Sends a packet
 *
 * @param packet The packet to send
 */
public void sendPacket(OutgoingPacket packet) {
    packetQueue.add(packet);
}

This takes the outgoing packet that was constructed, and adds it to the outgoing packets queue, which is local in every Player object.

And then in my game service loop I have the following:

@Override
public void run() {
    try {
        context.getPackets().queuePackets(); // incoming packets
        context.getTimeMachine().tick();
        context.getPlayerManager().tick();
        context.getPlayerManager().queueOutgoingPackets(); // outgoing packets
    } catch (Exception e) {
        e.printStackTrace();
        ServerContext.log(e.getMessage());
    }
}

And the following method context.getPlayerManager().queueOutgoingPackets():

public void queueOutgoingPackets() {
    for (Player p : players) {
        p.queueOutgoingPackets();
    }
}

Which happens to do this for every player:

public void queueOutgoingPackets() {
    while(!packetQueue.isEmpty()) {
        packetQueue.poll().send(this);
    }
}

And simply send:

public void send(Player p) {
    encode();
    p.getChannel().writeAndFlush(this);
}

Now this fixed my lag issues, completely, I would really like to know, why and how did this affect my networking performance? Is that because write and flush slowing down Netty thread, so when I send packets casually in middle of the game logic?

Now someone told me that I have to limit the outgoing packets queue per tick, per player, in case of a packets spam that comes from a player, think that it makes sense to do so? Should it also be done for incoming packets?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.