177 lines
6.5 KiB
C++
177 lines
6.5 KiB
C++
/* Thread-safe in-memory session, channel, user, and media registry. */
|
|
#ifndef VOICECAT_SERVER_SESSION_REGISTRY_H
|
|
#define VOICECAT_SERVER_SESSION_REGISTRY_H
|
|
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <shared_mutex>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
#define ASIO_STANDALONE 1
|
|
#include <asio.hpp>
|
|
|
|
#include "db.h"
|
|
#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{};
|
|
};
|
|
|
|
// Hashes asio::ip::udp::endpoint by "addr:port" string.
|
|
struct UdpEndpointHash {
|
|
size_t operator()(const asio::ip::udp::endpoint& ep) const {
|
|
std::string key = ep.address().to_string() + ':' + std::to_string(ep.port());
|
|
return std::hash<std::string>{}(key);
|
|
}
|
|
};
|
|
|
|
class SessionRegistry {
|
|
public:
|
|
explicit SessionRegistry(std::shared_ptr<Database> db);
|
|
|
|
void load_channels();
|
|
|
|
uint64_t register_session(std::weak_ptr<ConnSession> session);
|
|
|
|
void unregister_session(uint64_t session_id);
|
|
|
|
uint32_t add_user(uint64_t session_id, const voicecat::v1::User& user);
|
|
|
|
void remove_user(uint32_t user_id);
|
|
|
|
// Broadcast a UserEvent::LEFT for a user to all other sessions. Called by
|
|
// ConnSession::close() before remove_user() so remaining clients learn about an
|
|
// ungraceful disconnect (TCP drop, crash, network loss). Mirrors the first half of
|
|
// kick_user(). Takes the shared lock internally; safe to call from ConnSession::close.
|
|
void broadcast_left(uint32_t user_id, const std::string& reason);
|
|
|
|
bool set_user_channel(uint32_t user_id, uint32_t channel_id);
|
|
|
|
void set_user_voice_subscribed(uint32_t user_id, bool subscribed);
|
|
|
|
std::vector<voicecat::v1::Channel> channel_snapshot() const;
|
|
std::vector<voicecat::v1::User> user_snapshot() const;
|
|
std::optional<voicecat::v1::User> user_snapshot_user(uint32_t user_id) const;
|
|
std::optional<std::string> user_nickname(uint32_t user_id) const;
|
|
|
|
std::vector<std::shared_ptr<ConnSession>> resolve_text_targets(
|
|
uint64_t sender_session_id, voicecat::v1::TextScope scope, uint32_t target_id) const;
|
|
|
|
void broadcast(const voicecat::v1::Envelope& env, uint64_t exclude_session_id = 0) const;
|
|
|
|
// The caller closes returned sessions outside the registry lock because close re-enters it.
|
|
std::vector<std::shared_ptr<ConnSession>> find_stale_sessions(int64_t max_age_ms) const;
|
|
|
|
private:
|
|
void broadcast_unlocked(const voicecat::v1::Envelope& env,
|
|
uint64_t exclude_session_id = 0) const;
|
|
|
|
public:
|
|
void set_session_permissions(uint64_t session_id,
|
|
const voicecat::v1::Permissions& perms);
|
|
std::optional<voicecat::v1::Permissions> get_session_permissions(
|
|
uint64_t session_id) const;
|
|
|
|
std::shared_ptr<ConnSession> find_session_by_user_id(uint32_t user_id) const;
|
|
|
|
bool kick_user(uint32_t user_id, const std::string& reason);
|
|
|
|
bool ban_user(uint32_t user_id, const std::string& reason, int64_t expires_at);
|
|
|
|
bool set_server_mute(uint32_t user_id, bool muted, bool deafened);
|
|
|
|
bool move_user(uint32_t user_id, uint32_t channel_id);
|
|
|
|
uint32_t create_channel(const voicecat::v1::Channel& ch, const std::string& password,
|
|
std::string& error);
|
|
|
|
bool update_channel(const voicecat::v1::Channel& ch, const std::string& password,
|
|
std::string& error);
|
|
|
|
// Deleting a channel moves its users to Lobby.
|
|
bool delete_channel(uint32_t channel_id, std::string& error);
|
|
|
|
std::optional<voicecat::v1::Channel> get_channel(uint32_t channel_id) const;
|
|
|
|
bool check_channel_password(uint32_t channel_id, const std::string& password) const;
|
|
|
|
void register_udp_token(const std::array<uint8_t, 16>& token, uint64_t session_id);
|
|
|
|
std::shared_ptr<ConnSession> find_by_udp_token(const std::array<uint8_t, 16>& token) const;
|
|
|
|
void register_udp_endpoint(asio::ip::udp::endpoint ep, uint64_t session_id);
|
|
|
|
std::shared_ptr<ConnSession> find_by_udp_endpoint(const asio::ip::udp::endpoint& ep) const;
|
|
|
|
uint32_t assign_ssrc(uint64_t session_id);
|
|
|
|
// Add/replace a stream entry on a user (called when StreamAnnounce succeeds).
|
|
// Returns the updated User proto for broadcasting, or nullopt if user not found.
|
|
std::optional<voicecat::v1::User> set_user_stream(uint32_t user_id,
|
|
const voicecat::v1::StreamInfo& info);
|
|
|
|
// Remove a stream entry from a user (called on StreamStop). Returns the updated
|
|
// User proto for broadcasting, or nullopt if user not found.
|
|
std::optional<voicecat::v1::User> clear_user_stream(uint32_t user_id, uint32_t stream_id);
|
|
|
|
std::vector<std::shared_ptr<ConnSession>> find_channel_sessions(
|
|
uint32_t channel_id, uint64_t exclude_session_id = 0) const;
|
|
|
|
uint32_t user_channel(uint32_t user_id) const;
|
|
|
|
// Returns the authoritative per-channel Opus configuration.
|
|
std::optional<voicecat::v1::AudioConfig> channel_audio_config(uint32_t channel_id) const;
|
|
|
|
private:
|
|
void seed_default_channels();
|
|
|
|
mutable std::shared_mutex mu_;
|
|
|
|
std::shared_ptr<Database> db_;
|
|
|
|
uint64_t next_session_id_{1};
|
|
uint32_t next_user_id_{1};
|
|
uint32_t next_channel_id_{3}; // 1 and 2 are reserved for Lobby, Music Room
|
|
|
|
std::unordered_map<uint64_t, std::weak_ptr<ConnSession>> sessions_;
|
|
std::unordered_map<uint32_t, UserEntry> users_;
|
|
std::unordered_map<uint32_t, ChannelEntry> channels_;
|
|
std::unordered_map<uint64_t, voicecat::v1::Permissions> session_permissions_;
|
|
|
|
// Token → session_id (populated at auth, cleared on disconnect)
|
|
struct TokenHash {
|
|
size_t operator()(const std::array<uint8_t, 16>& t) const {
|
|
// FNV-1a over 16 bytes
|
|
size_t h = 14695981039346656037ULL;
|
|
for (auto b : t) { h ^= b; h *= 1099511628211ULL; }
|
|
return h;
|
|
}
|
|
};
|
|
std::unordered_map<std::array<uint8_t, 16>, uint64_t, TokenHash> udp_tokens_;
|
|
|
|
// UDP endpoint → session_id (populated after UDP binding packet arrives)
|
|
std::unordered_map<asio::ip::udp::endpoint, uint64_t, UdpEndpointHash> udp_endpoints_;
|
|
|
|
// ssrc → session_id (populated when StreamAnnounce is processed)
|
|
std::unordered_map<uint32_t, uint64_t> ssrc_to_session_;
|
|
std::atomic<uint32_t> next_ssrc_{1};
|
|
};
|
|
|
|
} // namespace voicecat::server
|
|
|
|
#endif // VOICECAT_SERVER_SESSION_REGISTRY_H
|