Jump to content
  • Advertisement
Sign in to follow this  
Fire Lancer

Multithreading, creating a blocking message system

This topic is 3471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to create a thread that handels all the networking io in my game. However I dont want it running in a loop polling 20 diffrent things to see if anything has happened. Instead I want it to wait for something to happen, like the windows GetMessage method does. Eg something along these lines:
bool run = true;
while(run)
{
    unsigned evt;
    unsigned paramA, paramB;
    WaitForSomethingToHappen(&evt, &paramA, &paramB);
    switch(evt)
    {
    case EVT_SHUTDOWN://game shutting down
        run = false;
        break;
    case EVT_SEND://game wants to send data
        ((Connection*)paramA)->Send((Packet*)ParamB);
        break;
    case EVT_RECVREADY://winsock has data ready to be read on this sonnections socket
        ((Connection*)paramA)->Recv();
        break;
    }
    //clear up any connections etc
}



Then an event would be sent to this thread when somethign happens so it can handle it, like the PostMessage for windows. The real problem is I dont know how to intigrate this with winsock (can I give it a function pointer to call when things happen or somthing like that?) I know I can use select() with winsock to check if any of the connections have data ready to be read, but then if it's blocking in select I cant handle the game shutting down (EVT_SHUTDOWN), or the game trying to send data...so I'm backing to polling select and my own stuff every few ms in a loop and its taking loads of CPU cycles even with very low trafic :(

Share this post


Link to post
Share on other sites
Advertisement
So I'll have this to hand;e the starting and stopping of the thread?

HANDLE NetEvent = CreateEvent(0,0,FALSE,NULL);
...
bool run=true;
while(true)
{
WaitForSingleObject(NetEvent);
ResetEvent(NetEvent);
//do somthing...
}
...
//somewhere in the game update under a diffrent thread
SetEvent(NetEvent);




So ok few things I dont get:
-How do I get winsock to go along with this with the incoming data?
-How do I pass the event data? Should I create a global for my evt, paramA and paramB from before or is there a standardized way to do that?
-How do I handle the case where multiple things try to do something at once, surely I need some sort of queue system, should I roll my own just treating the wait/set/reset event stuff as a signal for "queue has waiting item"?


EDIT: I just went over all the winsock2 functions and couldnt see one to tell it to do somthing happens (ie call SetEvent(myEvt))...

Share this post


Link to post
Share on other sites
Quote:
Original post by Fire Lancer

I know I can use select() with winsock to check if any of the connections have data ready to be read, but then if it's blocking in select I cant handle the game shutting down (EVT_SHUTDOWN), or the game trying to send data...so I'm backing to polling select and my own stuff every few ms in a loop and its taking loads of CPU cycles even with very low trafic :(


Select supports timeout parameter, and allows use of sockets in a completely non-blocking manner.

select() for 5 ms. If something happens, select will unblock immediately. If nothing happens, it will unblock after 5 ms. Timeout of 0 would be used on client, where one doesn't want to wait.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!