I am making several attempts to make my own simple asynchronous TCP server using boost :: asio after not touching it for several years.
In the last list of examples I can find: http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/tutorial/tutdaytime3/src.html
The problem I ran into in this example is that (I feel) it is cheating and it is cheating large ones by creating tcp_connection shared_ptr so that it does not worry about managing each connection for life. (I think) They do this for short, as this is a small tutorial, but this solution is not the real world.
What if you want to send a message to each client by timer or something like that? A collection of client connections will be needed on any non-trivial real-world server.
I worry about lifelong management of each connection. I suppose it would be natural to keep some collection of tcp_connection objects or pointers for them inside tcp_server. Add to this collection from the OnConnect callback and remove OnDisconnect from this collection.
Note that OnDisconnect will most likely be called from the actual Disconnect method, which in turn will be called from the OnReceive callback or OnSend callback in case of an error.
That is the problem.
We believe that we have a column that looks something like this:
tcp_connection::~tcp_connection tcp_server::OnDisconnect tcp_connection::OnDisconnect tcp_connection::Disconnect tcp_connection::OnReceive
This will lead to errors, since the call stack will disconnect, and we will execute the code in the object whose destructor was called ... I think, right?
I assume that everyone involved in server programming is somehow confronted with this scenario. What is the strategy for handling it?
I hope the explanation is good enough to follow. If you do not let me know, I will create my own list of sources, but it will be very large.
Edit: Linked
) Memory management in asynchronous C ++ code
IMO is not an acceptable answer, it relies on cheating with shared_ptr to receive calls and nothing more, and is not the real world. what if the server wanted to say hello to all clients every 5 minutes. A collection of some kind is needed. What if you call io_service.run for multiple threads?
I also ask the mailing list: http://boost.2283326.n4.nabble.com/How-to-design-proper-release-of-a-boost-asio-socket-or-wrapper-thereof-td4693442.html