비동기 다운로드 구현 완료

This commit is contained in:
2024-10-19 05:06:21 +09:00
parent b4476c68d7
commit 30569f4472
11 changed files with 170 additions and 116 deletions

View File

@@ -6,27 +6,40 @@
#include <thread>
#include <condition_variable>
#include <curl/curl.h>
#include <dpp/dpp.h>
#include <MusicQueue.hpp>
#define WORKER_COUNT 5
namespace bumbleBee {
class AsyncDownloadManager {
public:
static void enqueue(std::string query) {
static AsyncDownloadManager dl;
std::thread th(&bumbleBee::AsyncDownloadManager::enqueueAsyncDL, &dl, query);
static AsyncDownloadManager& getInstance(int worker_count, std::weak_ptr<dpp::cluster> bot, std::weak_ptr<bumbleBee::MusicQueue> musicQueue) {
static AsyncDownloadManager dl(worker_count);
dl.bot = bot;
dl.musicQueue = musicQueue;
return dl;
}
void enqueue(std::string query) {
std::thread th(&bumbleBee::AsyncDownloadManager::enqueueAsyncDL, this, query);
th.detach();
}
private:
AsyncDownloadManager(){
for (int i=0; i<WORKER_COUNT; i++) {
std::thread th(&bumbleBee::AsyncDownloadManager::downloadWorker, &(*this));
th.detach();
AsyncDownloadManager(int worker_count){
worker_thread.reserve(worker_count);
terminate = false;
for (int i=0; i<worker_count; i++) {
worker_thread.emplace_back([this](){this->downloadWorker();});
}
}
~AsyncDownloadManager(){
terminate = true;
dlQueueCondition.notify_all();
for (auto& t : worker_thread) {
t.join();
}
}
~AsyncDownloadManager(){}
AsyncDownloadManager(const AsyncDownloadManager& ref) = delete;
AsyncDownloadManager& operator=(const AsyncDownloadManager& ref) = delete;
void enqueueAsyncDL(std::string query);
void downloadWorker();
@@ -34,6 +47,10 @@ private:
std::queue<std::string> downloadQueue;
std::condition_variable dlQueueCondition;
std::mutex dlQueueMutex;
std::weak_ptr<dpp::cluster> bot;
std::weak_ptr<bumbleBee::MusicQueue> musicQueue;
std::vector<std::thread> worker_thread;
bool terminate;
};
}