85 lines
2.7 KiB
C
85 lines
2.7 KiB
C
|
|
/*
|
||
|
|
* server/session_registry.h — In-memory session, channel, and user registry.
|
||
|
|
*
|
||
|
|
* Tracks all authenticated sessions, the channel tree, and user<→>channel assignments.
|
||
|
|
* Protected by a shared_mutex (many readers, few writers). All methods are thread-safe.
|
||
|
|
*/
|
||
|
|
#ifndef VOICECAT_SERVER_SESSION_REGISTRY_H
|
||
|
|
#define VOICECAT_SERVER_SESSION_REGISTRY_H
|
||
|
|
|
||
|
|
#ifdef VOICECAT_HAS_NET
|
||
|
|
|
||
|
|
#include <cstdint>
|
||
|
|
#include <memory>
|
||
|
|
#include <shared_mutex>
|
||
|
|
#include <string>
|
||
|
|
#include <unordered_map>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
#include "proto/voicecat.pb.h"
|
||
|
|
|
||
|
|
namespace voicecat::server {
|
||
|
|
|
||
|
|
class ConnSession;
|
||
|
|
|
||
|
|
struct ChannelEntry {
|
||
|
|
voicecat::v1::Channel proto;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct UserEntry {
|
||
|
|
voicecat::v1::User proto;
|
||
|
|
uint64_t session_id{};
|
||
|
|
};
|
||
|
|
|
||
|
|
class SessionRegistry {
|
||
|
|
public:
|
||
|
|
SessionRegistry() = default;
|
||
|
|
|
||
|
|
// Create the default "Lobby" channel (id=1, permanent). Call once at startup.
|
||
|
|
void init_default_channels();
|
||
|
|
|
||
|
|
// Register a session (before auth). Returns the assigned session_id.
|
||
|
|
uint64_t register_session(std::weak_ptr<ConnSession> session);
|
||
|
|
|
||
|
|
// Remove a session (called on disconnect).
|
||
|
|
void unregister_session(uint64_t session_id);
|
||
|
|
|
||
|
|
// Add a user once authenticated. Returns the assigned user_id.
|
||
|
|
uint32_t add_user(uint64_t session_id, const voicecat::v1::User& user);
|
||
|
|
|
||
|
|
// Remove a user (called on disconnect after auth).
|
||
|
|
void remove_user(uint32_t user_id);
|
||
|
|
|
||
|
|
// Move a user to a channel. Returns false if channel doesn't exist.
|
||
|
|
bool set_user_channel(uint32_t user_id, uint32_t channel_id);
|
||
|
|
|
||
|
|
// Snapshot for ServerStateSnapshot message.
|
||
|
|
std::vector<voicecat::v1::Channel> channel_snapshot() const;
|
||
|
|
std::vector<voicecat::v1::User> user_snapshot() const;
|
||
|
|
|
||
|
|
// Resolve target sessions for a text message relay.
|
||
|
|
// TEXT_CHANNEL: all users in that channel (except sender's session).
|
||
|
|
// TEXT_PRIVATE: the session for that user_id.
|
||
|
|
std::vector<std::shared_ptr<ConnSession>> resolve_text_targets(
|
||
|
|
uint64_t sender_session_id, voicecat::v1::TextScope scope, uint32_t target_id) const;
|
||
|
|
|
||
|
|
// Broadcast an envelope to all sessions except the excluded one.
|
||
|
|
void broadcast(const voicecat::v1::Envelope& env, uint64_t exclude_session_id = 0) const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
mutable std::shared_mutex mu_;
|
||
|
|
|
||
|
|
uint64_t next_session_id_{1};
|
||
|
|
uint32_t next_user_id_{1};
|
||
|
|
uint32_t next_channel_id_{2}; // 1 is reserved for Lobby
|
||
|
|
|
||
|
|
std::unordered_map<uint64_t, std::weak_ptr<ConnSession>> sessions_;
|
||
|
|
std::unordered_map<uint32_t, UserEntry> users_;
|
||
|
|
std::unordered_map<uint32_t, ChannelEntry> channels_;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace voicecat::server
|
||
|
|
|
||
|
|
#endif // VOICECAT_HAS_NET
|
||
|
|
#endif // VOICECAT_SERVER_SESSION_REGISTRY_H
|