Search code examples
tcpnetwork-programmingreal-timeasyncsocketsfml

Nagle-Like Problem


so I have this real-time game, with a C++ sever with disabled nagle using SFML library , and client using asyncsocket, also disables nagle. I'm sending 30 packets every 1 second. There is no problem sending from the client to the server, but when sending from the server to the clients, some of the packets are migrating. For example, if I'm sending "a" and "b" in completly different packets, the client reads it as "ab". It's happens just once a time, but it makes a real problem in the game.

So what should I do? How can I solve that? Maybe it's something in the server? Maybe OS settings?

To be clear: I AM NOT using nagle but I still have this problem. I disabled in both client and server.


Solution

  • You have to disable Nagle in both peers. You might want to find a different protocol that's record-based such as SCTP.

    EDIT2

    Since you are asking for a protocol here's how I would do it:

    • Define a header for the message. Let's say I would pick a 32 bits header.

      Header:
          MSG Length: 16b
          Version: 8b
          Type: 8b
      
    • Then the real message comes in, having MSG Length bytes.

    So now that I have a format, how would I handle things ?

    Server

    When I write a message, I prepend the control information (the length is the most important, really) and send the whole thing. Having NODELAY enabled or not makes no difference.

    Client

    I continuously receive stuff from the server, right ? So I have to do some sort of read.

    • Read bytes from the server. Any amount can arrive. Keep reading until you've got at least 4 bytes.
    • Once you have these 4 bytes, interpret them as the header and extract the MSG Length
    • Keep reading until you've got at least MSG Length bytes. Now you've got your message and can process it

    This works regardless of TCP options (such as NODELAY), MTU restrictions, etc.