mardi 30 mai 2017

What is the right way to track Boost ASIO sessions?

sI have a server application that is based on the implementation shown here. This implementation creates a new session as a shared_ptr. As long as the client is connected, the session remains alive. When the client disconnects, a session object is destroyed. This is very convenient, but...

The problem: I would like to be able to track sessions, for two reasons:

  1. To track connected users, and group sessions from the same user
  2. To be able to shutdown all sessions and exit gracefully, when needed.

How can I do that?

Ideally, I'd like to be able to iterate over all sessions that are alive.

Failed/horrible solutions from my side

  • Add every session's shared_ptr to some container, right before new_session->start()

The problem with this is that the session is then never destroyed, not even when the user disconnects... of course, this is because an instance of shared_ptr is still alive in the container, which means I have to periodically check that the number of shared_ptr count == 1, which will complicate the design and create horrible thread-safety complications.

  • Create a container that has normal pointers of all sessions in the server instance, in addition to a mutex, and pass them both to each session, and make the destructor of the session remove itself from the container

This solution would work (I'm guessing here), but it's just very complicated that it feels ugly, and I wanted some advice before proceeding with this mess. I'm sure there's someone smarter than me out there to figure a smarter solution, or make me realize that boost ASIO has a solution for this already.

Aucun commentaire:

Enregistrer un commentaire