I''m working on a networking library that uses Winsock (but not the Asynchronous WSA calls). I''m stuck on a design decision to determine how an application will receive notification of events happening on connections.
My choices are callbacks or events.
Callbacks: the application will register a callback with the network engine, specifying the address of a function that will be executed when the specified event takes place. For example, a server might call:
hbNetEngine::getInstance()->setCallback(this,listen,
(networkCallback)&testServer::onClientAccept, NETWORK_ACCEPT_CALLBACK);
to register a callback for when a connection to a client is accepted/established. Then when the connection is established, the following might get executed:
hbNetEngine::getInstance()->executeCallback(this, NETWORK_ACCEPT_CALLBACK);
Alternatively, I could use events. Say a connection is accepted with a client, the library would then create a network event and push it onto the event queue. Then inside the application event loop, it extracts each event from the queue and performs the appropriate action. The event loop is similar to the Windows message loop but it''s my own version.
I prefer the idea of callbacks, but I''m worried about interrupting program flow when executing callbacks.
Any ideas/suggestions?
Thanks