-1

so im writing a udp server and client for the first time for a 1v1's game. My idea is to have the server handling first connections made and creating a new thread every time 2 new players connect to handle all communication between them. A typical client message would have the threadIndex (i have an array of threads), playerId (which player it came from) and whatever they need to be done.

Is it possible to receive the packet on all threads and analyze if its meant for them? Would this be efficient? How should i approach this?

xred
  • 1
  • 9

1 Answers1

1

The suitable approach depends of nature of server tasks, but creating a new thread for every pair of players is not the best idea probably. Basically lets imagine, that your server mostly performs:

  1. I/O bound tasks. In other words most of time it waits for some I\O opertiton - network respond, query to database or disk operation. In this case you probably need asynchorous model, when all your connections are handled in the same thread. It would be efficient because you actually don't have much to do in your own code. I suppose you more likely have kinda I/O bound tasks. For example you just need to route messages between players and push\pull some data from DB. All routed messages will have an Id of the game(between to plyers), so you will never miss any of them, and they won't be missent. Take a look on this video to see the ideas and goals of asynchronous approach.

  2. CPU bound tasks. Here server must compute something, perform heavy algorithms or process huge amount of data. In this case you probably need multithreading, but again thread per players pair may not be the most suitable approach, because it is not well scaleable and eats too much resourses. If you have some heavy CPU tasks, try to hanlde them in queue with a set of background workers. And then push the messages in asynchronous manner. Take a look on producer-consumer implementation with BlockingCollection.

You may have a combination of two cases, and of cource you can combine the approaches above. Also see questions 1, 2, 3. Try and return with specific questions. Hope it helps.

KozhevnikovDmitry
  • 1,660
  • 12
  • 27