3

I'm working on my first big C++ project and while I understand the language I'm trying to learn elegant design patterns using it. I have some low-level networking over a UDP sockets API where each message contains an incremented id to facilitate association of requests and responses. I'd like to use these to create a higher level async API, where I can send a request with a callback that gets fired upon response, much like how elegant high-level networking works in Python or JS. Any recommendations for implementing this? I thought about having a simple buffer of all the callbacks, but this would need to be pretty large if there's a lot of traffic. I also need some way of dealing with responses that never arrive after some timeout. Suggestions? I have all of Boost and C++ 11 at my disposal.

1

Your buffer of all callbacks is basically unavoidable, I'd not be much concerned about it. The required size will depend on the expected number of outstanding requests at any time, not on the total traffic, so it is very likely bounded. Depending on the reliability requirements, you could implement a growable buffer, but as you're dealing with an unreliable protocol anyway this might even be unnecessary.

Handling timeouts is no real issue at your networking level, your API users just need to provide a timeout value and a (possibly NULL) timeout callback, and then you just need to loop through the callback buffer at regular intervals (granularity depends on typical timeout values, you don't need to set a timer for each individual request) and call the timeout callbacks of old requests, removing them from the buffer just as you do when you receive return messages. The choice of suitable timeout times is the API user's responsibility and probably depends on the characteristics of the devices being addressed.

The buffer is a shared resource used by multiple processes, so you need to protect access using appropriate synchronisation primitives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.