wahjam/wahjam — pull request #48 by stefanha (2012-04-19 21:17:19+00:00)
Title: Server polling removal

This patch series builds upon the qt-core series to make wahjamsrv event-driven. This reduces the amount of CPU consumed by the server since it no longer spins in a loop polling for work.

The refactoring required to get us from the polling model to the event-driven model is fairly invasive. There are two main classes of code that need to be refactored:

1. Time-based code works like this in the polling model:

void Class::Run()
{
if (time(NULL) >= deadline) {
...do something...
}
}

This can be converted to QTimer. The timeout is set to the deadline and we set up a slot to handle the QTimer::timeout() signal.

2. Network code works like this in the polling model:

void Class::Run()
{
Net_Message *msg = netcon->Run();

if (!msg) {
if (netcon->GetStatus() != 0) {
status = DISCONNECTED;
}
return;
}

...process msg...
}

We can use the QTcpSocket readyRead() and disconnected() signals to get notified when data is available to be read and when the socket disconnects.

Patch 2 adds a circular array of the last 5 network messages we received. This is useful for both client and server debugging. I plan to leave this in to make our lives easier.