Hot reload fix
This commit is contained in:
1 parent
8b8a7f4f9a
commit
0fa9db4691
6 files changed
+677
-178
No files matched your search
@@ -11,6 +11,7 @@
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <queue>
|
||||
#include <functional>
|
||||
|
||||
namespace Echoion::Plugin {
|
||||
|
||||
@@ -32,6 +33,10 @@ public:
|
||||
void enableAutoReload();
|
||||
void disableAutoReload();
|
||||
[[nodiscard]] bool isAutoReloadEnabled() const noexcept;
|
||||
|
||||
// Callback for hot reload completion (called after auto-reload finishes)
|
||||
using HotReloadCallback = std::function<void()>;
|
||||
void setHotReloadCallback(HotReloadCallback callback) { hotReloadCallback_ = std::move(callback); }
|
||||
|
||||
private:
|
||||
Api::Result<void> loadLibraryInternal(const std::filesystem::path& path);
|
||||
@@ -54,7 +59,20 @@ private:
|
||||
FileWatcher fileWatcher_;
|
||||
std::atomic<bool> autoReloadEnabled_{false};
|
||||
|
||||
// Deferred reload - waits for current track to finish
|
||||
std::thread deferredReloadThread_;
|
||||
std::atomic<bool> hasPendingReload_{false};
|
||||
std::filesystem::path pendingReloadPath_;
|
||||
std::string pendingTrackId_; // Track ID that was playing when reload scheduled
|
||||
mutable std::mutex pendingReloadMutex_;
|
||||
|
||||
// Hot reload completion callback
|
||||
HotReloadCallback hotReloadCallback_;
|
||||
|
||||
void onCoreFileChanged();
|
||||
void waitForTrackAndReload(const std::filesystem::path& targetPath, const std::string& trackId);
|
||||
[[nodiscard]] bool isPlaybackActive() const;
|
||||
[[nodiscard]] bool isSameTrackPlaying(const std::string& trackId) const;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Plugin
|
||||
@@ -1,13 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <echoion/api.hpp>
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <dylib.hpp>
|
||||
#include <echoion/api.hpp>
|
||||
#include <future>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <queue>
|
||||
#include <condition_variable>
|
||||
#include <thread>
|
||||
|
||||
// TeamSpeak SDK headers
|
||||
#include "ts3_functions.h"
|
||||
@@ -18,9 +20,16 @@ namespace Echoion::Plugin {
|
||||
struct AudioDeviceConfig {
|
||||
static constexpr const char* DEVICE_ID = "echoion_audio_device";
|
||||
static constexpr const char* DEVICE_NAME = "Echoion Music Bot Audio";
|
||||
static constexpr int SAMPLE_RATE = 192000; // Match reference implementation
|
||||
static constexpr int CHANNELS = 2;
|
||||
static constexpr int SAMPLE_SIZE = sizeof(int16_t);
|
||||
static constexpr int SAMPLE_RATE =
|
||||
192000; // 192kHz for high quality audio (CODEC_OPUS_MUSIC)
|
||||
static constexpr int CHANNELS = 2;
|
||||
static constexpr int SAMPLE_SIZE = sizeof(int16_t);
|
||||
};
|
||||
|
||||
// Server groups cache entry
|
||||
struct ServerGroupsCacheEntry {
|
||||
std::vector<uint64_t> groups;
|
||||
std::chrono::steady_clock::time_point timestamp;
|
||||
};
|
||||
|
||||
// Plugin implementation of IPluginInterface for the Core to use
|
||||
@@ -37,14 +46,19 @@ public:
|
||||
Api::Result<void> shutdown() override;
|
||||
|
||||
// Client operations
|
||||
[[nodiscard]] Api::Result<std::vector<Api::ClientInfo>> getClients() const override;
|
||||
[[nodiscard]] Api::Result<std::optional<Api::ClientInfo>> getClient(uint64_t clientId) const override;
|
||||
Api::Result<void> moveClient(uint64_t clientId, uint64_t channelId) override;
|
||||
Api::Result<void> kickClient(uint64_t clientId, const std::string& reason) override;
|
||||
[[nodiscard]] Api::Result<std::vector<Api::ClientInfo>>
|
||||
getClients() override;
|
||||
[[nodiscard]] Api::Result<std::optional<Api::ClientInfo>>
|
||||
getClient(uint64_t clientId) override;
|
||||
Api::Result<void> moveClient(uint64_t clientId,
|
||||
uint64_t channelId) override;
|
||||
Api::Result<void> kickClient(uint64_t clientId,
|
||||
const std::string& reason) override;
|
||||
|
||||
// Channel operations
|
||||
[[nodiscard]] Api::Result<std::vector<Api::ChannelInfo>> getChannels() const override;
|
||||
[[nodiscard]] Api::Result<std::optional<Api::ChannelInfo>> getChannel(uint64_t channelId) const override;
|
||||
// Channel operations
|
||||
[[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;
|
||||
@@ -52,12 +66,15 @@ public:
|
||||
Api::Result<void> sendServerMessage(const std::string& message) override;
|
||||
|
||||
// Audio
|
||||
Api::Result<void> sendAudioData(const std::span<const uint8_t>& data) override;
|
||||
Api::Result<void> setWhisperList(const std::vector<uint64_t>& clientIds) override;
|
||||
Api::Result<void>
|
||||
sendAudioData(const std::span<const short>& data) override;
|
||||
Api::Result<void>
|
||||
setWhisperList(const std::vector<uint64_t>& clientIds) override;
|
||||
|
||||
// Event subscription
|
||||
void registerEventCallback(const Api::EventCallback& callback) override;
|
||||
// Event subscription
|
||||
void registerEventCallback(const Api::EventCallback& callback) override;
|
||||
void unregisterEventCallback(const Api::EventCallback& callback) override;
|
||||
void clearEventCallbacks();
|
||||
|
||||
// TeamSpeak event handlers (called by TeamSpeak SDK)
|
||||
void onClientConnected(uint64_t serverConnectionHandlerId, uint64_t clientId);
|
||||
@@ -68,13 +85,19 @@ public:
|
||||
void onVoiceDataReceived(uint64_t serverConnectionHandlerId, uint64_t clientId,
|
||||
const std::span<const uint8_t>& data);
|
||||
|
||||
// Server connection management (IPluginInterface implementation)
|
||||
Api::Result<void> connectToServer(const Api::ServerConfig& config) override;
|
||||
Api::Result<void> disconnectFromServer() override;
|
||||
[[nodiscard]] bool isConnected() const override;
|
||||
Api::Result<void> reconnect() override;
|
||||
// Server group list callback (called by TeamSpeak SDK)
|
||||
void onServerGroupByClientID(uint64_t serverConnectionHandlerId,
|
||||
uint64_t clientDatabaseId,
|
||||
const char* groupName, uint64_t serverGroupId,
|
||||
uint64_t clientId);
|
||||
|
||||
// Audio device management
|
||||
// Server connection management (IPluginInterface implementation)
|
||||
Api::Result<void> connectToServer(const Api::ServerConfig& config) override;
|
||||
Api::Result<void> disconnectFromServer() override;
|
||||
[[nodiscard]] bool isConnected() const override;
|
||||
Api::Result<void> reconnect() override;
|
||||
|
||||
// Audio device management
|
||||
Api::Result<void> registerAudioDevice();
|
||||
Api::Result<void> unregisterAudioDevice();
|
||||
Api::Result<void> openAudioDevice();
|
||||
@@ -87,31 +110,48 @@ public:
|
||||
[[nodiscard]] uint64_t getServerConnectionHandlerId() const;
|
||||
|
||||
// Audio data submission (for internal use)
|
||||
void submitAudioData(const std::span<const int16_t> samples);
|
||||
void submitAudioData(const std::span<const short> samples);
|
||||
|
||||
// Automatic configuration
|
||||
Api::Result<void> configureAudioProfile();
|
||||
Api::Result<void> checkAndHandleFirstRun();
|
||||
|
||||
private:
|
||||
void broadcastEvent(const Api::PluginEvent& event);
|
||||
std::string getClientName(uint64_t clientId) const;
|
||||
std::string getChannelName(uint64_t channelId) const;
|
||||
|
||||
TS3Functions* ts3Functions_ = nullptr;
|
||||
uint64_t serverConnectionHandlerId_ = 0;
|
||||
std::atomic<bool> initialized_{false};
|
||||
std::atomic<bool> audioDeviceRegistered_{false};
|
||||
std::atomic<bool> audioDeviceOpen_{false};
|
||||
// Fetch server groups for a client (returns future that completes when
|
||||
// groups are received)
|
||||
std::future<std::vector<uint64_t>> fetchServerGroups(uint64_t clientId);
|
||||
|
||||
std::vector<Api::EventCallback> eventCallbacks_;
|
||||
// Get cached server groups (or empty if not cached)
|
||||
std::vector<uint64_t> getCachedServerGroups(uint64_t clientId);
|
||||
|
||||
TS3Functions* ts3Functions_ = nullptr;
|
||||
uint64_t serverConnectionHandlerId_ = 0;
|
||||
std::atomic<bool> initialized_{false};
|
||||
std::atomic<bool> audioDeviceRegistered_{false};
|
||||
std::atomic<bool> audioDeviceOpen_{false};
|
||||
|
||||
std::vector<Api::EventCallback> eventCallbacks_;
|
||||
mutable std::mutex eventMutex_;
|
||||
|
||||
// Audio data queue
|
||||
std::queue<std::vector<int16_t>> audioQueue_;
|
||||
std::mutex audioMutex_;
|
||||
std::condition_variable audioCv_;
|
||||
std::queue<std::vector<short>> audioQueue_;
|
||||
std::mutex audioMutex_;
|
||||
std::condition_variable audioCv_;
|
||||
std::atomic<bool> stopAudioThread_{false};
|
||||
std::jthread audioThread_;
|
||||
|
||||
// Connection info
|
||||
Api::ServerConfig currentServerConfig_;
|
||||
// Server groups cache
|
||||
mutable std::mutex serverGroupsMutex_;
|
||||
std::map<uint64_t, ServerGroupsCacheEntry> serverGroupsCache_;
|
||||
std::map<uint64_t, std::promise<std::vector<uint64_t>>>
|
||||
pendingServerGroupRequests_;
|
||||
|
||||
// Connection info
|
||||
Api::ServerConfig currentServerConfig_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Plugin
|
||||
Reference in new issue
Block a user