many different fixes
This commit is contained in:
1 parent
a583c17916
commit
53801f2b53
36 files changed
+5149
-1291
No files matched your search
@@ -1,14 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <echoion/api.hpp>
|
||||
#include "commands/Command.hpp"
|
||||
#include "echoion/api/ConfigTypes.hpp"
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <echoion/api.hpp>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Core {
|
||||
|
||||
@@ -28,10 +33,13 @@ using PermissionCheckFunc = std::function<bool(uint64_t clientId, CommandCategor
|
||||
class CommandManager {
|
||||
public:
|
||||
CommandManager();
|
||||
~CommandManager() = default;
|
||||
~CommandManager();
|
||||
|
||||
// Disable copy
|
||||
CommandManager(const CommandManager&) = delete;
|
||||
// Shutdown the command manager (stops thread pool)
|
||||
void shutdown();
|
||||
|
||||
// Disable copy
|
||||
CommandManager(const CommandManager&) = delete;
|
||||
CommandManager& operator=(const CommandManager&) = delete;
|
||||
|
||||
// Enable move
|
||||
@@ -59,25 +67,37 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Dispatches a command message to the appropriate handler.
|
||||
* @param clientId The client ID who invoked the command.
|
||||
* @param message The full message (including prefix, e.g., "!play song").
|
||||
* @param musicManager Weak pointer to the MusicManager.
|
||||
* @param replyFunc Function to call to send a reply.
|
||||
* @param invokerName Name of the invoking user.
|
||||
* @return true if a command was found and executed, false otherwise.
|
||||
*/
|
||||
[[nodiscard]] bool dispatch(
|
||||
uint64_t clientId,
|
||||
std::string_view message,
|
||||
std::weak_ptr<MusicManager> musicManager,
|
||||
std::function<void(std::string_view)> replyFunc,
|
||||
std::string_view invokerName = ""
|
||||
);
|
||||
/**
|
||||
* @brief Dispatches a command message to the appropriate handler (ASYNC).
|
||||
* Commands are executed in a thread pool, allowing multiple users to
|
||||
* use the bot simultaneously without blocking.
|
||||
* @param clientId The client ID who invoked the command.
|
||||
* @param message The full message (including prefix, e.g., "!play song").
|
||||
* @param musicManager Weak pointer to the MusicManager.
|
||||
* @param replyFunc Function to call to send a reply.
|
||||
* @param invokerName Name of the invoking user.
|
||||
* @return true if a command was found and queued for execution, false
|
||||
* otherwise.
|
||||
*/
|
||||
[[nodiscard]] bool dispatch(uint64_t clientId,
|
||||
std::string_view message,
|
||||
std::weak_ptr<MusicManager> musicManager,
|
||||
std::function<void(std::string_view)> replyFunc,
|
||||
std::string_view invokerName = "");
|
||||
|
||||
[[nodiscard]] bool hasCommand(std::string_view name) const;
|
||||
[[nodiscard]] std::optional<CommandPtr> getCommand(std::string_view name) const;
|
||||
/**
|
||||
* @brief Executes a command synchronously (for internal use).
|
||||
* Prefer using dispatch() for normal command handling.
|
||||
*/
|
||||
[[nodiscard]] bool
|
||||
dispatchSync(uint64_t clientId,
|
||||
std::string_view message,
|
||||
std::weak_ptr<MusicManager> musicManager,
|
||||
std::function<void(std::string_view)> replyFunc,
|
||||
std::string_view invokerName = "");
|
||||
|
||||
[[nodiscard]] bool hasCommand(std::string_view name) const;
|
||||
[[nodiscard]] std::optional<CommandPtr> getCommand(std::string_view name) const;
|
||||
[[nodiscard]] std::vector<std::pair<std::string, CommandPtr>> getAllCommands() const;
|
||||
|
||||
// Get commands visible to a specific client (for help)
|
||||
@@ -119,6 +139,27 @@ private:
|
||||
// Permission checking
|
||||
PermissionCheckFunc permissionCheck_;
|
||||
std::optional<Api::AppConfig> serverConfig_;
|
||||
};
|
||||
|
||||
// Async command execution
|
||||
struct CommandTask {
|
||||
uint64_t clientId;
|
||||
std::string message;
|
||||
std::weak_ptr<MusicManager> musicManager;
|
||||
std::function<void(std::string_view)> replyFunc;
|
||||
std::string invokerName;
|
||||
};
|
||||
|
||||
void startThreadPool();
|
||||
void processCommandQueue();
|
||||
void executeCommandAsync(CommandTask task);
|
||||
|
||||
std::vector<std::jthread> workerThreads_;
|
||||
std::queue<CommandTask> commandQueue_;
|
||||
std::mutex queueMutex_;
|
||||
std::condition_variable queueCv_;
|
||||
std::atomic<bool> shutdown_{false};
|
||||
static constexpr size_t THREAD_POOL_SIZE =
|
||||
4; // Handle up to 4 concurrent commands
|
||||
|
||||
}; // namespace Echoion::Core
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user