I've been doing a lot of work with IO completion ports lately and I have to say it's definitely a love hate relationship. The code can be really hard to debug unless you have good fundamentals in writing multithreaded code.
One way i've been using IOCPs gets extra sticky because i'm using them to manage the two ends of a proxy. There is a socket going from application->me and a socket going from me->remote server. Tracking the state of the two sockets and keeping them in lockstep so that they behave as if they were a single socket and don't introduce weirdness in the client apps can get tough.
Anyone else using IOCPs in this way? i'd love to chat about techniques. A couple key takeaways (some from a great engineer who I work with)
- Organize your code as a state machine (it will pay off in the long run) if you can track your state and detect when you run into a situation or state that you hadn't anticipated.
- Comment your code. It's not simple code, and so you should explain the scenarios you cover in your code so that the next guy who picks it up can understand it and get moving quickly. Some code is self-explanatory, but this stuff is not.