diff --git a/src/Session/Session.cc b/src/Session/Session.cc index fc3a38dde..27f15af21 100644 --- a/src/Session/Session.cc +++ b/src/Session/Session.cc @@ -369,6 +369,7 @@ void Session::OnRegisterViewer(const CARTA::RegisterViewer& message, uint16_t ic } else { type = CARTA::SessionType::RESUMED; if (session_id != _id) { + spdlog::info("({}) Session setting id to {} (was {}) on resume", fmt::ptr(this), session_id, _id); _id = session_id; spdlog::info("({}) Session setting id to {}", fmt::ptr(this), session_id); status = fmt::format("Start a new backend and assign it with session id {}", session_id); diff --git a/src/Session/Session.h b/src/Session/Session.h index 255906cc7..056821bd6 100644 --- a/src/Session/Session.h +++ b/src/Session/Session.h @@ -288,9 +288,6 @@ class Session { void SendFileEvent( int file_id, CARTA::EventType event_type, u_int32_t event_id, google::protobuf::MessageLite& message, bool compress = true); void SendLogEvent(const std::string& message, std::vector tags, CARTA::ErrorSeverity severity); - void StartAnimationThread() { - // Not sure if needed... XXX - } // uWebSockets uWS::WebSocket* _socket; diff --git a/src/Session/SessionManager.cc b/src/Session/SessionManager.cc index c7a6c8678..e930d5c8c 100644 --- a/src/Session/SessionManager.cc +++ b/src/Session/SessionManager.cc @@ -5,10 +5,9 @@ */ #include "SessionManager.h" -#include "ThreadingManager/ThreadingManager.h" - #include "Logger/Logger.h" #include "OnMessageTask.h" +#include "ThreadingManager/ThreadingManager.h" #include "Util/Message.h" #include "Util/Token.h" @@ -17,7 +16,7 @@ namespace carta { SessionManager::SessionManager(ProgramSettings& settings, std::string auth_token, std::shared_ptr file_list_handler) : _session_number(0), _app(uWS::App()), _settings(settings), _auth_token(auth_token), _file_list_handler(file_list_handler) {} -void SessionManager::DeleteSession(int session_id) { +void SessionManager::DeleteSession(uint32_t session_id) { Session* session = _sessions[session_id]; if (session) { spdlog::info( @@ -57,9 +56,11 @@ void SessionManager::OnUpgrade( return; } - _session_number++; - // protect against overflow - _session_number = max(_session_number, 1u); + auto now = std::chrono::system_clock::now(); + auto now_ms = std::chrono::time_point_cast(now); + auto epoch = now_ms.time_since_epoch(); + auto value = std::chrono::duration_cast(epoch); + _session_number = value.count(); http_response->template upgrade({_session_number, address}, // http_request->getHeader("sec-websocket-key"), // @@ -103,8 +104,10 @@ void SessionManager::OnDisconnect(WSType* ws, int code, std::string_view message uint32_t session_id = static_cast(ws->getUserData())->session_id; // Delete the Session - _sessions[session_id]->DecreaseRefCount(); - DeleteSession(session_id); + if (_sessions.count(session_id) > 0) { + _sessions[session_id]->DecreaseRefCount(); + DeleteSession(session_id); + } // Close the websockets ws->close(); diff --git a/src/Session/SessionManager.h b/src/Session/SessionManager.h index 3de158a59..fdc33f674 100644 --- a/src/Session/SessionManager.h +++ b/src/Session/SessionManager.h @@ -20,7 +20,7 @@ class SessionManager { public: using WSType = uWS::WebSocket; SessionManager(ProgramSettings& settings, std::string auth_token, std::shared_ptr); - void DeleteSession(int session_id); + void DeleteSession(uint32_t session_id); void OnUpgrade(uWS::HttpResponse* http_response, uWS::HttpRequest* http_request, struct us_socket_context_t* context); // Called on connection. Creates session objects and assigns UUID to it void OnConnect(WSType* ws);