VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
sessionManager.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2026 MAK Technologies, Inc.
3  * All rights reserved.
4  ******************************************************************************/
5 
6 #pragma once
7 
8 #include <string>
9 #include <vector>
10 #include <map>
11 #include <mutex>
12 #include <atomic>
13 #include <chrono>
14 
16 enum class DtSessionType
17 {
18  None,
19  Player,
20  Instructor,
21  Anonymous,
22 };
23 
27 {
28  std::string clientName;
29  std::string wsUserName;
30  std::string hostname;
31  std::string connectedAt;
32 
33  uint64_t messagesSent = 0;
34  uint64_t messagesReceived = 0;
35  uint64_t bytesSent = 0;
36  uint64_t bytesReceived = 0;
37 
38  double updateFrequencyHz = 0.0;
39  size_t knownObjectCount = 0;
40 };
41 
49 {
50 public:
52  static DtSessionManager& getInstance();
53 
58  std::string createSession(
59  const std::string& username, const std::vector<std::string>& permissions);
60 
62  bool validateSession(const std::string& sessionKey) const;
63 
66  bool hasPermission(const std::string& sessionKey, const std::string& permission) const;
67 
70  std::string getUsername(const std::string& sessionKey) const;
71 
75  bool removeSession(const std::string& sessionKey);
76 
79  void touchSession(const std::string& sessionKey);
80 
83  bool removeSessionById(int sessionId);
84 
87  void setSessionType(const std::string& sessionKey, DtSessionType type);
88 
91  DtSessionType sessionType(const std::string& sessionKey) const;
92 
95  void assignRole(const std::string& sessionKey, const std::string& roleName);
96 
99  void unassignRole(const std::string& sessionKey, const std::string& roleName);
100 
103  void clearRoles(const std::string& sessionKey);
104 
106  void clearAllRoles();
107 
110  std::vector<std::string> assignedRoles(const std::string& sessionKey) const;
111 
115  uint64_t rolesGeneration(const std::string& sessionKey) const;
116 
122  uint64_t globalRolesGeneration() const;
123 
126  std::string sessionKeyForId(int sessionId) const;
127 
130  int sessionIdForKey(const std::string& sessionKey) const;
131 
134  {
136  std::string username;
137  std::string lastActivityAt;
138  std::string sessionType;
139  std::vector<std::string> assignedRoles;
140 
141  bool hasWebSocket = false;
143  };
144 
147  std::vector<DtSessionInfo> getActiveSessions() const;
148 
152  std::string createAnonymousSession();
153 
155  bool isWebSocketAttached(const std::string& sessionKey) const;
156 
160  bool attachWebSocket(
161  const std::string& sessionKey,
162  const std::string& clientName,
163  const std::string& wsUserName,
164  const std::string& hostname);
165 
169  void detachWebSocket(const std::string& sessionKey);
170 
174  void updateWsStats(
175  const std::string& sessionKey,
176  uint64_t messagesSent,
177  uint64_t messagesReceived,
178  uint64_t bytesSent,
179  uint64_t bytesReceived,
180  double updateFrequencyHz,
181  size_t knownObjectCount);
182 
183 private:
185 
187  {
189  std::string username;
190  std::vector<std::string> permissions;
191  std::chrono::system_clock::time_point lastActivityAt;
193  std::vector<std::string> assignedRoles;
194  uint64_t rolesGeneration = 0;
195 
196  bool wsAttached = false;
198  };
199 
200  mutable std::mutex myMutex;
202  std::map<std::string, DtSessionData> mySessions;
203  std::atomic<uint64_t> myGlobalRolesGeneration{0};
204 };
void clearRoles(const std::string &sessionKey)
Removes all roles from the session identified by sessionKey. Does nothing if the session does not exi...
An ephemeral session created for an unauthenticated WebSocket connection.
std::string lastActivityAt
ISO 8601 UTC timestamp of the most recent API/WS activity.
Definition: sessionManager.h:137
std::vector< std::string > permissions
Definition: sessionManager.h:190
Default — used by status/configure clients.
int myNextSessionId
Definition: sessionManager.h:201
A player session assigned a Role in the simulation.
bool hasWebSocket
True if a WebSocket connection is currently attached.
Definition: sessionManager.h:141
std::mutex myMutex
Definition: sessionManager.h:200
Definition: sessionManager.h:186
size_t knownObjectCount
Definition: sessionManager.h:39
std::string sessionKeyForId(int sessionId) const
Returns the session key for the given integer session ID. Returns an empty string if no session with ...
void clearAllRoles()
Removes all roles from every currently active session.
void setSessionType(const std::string &sessionKey, DtSessionType type)
Sets the session type for the given session key. Does nothing if the session does not exist...
double updateFrequencyHz
Definition: sessionManager.h:38
DtSessionManager — process-wide singleton that creates and tracks login sessions. ...
Definition: sessionManager.h:48
std::chrono::system_clock::time_point lastActivityAt
Definition: sessionManager.h:191
int sessionId
Definition: sessionManager.h:135
void detachWebSocket(const std::string &sessionKey)
Detaches the WebSocket connection from the session and clears WS statistics. If the session is Anonym...
bool validateSession(const std::string &sessionKey) const
Returns true if a session with the given key exists.
std::vector< DtSessionInfo > getActiveSessions() const
Returns a snapshot of all currently active sessions. Session keys are intentionally omitted from the ...
bool hasPermission(const std::string &sessionKey, const std::string &permission) const
Returns true if the session has the given permission. Returns false if the session does not exist or ...
uint64_t rolesGeneration(const std::string &sessionKey) const
Returns the roles-change generation counter for the session. The counter is incremented whenever assi...
std::string sessionType
&quot;None&quot;, &quot;Player&quot;, &quot;Instructor&quot;, or &quot;Anonymous&quot;.
Definition: sessionManager.h:138
std::string getUsername(const std::string &sessionKey) const
Returns the username associated with the session key. Returns an empty string if the session does not...
std::string connectedAt
ISO 8601 UTC timestamp of the WebSocket Connect handshake.
Definition: sessionManager.h:31
void updateWsStats(const std::string &sessionKey, uint64_t messagesSent, uint64_t messagesReceived, uint64_t bytesSent, uint64_t bytesReceived, double updateFrequencyHz, size_t knownObjectCount)
Updates WebSocket statistics for the session and touches its last-activity timestamp. Replaces the direct touchSession() call in the WebSocket read path. Does nothing if the session does not exist or the key is empty.
int sessionId
Definition: sessionManager.h:188
bool wsAttached
True when a WebSocket connection is currently active.
Definition: sessionManager.h:196
std::vector< std::string > assignedRoles
Definition: sessionManager.h:139
std::atomic< uint64_t > myGlobalRolesGeneration
Definition: sessionManager.h:203
DtSessionType
DtSessionType — classification of what role a session plays in the system.
Definition: sessionManager.h:16
std::string wsUserName
From the WebLVC Connect message userName field (informational only).
Definition: sessionManager.h:29
An instructor session; can configure role assignments.
std::string createSession(const std::string &username, const std::vector< std::string > &permissions)
Creates a new session for the given username and returns the session key.
void assignRole(const std::string &sessionKey, const std::string &roleName)
Adds a role name to the session&#39;s assigned roles. Does nothing if the session does not exist or the r...
bool removeSession(const std::string &sessionKey)
Removes the session identified by sessionKey. Does nothing if the key does not exist. Returns true if the session was found and removed, false otherwise.
bool isWebSocketAttached(const std::string &sessionKey) const
Returns true if the session currently has a WebSocket connection attached.
std::string createAnonymousSession()
Creates an ephemeral Anonymous session for an unauthenticated WebSocket connection. The session is automatically removed by detachWebSocket() when the connection closes.
std::vector< std::string > assignedRoles(const std::string &sessionKey) const
Returns the list of role names assigned to the session. Returns an empty vector if the session does n...
DtWsConnectionInfo — snapshot of the WebSocket connection attached to a session. Populated by DtSessi...
Definition: sessionManager.h:26
int sessionIdForKey(const std::string &sessionKey) const
Returns the integer session ID for the given session key. Returns -1 if no session with that key exis...
DtSessionType sessionType(const std::string &sessionKey) const
Returns the type of the session identified by sessionKey. Returns DtSessionType::None if the session ...
Snapshot of a single active session returned by getActiveSessions().
Definition: sessionManager.h:133
void touchSession(const std::string &sessionKey)
Updates the last-activity timestamp for the given session to the current time. Does nothing if the se...
std::vector< std::string > assignedRoles
Definition: sessionManager.h:193
bool attachWebSocket(const std::string &sessionKey, const std::string &clientName, const std::string &wsUserName, const std::string &hostname)
Attaches a WebSocket connection to the session, recording initial connection metadata. Returns false (and does nothing) if the session already has a WebSocket attached, or if the session does not exist.
std::string username
Definition: sessionManager.h:136
DtWsConnectionInfo wsInfo
Valid only when hasWebSocket is true.
Definition: sessionManager.h:142
uint64_t messagesSent
Definition: sessionManager.h:33
uint64_t bytesReceived
Definition: sessionManager.h:36
std::string clientName
From the WebLVC Connect message ClientName field.
Definition: sessionManager.h:28
std::string username
Definition: sessionManager.h:189
uint64_t messagesReceived
Definition: sessionManager.h:34
void unassignRole(const std::string &sessionKey, const std::string &roleName)
Removes a role name from the session&#39;s assigned roles. Does nothing if the session does not exist or ...
std::map< std::string, DtSessionData > mySessions
Definition: sessionManager.h:202
std::string hostname
From the WebLVC Connect message hostname field.
Definition: sessionManager.h:30
DtWsConnectionInfo wsInfo
Valid only when wsAttached is true.
Definition: sessionManager.h:197
bool removeSessionById(int sessionId)
Removes the session identified by its integer session ID. Returns true if the session was found and r...
static DtSessionManager & getInstance()
Returns the process-wide singleton instance.
uint64_t bytesSent
Definition: sessionManager.h:35
uint64_t globalRolesGeneration() const
Returns the process-wide roles-change generation counter. Incremented whenever any session&#39;s role ass...

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)