138 lines
4.5 KiB
C++
138 lines
4.5 KiB
C++
#pragma once
|
|
|
|
// Implements Echoion's IPluginInterface against the native TeamSpeak UDP
|
|
// protocol (EchoionProtocol) instead of the TS3 Client Plugin SDK. This is the
|
|
// single seam that lets the whole Core run as a standalone application: Core
|
|
// only ever talks to IPluginInterface, so swapping this implementation in for
|
|
// the plugin drops the TeamSpeak-client dependency entirely.
|
|
|
|
#include <PluginInterface.hpp>
|
|
|
|
#include "echoion/protocol/Identity.hpp"
|
|
#include "echoion/protocol/TsClient.hpp"
|
|
|
|
#include <atomic>
|
|
#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;
|
|
|
|
// Channels
|
|
[[nodiscard]] Api::Result<std::vector<Api::ChannelInfo>>
|
|
getChannels() const override;
|
|
[[nodiscard]] Api::Result<std::optional<Api::ChannelInfo>>
|
|
getChannel(uint64_t channelId) const 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;
|
|
|
|
private:
|
|
|
|
void wireCallbacks();
|
|
void onCommand(const Protocol::ParsedCommand& parsed);
|
|
void onTextMessage(const Protocol::CommandParams& params);
|
|
void broadcast(const Api::PluginEvent& event);
|
|
void upsertClientGroup(const Protocol::CommandParams& p,
|
|
bool emitConnected);
|
|
void refreshClient(const Protocol::CommandParams& params);
|
|
void loadInitialSnapshot();
|
|
[[nodiscard]] Api::Result<void>
|
|
commandResult(const Protocol::CommandResult& result) const;
|
|
|
|
StandaloneSettings settings_;
|
|
std::unique_ptr<Protocol::Identity> identity_;
|
|
std::unique_ptr<Protocol::TsClient> client_;
|
|
std::unique_ptr<AudioEncoder> encoder_;
|
|
|
|
Api::ServerConfig currentConfig_{};
|
|
std::atomic<bool> initialized_{false};
|
|
|
|
mutable std::mutex mirrorMutex_;
|
|
std::map<uint64_t, Api::ClientInfo> clients_;
|
|
std::map<uint64_t, Api::ChannelInfo> channels_;
|
|
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_;
|
|
|
|
std::mutex cbMutex_;
|
|
std::vector<Api::EventCallback> callbacks_;
|
|
};
|
|
|
|
} // namespace Echoion::Standalone
|