Class DEngine



The DEngine class performs event-handling for other classes. Each class that uses the event system takes a DEngine in its constructor. When the DEngine's update function is called, it dispatches any events belonging to those classes. The events are dispatched one at a time, and won't interrupt running code. (Unless update is called in a separate thread!) We can respond to events without needing to worry about cross-thread synchronization. The resolution of the events' timing depends on how often update is called, for example calling update 10 times per second gives a resolution of 100 milliseconds.


DEngine(void)

Default constructor, creates a new DEngine.


static EndianType getEndianness(void)

Returns the computer's byte-order, which is one of these values from the EndianType enumeration: DA_UNKNOWN, DA_BIG_ENDIAN, or DA_LITTLE_ENDIAN.


string getErrMsg(void)

Returns a description of the most recent error.


boost::asio::io_service* getIoService(void)

Returns the Boost io_service used for networking events, which is mainly useful if you want to use the Boost library directly.


static void sleep(long int milliseconds)

Makes the calling thread sleep for the specified number of milliseconds.


int update(void)

Should be called periodically to dispatch events. Update processes at most 1000 networking events and 1000 user-defined events per invocation. Since new events can be created while update is running, this prevents update from blocking indefinitely if events are created faster than update can process them.

Update returns the number of events that were dispatched. It returns -1 if errors were encountered, and sets errMsg.

A standard event loop looks something like this:

while (runProgram)
{
    eventsRun = netEngine.update();
    if (eventsRun < 0)
    {
        cout << netEngine.getErrMsg();
        runProgram = false;
    }
    else if (eventsRun == 0)
        DEngine::sleep(1);
}

If no events were processed there is no work to do, and the loop sleeps for 1 millisecond before checking for new work. This lets us avoid looping endlessly and consuming 100% of the CPU. Under Windows, you may find that the sleep function waits for much longer than 1 millisecond, which reduces the responsiveness of the event loop. The program can be scheduled to run more frequently by adjusting its scheduling priority - search for "Scheduling Priorities" on msdn.microsoft.com for more information.

Applications that need to process a very large number of events, on the order of 100,000 or more per second, may make multiple calls to update without sleeping in between. The program would only sleep if update returned 0 events processed.





















Back to class reference