Detailed information on server main loop is guided in Reception and Event Callback from Server and
16. Managing Timer loop, RMI, Event from server.
10.1Reception and Event Callback from Server
Game server utilizes the entire CPU and Thread poolingto handle reception of other clients when accessing the database. ProudNet also works in thread pooling method which has following features.
Creates a separate Thread pool at startup of server. Please refer to Proud.CNetServer.Start() to set how many threads need to be created.
Game client takes care of piled messages only when a function is called to handle those messages at a certain time. But server doesn't require such a call.
When an event or a RMI reception occurs at server, they get callback from a thread pool included in Proud.CNetServer.
An event or a RMI reception of the same client can't callback from more than 2 threads since they can't guarantee the order of operation unlike ProudNet does that always operates callback in order of RMI reception arrival. Of course, an event or a RMI calls coming for different clients can be called back simultaneously.
When any event or RMI must be occurred while all threads are operating callback routines, callback waits until any thread becomes available.
Even if the operation time of user-definedd call back routine is long, it doesn't affect the performance of network communication. Thus server developer doesn't have to create a separated thread pool besides what ProudNet offers.
The below picture shows a case where client A,B, and C are included in server, and how they are queued for each RMIs or events that just occurred inside of Proud.CNetServer. A1, A2, and A3 are events or RMIs for client A. In the same manner, B1, B2, and B3 are for client B. There are 2 threads in pool. In the case like this, the operation runs as followed.
A1, A2, and A3 can't be executed all together at the same time. This is same for all events in B and C.
But one each event of As, Bs, and Cs can be executed simultaneously.
Since there are only 2 threads in pool, 2 events from A, B, and C will be chosen to perform callback but in prior to that, the thread with completion of callback routine will operate callback of unselected RMI or event.
10.2Timer callback from server
Just like game client, game server also can operate something at a fixed time. In the case like this, there is a way to have a very simple loop as followed.
while(1) { do_something(); // The world 1000 starts operation Sleep(1); // Wait for a specific time }
There is also a way to handle the loop from a separated thread using Proud.CTimerThread or Proud.CTimerQueue.
However, it is recommended for the thread pool of server directly calls the user-defined timer function. For instance, if server has only 1 thread, you can reduce the number of access to the critical section since the timer function and event callback can be performed in a single thread.