network fixes

This commit is contained in:
fluttershy 2025-02-19 18:04:49 +05:00
parent 227c77604f
commit 84844cee6c
4 changed files with 13 additions and 17 deletions

View File

@ -10,7 +10,7 @@ public:
Discord(const nlohmann::json& data) : data(data), net(std::make_tuple(std::make_unique<Args>(data)...)) {} Discord(const nlohmann::json& data) : data(data), net(std::make_tuple(std::make_unique<Args>(data)...)) {}
template<class T> template<class T>
_deprecated_t("Use ctx() instead") _deprecated_t("Use ctx() instead")
auto& get() const { auto& get() const {
return *std::get<std::unique_ptr<T>>(net); return *std::get<std::unique_ptr<T>>(net);
} }
const auto& ctx() const { const auto& ctx() const {

View File

@ -1,5 +1,5 @@
#include "network.hpp" #include "network.hpp"
NetworkManager::NetworkManager() : curl(curl_easy_init()), res(CURLE_OK), web(WebSocket::getInstance()) { NetworkManager::NetworkManager() : curl(curl_easy_init(), curl_easy_cleanup), res(CURLE_OK), web(WebSocket::getInstance()) {
Log::create(INFO, NETWORK, "Network init"); Log::create(INFO, NETWORK, "Network init");
curl_global_init(CURL_GLOBAL_DEFAULT); curl_global_init(CURL_GLOBAL_DEFAULT);
if (!curl) { if (!curl) {
@ -8,9 +8,6 @@ NetworkManager::NetworkManager() : curl(curl_easy_init()), res(CURLE_OK), web(We
} }
} }
NetworkManager::~NetworkManager() { NetworkManager::~NetworkManager() {
if (curl) {
curl_easy_cleanup(curl);
}
curl_global_cleanup(); curl_global_cleanup();
} }
auto NetworkManager::WriteCallback(void* contents, unsigned long size, unsigned long nmemb, void* userp) -> unsigned long { auto NetworkManager::WriteCallback(void* contents, unsigned long size, unsigned long nmemb, void* userp) -> unsigned long {
@ -28,19 +25,19 @@ auto NetworkManager::getLatency() const -> unsigned long {
auto NetworkManager::request(const std::string& method, const std::string& path, const std::string& data) -> std::string { auto NetworkManager::request(const std::string& method, const std::string& path, const std::string& data) -> std::string {
std::string response; std::string response;
if (curl) { if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, path.c_str()); curl_easy_setopt(curl.get(), CURLOPT_URL, path.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str()); curl_easy_setopt(curl.get(), CURLOPT_CUSTOMREQUEST, method.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, data.c_str());
curl_slist* headers = nullptr; curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Accept: application/json"); headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, ("Authorization: " + web.getToken()).c_str()); headers = curl_slist_append(headers, ("Authorization: " + web.getToken()).c_str());
headers = curl_slist_append(headers, "User-Agent: DiscordBot"); headers = curl_slist_append(headers, "User-Agent: DiscordBot");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY); curl_easy_setopt(curl.get(), CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY);
res = curl_easy_perform(curl); res = curl_easy_perform(curl.get());
if (res != 0) { if (res != 0) {
Log::create(ERROR, NETWORK, "curl_easy_perform() failed: " + std::string(curl_easy_strerror(res))); Log::create(ERROR, NETWORK, "curl_easy_perform() failed: " + std::string(curl_easy_strerror(res)));
} }

View File

@ -5,7 +5,7 @@
#include <curl/curl.h> #include <curl/curl.h>
class NetworkManager { class NetworkManager {
private: private:
CURL* curl; std::unique_ptr<void, decltype(&curl_easy_cleanup)> curl;
CURLcode res; CURLcode res;
WebSocket& web; WebSocket& web;
NetworkManager(); NetworkManager();

View File

@ -28,10 +28,9 @@ void EventEmitter::once(const std::string& event, const eventHandlers& handler)
} }
void EventEmitter::off(const std::string& event, eventHandlers handler) { void EventEmitter::off(const std::string& event, eventHandlers handler) {
if (auto it = handlers.find(event); it != handlers.cend()) { if (auto it = handlers.find(event); it != handlers.cend()) {
auto& vec = it->second; it->second.erase(std::ranges::remove_if(it->second, [&handler](const eventHandlers& h) {
vec.erase(std::ranges::remove_if(vec, [&handler](const eventHandlers& h) {
return h.target<eventHandlers>() == handler.target<eventHandlers>(); return h.target<eventHandlers>() == handler.target<eventHandlers>();
}).begin(), vec.cend()); }).begin(), it->second.cend());
} }
} }
WebSocket::WebSocket(const std::string& token, int intents, bool isBot) : token(token), intents(intents), isBot(isBot) { WebSocket::WebSocket(const std::string& token, int intents, bool isBot) : token(token), intents(intents), isBot(isBot) {