I'm planning on how my multiplayer game client game loop should work. While I'm convinced that server should be moved from game loop into it's own thread, I'm not sure what should I do about receiving server data. To me most logical approach would be something like this:

protected override void Update(GameTime gameTime)
        receiveServerData(); //may receiving data lag the game?

private void receiveServerData()
        NetIncomingMessage msg;
        while ((msg = netClient.ReadMessage()) != null)
            // ...

But I'm afraid that reading incoming data inside game loop can lag the game if processing too much data, so here's my question: Is it good idea to process incoming server data inside game loop, or should create thread for receiving this data instead? And if so, how should I do it?


1 Answer 1


I think you're correct to be concerned about this approach. You should think of the data you're receiving from the server as coming in at different speeds, at different times, and sometimes not at all, so you really want the handling of data from the server to happen asynchronously from your game update.

I recommend you look into the observer design pattern. The idea is that rather than call a receiveServerData() (regardless of whether data is coming in), you want to create a network listener that reacts when server data arrives.

enter image description here

  • \$\begingroup\$ Non-OOP note: receiveData() calls will likely have almost equivalent performance, as they're still calling some equivalent of recv somewhere nearer the metal. \$\endgroup\$
    – Anko
    Mar 9, 2013 at 18:29
  • \$\begingroup\$ Ok, found out how to make it your way Cameron, and it shouldn't be much work switching it from my way, so I'll test it for performance change on the way. \$\endgroup\$ Mar 10, 2013 at 9:03
  • \$\begingroup\$ @demicanadian I'm curious how the results compare. Let us know. :) \$\endgroup\$ Mar 10, 2013 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .