Files

88 lines
2.1 KiB
C++

#pragma once
#include <chrono>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <unordered_map>
namespace Echoion::Core
{
// Session management for RPC clients
// Supports 100+ concurrent authenticated sessions
class SessionManager
{
public:
struct Session {
uint64_t clientId;
std::string authToken;
std::chrono::steady_clock::time_point lastActivity;
std::chrono::steady_clock::time_point createdAt;
bool isAuthenticated;
// Optional: store client-specific state
struct ClientState {
std::optional<std::string> currentPlaylist;
float preferredVolume = -1.0f; // -1 = use default
// Add more client preferences here
};
std::optional<ClientState> clientState;
};
struct Config {
size_t maxSessions = 150; // 100 users + buffer
std::chrono::seconds sessionTimeout{3600}; // 1 hour timeout
std::chrono::seconds cleanupInterval{
300}; // Cleanup every 5 min
};
SessionManager();
explicit SessionManager(const Config& config);
// Initialize session manager
bool initialize();
void shutdown();
// Create new session
std::string createSession(uint64_t clientId);
// Authenticate session
bool authenticateSession(const std::string& token);
// Validate and update session
bool validateSession(const std::string& token);
void updateActivity(const std::string& token);
// Get session info
std::optional<Session> getSession(const std::string& token) const;
// End session
void endSession(const std::string& token);
void endSessionByClientId(uint64_t clientId);
// Get all active sessions
std::vector<Session> getAllSessions() const;
// Get session count
size_t getSessionCount() const;
// Cleanup expired sessions (call periodically)
size_t cleanupExpiredSessions();
// Check if at capacity
bool isAtCapacity() const;
private:
std::string generateToken();
Config config_;
std::unordered_map<std::string, Session> sessions_;
mutable std::mutex mutex_;
std::atomic<bool> running_{false};
};
} // namespace Echoion::Core