Search code examples
c++socketsiocp

Completion key vs extending the OVERLAPPED structure


I am not sure what approach I should take when dealing with data associated with each socket. Should I use the completion key or should I extend the OVERLAPPED structure.

Extending the OVERLAPPED structure seems like a hack, so does it offer any advantages over the completion key?


Solution

  • The completion key is "per connection" data, that is it is the same on every completion for all operations on a given handle and is a good way to link to a "file" or "socket" structure.

    The Overlapped is "per operation" data, each concurrent operation on a handle MUST use a unique overlapped structure. This makes it ideal to hold per operation data such as "type of operation" and associated memory (such as buffers), etc.

    The "extended overlapped" isn't a hack, it's the way the design is supposed to be used.