I am coding (and designing at once) basic asynchronous server with boost::asio. Reading trough examples, I've noticed they are using std::enable_shared_from_this along with boost::shared_ptr to destroy the session object as soon as socket gets disconnected - lack of next asynchronous operation async_read_some() results in shared_from_this() not being called, which leads to session object go out of scope. Everything works just fine, but...
The problem arises when i want to somehow manage the entire collection of all active connections. By calling make_shared<session> as in the examples, the object "lives its own life", getting destroyed as soon as it goes out of scope. But if i put it into some container, this rule no longer applies - shared ptr would still exist in the container, having use count of at least one, making it necessary to explicitly notify the holder to free the session.
In http example http://ift.tt/1S8EPbu (connection and connection_manager) connection_manager class is forward declared in connection.hpp. Class connection_manager has a collection of connection, while connection can call "stop" member funcion of connection_manager - and thats a cycle in dependencies which I am very scared of.
The only thing that comes to my mind is to make the holder implement an interface which has method socket_closed() and pass *this along with socket when constructing connection, so connection could notify the holder back. Is there any clean solution for the problem?
Aucun commentaire:
Enregistrer un commentaire