There is ConnectionManager which waits for incoming connections. For each incoming connection it creates a Connection instance which handles the inbound and outbound traffic on this connection. Each Connection has a Watchdog which handles ‘bad connection’ conditions and calls registered ‘Listerners’. One ‘Listener’ is the ConnectionManager which closes the connection and deletes the Connection instance which in turn deletes the corresponding Watchdog.
Wait. A. Minute.
The Watchdog calls the ConnectionManager which deletes the Connection which deletes the Watchdog? The Watchdog chases its own tail.
I am completly blocked. How do I resolve this?
Solution: I will make the Listener thingy asynchronous, altough I don’t know yet how to do that without too much pain. The Watchdog doesn’t know about the ConnectionManager. It is fairly generic. Also the Win32-Thread-API doesn’t have something like ‘join’, so I might need to roll my own with GetExitCodeThread() and STILL_ACTIVE…
Thanks, guys.
If the watchdog is running in a different thread, then the problem isn’t too bad – the watchdog signals the ConnectionManager to delete with a asynchronous message, then exits it’s own thread.
Mean-while, the ConnectionManager thread gets the delete message, and starts deleting the watchdog.
To avoid race-conditions the watchdog destructor should join the watchdog thread, and cleanup the thread. (Probably also signal the watchdog thread, or assert() something about the watchdog thread being ready to exit).