174 lines
6.0 KiB
C++
174 lines
6.0 KiB
C++
#pragma once
|
|
|
|
// Implements Echoion's host interface by adapting VoidTeamspeak's typed client
|
|
// operations and events. TeamSpeak protocol details remain inside
|
|
// VoidTeamspeak; Core only sees the host-neutral Echoion API contract.
|
|
|
|
#include <PluginInterface.hpp>
|
|
|
|
#include "echoion/standalone/EventCallbackGate.hpp"
|
|
#include <Void/TeamSpeak/TeamSpeak.hpp>
|
|
|
|
#include <atomic>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace Echoion::Standalone
|
|
{
|
|
|
|
class AudioEncoder;
|
|
|
|
// Runtime knobs the protocol client needs that ServerConfig does not carry.
|
|
// Sourced from environment / defaults by main(), so no Core changes are
|
|
// required.
|
|
struct StandaloneSettings {
|
|
std::string identityPath; // where to load/persist the TS identity
|
|
std::string avatarCachePath;
|
|
int securityLevel = 8;
|
|
int opusBitrate = 96000;
|
|
};
|
|
|
|
class StandaloneClientInterface : public Api::IPluginInterface
|
|
{
|
|
public:
|
|
|
|
explicit StandaloneClientInterface(StandaloneSettings settings);
|
|
~StandaloneClientInterface() override;
|
|
|
|
// Lifecycle
|
|
Api::Result<void> initialize() override;
|
|
Api::Result<void> shutdown() override;
|
|
|
|
// Clients
|
|
[[nodiscard]] Api::Result<std::vector<Api::ClientInfo>>
|
|
getClients() override;
|
|
[[nodiscard]] Api::Result<std::optional<Api::ClientInfo>>
|
|
getClient(uint64_t clientId) override;
|
|
[[nodiscard]] Api::Result<std::optional<std::string>>
|
|
getClientIp(uint64_t clientId) override;
|
|
[[nodiscard]] Api::Result<std::optional<std::string>>
|
|
getClientAvatarPath(uint64_t clientId) override;
|
|
[[nodiscard]] Api::Result<std::optional<std::string>>
|
|
getClientAvatarUrl(uint64_t clientId) override;
|
|
[[nodiscard]] Api::Result<std::optional<std::string>>
|
|
requestClientAvatar(uint64_t clientId,
|
|
const std::string& destDir) override;
|
|
Api::Result<void> moveClient(uint64_t clientId,
|
|
uint64_t channelId) override;
|
|
Api::Result<void> kickClient(uint64_t clientId,
|
|
const std::string& reason) override;
|
|
Api::Result<void>
|
|
kickClientFromChannel(uint64_t clientId,
|
|
const std::string& reason) override;
|
|
Api::Result<void> banClient(uint64_t clientId,
|
|
uint64_t durationSeconds,
|
|
const std::string& reason) override;
|
|
[[nodiscard]] Api::Result<std::vector<Api::BanInfo>>
|
|
getBans() override;
|
|
Api::Result<void> unban(uint64_t banId) override;
|
|
|
|
// Channels
|
|
[[nodiscard]] Api::Result<std::vector<Api::ChannelInfo>>
|
|
getChannels() const override;
|
|
[[nodiscard]] Api::Result<std::optional<Api::ChannelInfo>>
|
|
getChannel(uint64_t channelId) const override;
|
|
Api::Result<uint64_t> createChannel(
|
|
const Api::ChannelCreateOptions& options) override;
|
|
Api::Result<void>
|
|
editChannel(uint64_t channelId,
|
|
const Api::ChannelEditOptions& options) override;
|
|
Api::Result<void> reorderChannel(uint64_t channelId,
|
|
uint64_t parentId,
|
|
uint64_t orderAfterId) override;
|
|
Api::Result<void> deleteChannel(uint64_t channelId,
|
|
bool force) override;
|
|
|
|
[[nodiscard]] Api::Result<std::vector<Api::ServerGroupInfo>>
|
|
getServerGroups() override;
|
|
Api::Result<void>
|
|
addClientToServerGroup(uint64_t groupId,
|
|
uint64_t clientDatabaseId) override;
|
|
Api::Result<void>
|
|
removeClientFromServerGroup(uint64_t groupId,
|
|
uint64_t clientDatabaseId) override;
|
|
[[nodiscard]] Api::Result<Api::ServerInfo>
|
|
getTeamSpeakServerInfo() override;
|
|
|
|
// Messaging
|
|
Api::Result<void>
|
|
sendTextMessage(uint64_t targetId,
|
|
const std::string& message) override;
|
|
Api::Result<void>
|
|
sendChannelMessage(uint64_t channelId,
|
|
const std::string& message) override;
|
|
Api::Result<void>
|
|
sendServerMessage(const std::string& message) override;
|
|
|
|
// Audio
|
|
Api::Result<void>
|
|
sendAudioData(const std::span<const short>& data) override;
|
|
Api::Result<void>
|
|
setWhisperList(const std::vector<uint64_t>& clientIds) override;
|
|
|
|
// Connection
|
|
Api::Result<void>
|
|
connectToServer(const Api::ServerConfig& config) override;
|
|
Api::Result<void> disconnectFromServer() override;
|
|
[[nodiscard]] bool isConnected() const override;
|
|
Api::Result<void> reconnect() override;
|
|
|
|
// Events
|
|
void registerEventCallback(const Api::EventCallback& cb) override;
|
|
void unregisterEventCallback(const Api::EventCallback& cb) override;
|
|
[[nodiscard]] Api::Result<std::string>
|
|
hostControl(const std::string& method,
|
|
const std::string& requestJson) override;
|
|
using HostControlHandler = std::function<Api::Result<std::string>(
|
|
const std::string&, const std::string&)>;
|
|
void setHostControlHandler(HostControlHandler handler);
|
|
// Prevent new event dispatches, release registered callbacks and
|
|
// wait until every callback already in flight has returned. The
|
|
// host must call this before unloading code that owns an event
|
|
// callback.
|
|
void quiesceEventCallbacks();
|
|
|
|
private:
|
|
|
|
void wireCallbacks();
|
|
void broadcast(const Api::PluginEvent& event);
|
|
void loadInitialSnapshot();
|
|
[[nodiscard]] Api::Result<void> commandResult(
|
|
const Void::Result::Result<void, Void::TeamSpeak::Error>&
|
|
result) const;
|
|
|
|
StandaloneSettings settings_;
|
|
std::shared_ptr<Void::TeamSpeak::Identity> identity_;
|
|
std::unique_ptr<Void::TeamSpeak::Client> client_;
|
|
std::unique_ptr<AudioEncoder> encoder_;
|
|
|
|
Api::ServerConfig currentConfig_{};
|
|
std::atomic<bool> initialized_{false};
|
|
|
|
mutable std::mutex mirrorMutex_;
|
|
std::map<uint64_t, Void::TeamSpeak::RemoteClient> clients_;
|
|
std::map<uint64_t, Void::TeamSpeak::Channel> channels_;
|
|
std::optional<Void::TeamSpeak::ServerInfo> serverInfo_;
|
|
std::map<uint64_t, std::string> clientIps_;
|
|
std::map<uint64_t, std::string> avatarHashes_;
|
|
std::map<uint64_t, std::string> avatarUrls_;
|
|
std::map<uint64_t, std::string> avatarPaths_;
|
|
std::vector<uint64_t> whisperList_;
|
|
std::jthread snapshotThread_;
|
|
mutable std::mutex hostControlMutex_;
|
|
HostControlHandler hostControlHandler_;
|
|
|
|
EventCallbackGate eventCallbacks_;
|
|
};
|
|
|
|
} // namespace Echoion::Standalone
|