1 parent
45eb2cab42
commit
8aa968eb9f
3 files changed
+922
-616
No files matched your search
@@ -1,10 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
// Implements Echoion's IPluginInterface against the native TeamSpeak UDP
|
||||
// protocol through VoidTeamspeak 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.
|
||||
// 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>
|
||||
|
||||
@@ -63,12 +61,42 @@ namespace Echoion::Standalone
|
||||
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>
|
||||
@@ -85,14 +113,6 @@ namespace Echoion::Standalone
|
||||
sendAudioData(const std::span<const short>& data) override;
|
||||
Api::Result<void>
|
||||
setWhisperList(const std::vector<uint64_t>& clientIds) override;
|
||||
[[nodiscard]] Api::Result<AudioStreamId>
|
||||
createAudioStream(const AudioStreamOptions& options) override;
|
||||
Api::Result<void> destroyAudioStream(AudioStreamId id) override;
|
||||
Api::Result<void> sendAudioDataToStream(
|
||||
AudioStreamId id, const std::span<const short>& data) override;
|
||||
Api::Result<void> setAudioStreamRecipients(
|
||||
AudioStreamId id,
|
||||
const std::vector<uint64_t>& recipients) override;
|
||||
|
||||
// Connection
|
||||
Api::Result<void>
|
||||
@@ -102,28 +122,22 @@ namespace Echoion::Standalone
|
||||
Api::Result<void> reconnect() override;
|
||||
|
||||
// Events
|
||||
void
|
||||
registerEventCallback(const Api::EventCallback& cb) override;
|
||||
void
|
||||
unregisterEventCallback(const Api::EventCallback& cb) override;
|
||||
// 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 registerEventCallback(const Api::EventCallback& cb) override;
|
||||
void unregisterEventCallback(const Api::EventCallback& cb) override;
|
||||
// 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 onCommand(std::string_view name,
|
||||
const std::vector<Void::TeamSpeak::CommandFields>& records);
|
||||
void onTextMessage(const Void::TeamSpeak::CommandFields& params);
|
||||
void broadcast(const Api::PluginEvent& event);
|
||||
void upsertClientGroup(const Void::TeamSpeak::CommandFields& p,
|
||||
bool emitConnected);
|
||||
void refreshClient(const Void::TeamSpeak::CommandFields& params);
|
||||
void loadInitialSnapshot();
|
||||
[[nodiscard]] Api::Result<void> commandResult(
|
||||
const Void::Result::Result<void, Void::TeamSpeak::Error>& result) const;
|
||||
const Void::Result::Result<void, Void::TeamSpeak::Error>&
|
||||
result) const;
|
||||
|
||||
StandaloneSettings settings_;
|
||||
std::shared_ptr<Void::TeamSpeak::Identity> identity_;
|
||||
@@ -134,17 +148,14 @@ namespace Echoion::Standalone
|
||||
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, 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_;
|
||||
struct AudioStreamRuntime;
|
||||
std::map<AudioStreamId, std::shared_ptr<AudioStreamRuntime>> audioStreams_;
|
||||
std::atomic<AudioStreamId> nextAudioStreamId_{1};
|
||||
mutable std::mutex audioStreamsMutex_;
|
||||
std::jthread snapshotThread_;
|
||||
|
||||
EventCallbackGate eventCallbacks_;
|
||||
|
||||
+811
-522
File diff suppressed because it is too large.
Load diff
+67
-61
@@ -1,21 +1,16 @@
|
||||
// Standalone Echoion host.
|
||||
//
|
||||
// Replaces the TeamSpeak-client + plugin sandwich: loads the same hot-reloadable
|
||||
// Core shared library via the (TeamSpeak-independent) CoreLoader, but backs
|
||||
// IPluginInterface with a native TeamSpeak UDP protocol client instead of the
|
||||
// TS3 Client Plugin SDK. The bot therefore runs as a plain daemon — no
|
||||
// TeamSpeak client, no Weston/RDP, no plugin directory.
|
||||
// Standalone Echoion host. It loads the hot-reloadable Core through CoreLoader
|
||||
// and adapts Echoion's host contract to VoidTeamspeak's typed client API.
|
||||
|
||||
#include "echoion/host/CoreLoader.hpp"
|
||||
#include "echoion/standalone/StandaloneClientInterface.hpp"
|
||||
#include "echoion/standalone/ReleaseManifest.hpp"
|
||||
#include "echoion/standalone/StandaloneClientInterface.hpp"
|
||||
|
||||
#include <CoreInterface.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Stacktrace/Stacktrace.hpp>
|
||||
#include <Void/System/FileWatcher.hpp>
|
||||
#include <Void/System/Process.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/System/Process.hpp>
|
||||
#include <Void/TeamSpeak/Identity.hpp>
|
||||
|
||||
#include <atomic>
|
||||
@@ -50,8 +45,8 @@ namespace
|
||||
g_cv.notify_all();
|
||||
}
|
||||
|
||||
std::optional<std::string_view> argumentValue(
|
||||
int argc, char** argv, std::string_view name)
|
||||
std::optional<std::string_view> argumentValue(int argc, char** argv,
|
||||
std::string_view name)
|
||||
{
|
||||
for (int index = 1; index + 1 < argc; ++index) {
|
||||
if (std::string_view{argv[index]} == name) {
|
||||
@@ -76,14 +71,17 @@ namespace
|
||||
{
|
||||
const auto transaction = directory / "release-transaction";
|
||||
if (!std::filesystem::exists(transaction) ||
|
||||
std::getenv("ECHOION_REEXEC_VERSION")) return;
|
||||
std::getenv("ECHOION_REEXEC_VERSION")) {
|
||||
return;
|
||||
}
|
||||
auto version = Void::FileSystem::read_file(transaction);
|
||||
if (version && !version->empty()) {
|
||||
std::ignore = Void::FileSystem::atomic_write(
|
||||
directory / "failed-release", *version);
|
||||
Void::Log::error("main",
|
||||
"Release {} failed its startup health check; stable service restored",
|
||||
*version);
|
||||
"Release {} failed its startup health check; "
|
||||
"stable service restored",
|
||||
*version);
|
||||
}
|
||||
std::ignore = Void::FileSystem::remove_path(transaction);
|
||||
}
|
||||
@@ -132,10 +130,9 @@ namespace
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
const std::string_view argument{argv[i]};
|
||||
if (argument == "--help") {
|
||||
std::cout
|
||||
<< "Usage: echoiond [--config PATH]\n"
|
||||
" echoiond --import-identity EXPORT.ini "
|
||||
"[--identity-path PATH] [--force]\n";
|
||||
std::cout << "Usage: echoiond [--config PATH]\n"
|
||||
" echoiond --import-identity EXPORT.ini "
|
||||
"[--identity-path PATH] [--force]\n";
|
||||
return 0;
|
||||
}
|
||||
if (argument == "--import-identity") {
|
||||
@@ -165,7 +162,8 @@ namespace
|
||||
}
|
||||
const auto identity =
|
||||
Void::TeamSpeak::Identity::importTeamSpeakIni(source->string());
|
||||
if (const auto parent = destination.parent_path(); !parent.empty()) {
|
||||
if (const auto parent = destination.parent_path();
|
||||
!parent.empty()) {
|
||||
std::filesystem::create_directories(parent);
|
||||
}
|
||||
identity.save(destination.string());
|
||||
@@ -186,9 +184,10 @@ int main(int argc, char** argv)
|
||||
std::cerr << Void::Stacktrace::format_stacktrace(frames) << '\n';
|
||||
std::abort();
|
||||
});
|
||||
if (const auto validationPath = argumentValue(argc, argv,
|
||||
"--validate-release")) {
|
||||
auto manifest = Echoion::Standalone::ReleaseManifest::load(*validationPath);
|
||||
if (const auto validationPath =
|
||||
argumentValue(argc, argv, "--validate-release")) {
|
||||
auto manifest =
|
||||
Echoion::Standalone::ReleaseManifest::load(*validationPath);
|
||||
if (!manifest) {
|
||||
std::cerr << manifest.error().message << '\n';
|
||||
return 2;
|
||||
@@ -203,8 +202,8 @@ int main(int argc, char** argv)
|
||||
std::filesystem::path cfgDir = configDir();
|
||||
recordInterruptedRelease(cfgDir);
|
||||
std::filesystem::path cfgPath = cfgDir / "config.json";
|
||||
const auto identityPath = std::filesystem::path(envOr(
|
||||
"ECHOION_IDENTITY_PATH", (cfgDir / "identity.txt").string()));
|
||||
const auto identityPath = std::filesystem::path(
|
||||
envOr("ECHOION_IDENTITY_PATH", (cfgDir / "identity.txt").string()));
|
||||
if (const auto importResult = importIdentity(argc, argv, identityPath);
|
||||
importResult >= 0) {
|
||||
return importResult;
|
||||
@@ -222,8 +221,9 @@ int main(int argc, char** argv)
|
||||
settings.securityLevel = envInt("ECHOION_SECURITY_LEVEL", 8);
|
||||
settings.opusBitrate = envInt("ECHOION_OPUS_BITRATE", 96000);
|
||||
|
||||
auto interface = std::make_shared<Echoion::Standalone::StandaloneClientInterface>(
|
||||
settings);
|
||||
auto interface =
|
||||
std::make_shared<Echoion::Standalone::StandaloneClientInterface>(
|
||||
settings);
|
||||
if (auto r = interface->initialize(); !r) {
|
||||
Void::Log::error("main", "interface init failed: {}",
|
||||
r.error().message);
|
||||
@@ -242,7 +242,7 @@ int main(int argc, char** argv)
|
||||
if (const auto restorePath = argumentValue(argc, argv, "--restore-state")) {
|
||||
if (auto result = loader.restoreCheckpoint(*restorePath); !result) {
|
||||
Void::Log::error("main", "failed to restore restart checkpoint: {}",
|
||||
result.error().message);
|
||||
result.error().message);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -254,12 +254,12 @@ int main(int argc, char** argv)
|
||||
}
|
||||
|
||||
core->setPluginInterface(interface);
|
||||
interface->registerEventCallback([core](const Echoion::Api::PluginEvent& ev) {
|
||||
std::ignore = core->onPluginEvent(ev);
|
||||
});
|
||||
loader.setHotReloadPrepareCallback([interface] {
|
||||
interface->quiesceEventCallbacks();
|
||||
});
|
||||
interface->registerEventCallback(
|
||||
[core](const Echoion::Api::PluginEvent& ev) {
|
||||
std::ignore = core->onPluginEvent(ev);
|
||||
});
|
||||
loader.setHotReloadPrepareCallback(
|
||||
[interface] { interface->quiesceEventCallbacks(); });
|
||||
// Re-register the Core callback after each hot reload (new Core instance).
|
||||
loader.setHotReloadCallback([&loader, interface] {
|
||||
if (auto c = loader.getCore()) {
|
||||
@@ -278,14 +278,15 @@ int main(int argc, char** argv)
|
||||
if (const char* manifest = std::getenv("ECHOION_RELEASE_MANIFEST")) {
|
||||
releaseManifestPath = manifest;
|
||||
releaseWatcher = std::make_unique<Void::FileSystem::FileWatcher>();
|
||||
releaseWatcher->watch(*releaseManifestPath,
|
||||
releaseWatcher->watch(
|
||||
*releaseManifestPath,
|
||||
[](const Void::FileSystem::FileWatcher::Event& event) {
|
||||
if (event.exists) {
|
||||
requestUpdate();
|
||||
}
|
||||
});
|
||||
Void::Log::info("main", "Watching release manifest {}",
|
||||
releaseManifestPath->string());
|
||||
releaseManifestPath->string());
|
||||
} else {
|
||||
// Compatibility mode for development. Production release bundles use
|
||||
// transactional daemon re-exec instead of replacing Core in isolation.
|
||||
@@ -296,13 +297,14 @@ int main(int argc, char** argv)
|
||||
std::signal(SIGTERM, onSignal);
|
||||
Void::Log::info("main", "Echoion standalone running. Ctrl-C to stop.");
|
||||
if (const char* version = std::getenv("ECHOION_REEXEC_VERSION")) {
|
||||
std::ignore = Void::FileSystem::atomic_write(
|
||||
cfgDir / "release-health", version);
|
||||
std::ignore = Void::FileSystem::remove_path(
|
||||
cfgDir / "release-transaction");
|
||||
std::ignore =
|
||||
Void::FileSystem::atomic_write(cfgDir / "release-health", version);
|
||||
std::ignore =
|
||||
Void::FileSystem::remove_path(cfgDir / "release-transaction");
|
||||
std::ignore = Void::FileSystem::remove_path(cfgDir / "failed-release");
|
||||
::unsetenv("ECHOION_REEXEC_VERSION");
|
||||
Void::Log::info("main", "Release {} passed startup health check", version);
|
||||
Void::Log::info("main", "Release {} passed startup health check",
|
||||
version);
|
||||
}
|
||||
|
||||
std::optional<Echoion::Standalone::ReleaseManifest> pendingRelease;
|
||||
@@ -310,49 +312,52 @@ int main(int argc, char** argv)
|
||||
while (!g_stop.load() && !pendingRelease) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(g_mu);
|
||||
g_cv.wait(lock, [] {
|
||||
return g_stop.load() || g_updateRequested.load();
|
||||
});
|
||||
g_cv.wait(lock,
|
||||
[] { return g_stop.load() || g_updateRequested.load(); });
|
||||
}
|
||||
if (g_stop.load() || !releaseManifestPath) {
|
||||
break;
|
||||
}
|
||||
g_updateRequested = false;
|
||||
auto manifest = Echoion::Standalone::ReleaseManifest::load(
|
||||
*releaseManifestPath);
|
||||
auto manifest =
|
||||
Echoion::Standalone::ReleaseManifest::load(*releaseManifestPath);
|
||||
if (!manifest) {
|
||||
Void::Log::error("main", "release rejected: {}",
|
||||
manifest.error().message);
|
||||
manifest.error().message);
|
||||
continue;
|
||||
}
|
||||
if (auto failed = Void::FileSystem::read_file(cfgDir / "failed-release");
|
||||
if (auto failed =
|
||||
Void::FileSystem::read_file(cfgDir / "failed-release");
|
||||
failed && *failed == manifest->version) {
|
||||
Void::Log::error("main",
|
||||
"Release {} was previously rolled back; publish a new version to retry",
|
||||
manifest->version);
|
||||
"Release {} was previously rolled back; publish a "
|
||||
"new version to retry",
|
||||
manifest->version);
|
||||
continue;
|
||||
}
|
||||
auto executable = std::filesystem::canonical("/proc/self/exe");
|
||||
auto validation = Void::Process::run(executable,
|
||||
{"--validate-release", releaseManifestPath->string()}, {},
|
||||
std::chrono::seconds{30});
|
||||
auto validation = Void::Process::run(
|
||||
executable, {"--validate-release", releaseManifestPath->string()},
|
||||
{}, std::chrono::seconds{30});
|
||||
if (!validation || validation->exit_code != 0) {
|
||||
Void::Log::error("main", "release validation failed: {}",
|
||||
validation ? validation->err : validation.error().message());
|
||||
validation ? validation->err
|
||||
: validation.error().message());
|
||||
continue;
|
||||
}
|
||||
checkpointPath = configDir() / "restart-state.bin";
|
||||
if (auto checkpoint = loader.checkpoint(checkpointPath); !checkpoint) {
|
||||
Void::Log::error("main", "release checkpoint failed: {}",
|
||||
checkpoint.error().message);
|
||||
checkpoint.error().message);
|
||||
continue;
|
||||
}
|
||||
pendingRelease = std::move(*manifest);
|
||||
}
|
||||
|
||||
releaseWatcher.reset();
|
||||
Void::Log::info("main", "{}", pendingRelease
|
||||
? "Applying validated release..." : "Shutting down...");
|
||||
Void::Log::info("main", "{}",
|
||||
pendingRelease ? "Applying validated release..."
|
||||
: "Shutting down...");
|
||||
loader.disableAutoReload();
|
||||
(void)interface->disconnectFromServer();
|
||||
interface->quiesceEventCallbacks();
|
||||
@@ -360,10 +365,10 @@ int main(int argc, char** argv)
|
||||
(void)interface->shutdown();
|
||||
if (pendingRelease) {
|
||||
if (auto transaction = Void::FileSystem::atomic_write(
|
||||
cfgDir / "release-transaction", pendingRelease->version);
|
||||
cfgDir / "release-transaction", pendingRelease->version);
|
||||
!transaction) {
|
||||
Void::Log::error("main", "cannot record release transaction: {}",
|
||||
transaction.error().message());
|
||||
transaction.error().message());
|
||||
return 1;
|
||||
}
|
||||
::setenv("ECHOION_CORE_PATH", pendingRelease->core.c_str(), 1);
|
||||
@@ -380,10 +385,11 @@ int main(int argc, char** argv)
|
||||
}
|
||||
arguments.emplace_back("--restore-state");
|
||||
arguments.push_back(checkpointPath.string());
|
||||
if (auto result = Void::Process::reexec(pendingRelease->executable,
|
||||
arguments); !result) {
|
||||
if (auto result =
|
||||
Void::Process::reexec(pendingRelease->executable, arguments);
|
||||
!result) {
|
||||
Void::Log::error("main", "release re-exec failed: {}",
|
||||
result.error().message());
|
||||
result.error().message());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user