Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?
        updateWorld();
        processInput();
        sendClientMovementToServer();
    }

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
Non-OOP note: receiveData() calls will likely have almost equivalent performance, as they're still calling some equivalent of recv somewhere nearer the metal. –  Anko Mar 9 '13 at 18:29
    
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. –  demicanadian Mar 10 '13 at 9:03
    
@demicanadian I'm curious how the results compare. Let us know. :) –  Cameron Fredman Mar 10 '13 at 9:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.