slap a bunch of shit everywhere until the crashing stops

This commit is contained in:
ouwou
2020-10-05 21:37:07 -04:00
parent cfcb0d4e66
commit 0cdaea97dd
8 changed files with 52 additions and 18 deletions

View File

@@ -249,11 +249,10 @@ void Abaddon::ActionListChannelItemClick(Snowflake id) {
m_main_window->UpdateChatActiveChannel(id); m_main_window->UpdateChatActiveChannel(id);
if (m_channels_requested.find(id) == m_channels_requested.end()) { if (m_channels_requested.find(id) == m_channels_requested.end()) {
m_discord.FetchMessagesInChannel(id, [this, id](const std::vector<Snowflake> &msgs) { m_discord.FetchMessagesInChannel(id, [this, id](const std::vector<Snowflake> &msgs) {
if (msgs.size() > 0) { if (msgs.size() > 0)
m_oldest_listed_message[id] = msgs.back(); m_oldest_listed_message[id] = msgs.back();
m_main_window->UpdateChatWindowContents();
}
m_main_window->UpdateChatWindowContents();
m_channels_requested.insert(id); m_channels_requested.insert(id);
}); });
} else { } else {

View File

@@ -367,7 +367,7 @@ std::string ChatMessageItemContainer::ParseMessageContent(std::string content) {
} }
std::string ChatMessageItemContainer::ParseMentions(std::string content) { std::string ChatMessageItemContainer::ParseMentions(std::string content) {
constexpr static const auto mentions_regex = R"(&lt;@(\d+)&gt;)"; constexpr static const auto mentions_regex = R"(&lt;@!?(\d+)&gt;)";
return RegexReplaceMany(content, mentions_regex, [this](const std::string &idstr) -> std::string { return RegexReplaceMany(content, mentions_regex, [this](const std::string &idstr) -> std::string {
const Snowflake id(idstr); const Snowflake id(idstr);

View File

@@ -5,6 +5,9 @@
DiscordClient::DiscordClient() DiscordClient::DiscordClient()
: m_http(DiscordAPI) : m_http(DiscordAPI)
, m_decompress_buf(InflateChunkSize) { , m_decompress_buf(InflateChunkSize) {
m_msg_dispatch.connect(sigc::mem_fun(*this, &DiscordClient::MessageDispatch));
LoadEventMap(); LoadEventMap();
} }
@@ -396,7 +399,10 @@ void DiscordClient::HandleGatewayMessageRaw(std::string str) {
if (err != Z_OK) { if (err != Z_OK) {
fprintf(stderr, "Error decompressing input buffer %d (%d/%d)\n", err, m_zstream.avail_in, m_zstream.avail_out); fprintf(stderr, "Error decompressing input buffer %d (%d/%d)\n", err, m_zstream.avail_in, m_zstream.avail_out);
} else { } else {
HandleGatewayMessage(std::string(m_decompress_buf.begin(), m_decompress_buf.begin() + m_zstream.total_out)); m_msg_mutex.lock();
m_msg_queue.push(std::string(m_decompress_buf.begin(), m_decompress_buf.begin() + m_zstream.total_out));
m_msg_dispatch.emit();
m_msg_mutex.unlock();
if (m_decompress_buf.size() > InflateChunkSize) if (m_decompress_buf.size() > InflateChunkSize)
m_decompress_buf.resize(InflateChunkSize); m_decompress_buf.resize(InflateChunkSize);
} }
@@ -407,6 +413,14 @@ void DiscordClient::HandleGatewayMessageRaw(std::string str) {
m_compressed_buf.clear(); m_compressed_buf.clear();
} }
void DiscordClient::MessageDispatch() {
m_msg_mutex.lock();
auto msg = m_msg_queue.front();
m_msg_queue.pop();
m_msg_mutex.unlock();
HandleGatewayMessage(msg);
}
void DiscordClient::HandleGatewayMessage(std::string str) { void DiscordClient::HandleGatewayMessage(std::string str) {
GatewayMessage m; GatewayMessage m;
try { try {

View File

@@ -11,6 +11,8 @@
#include <unordered_set> #include <unordered_set>
#include <mutex> #include <mutex>
#include <zlib.h> #include <zlib.h>
#include <glibmm.h>
#include <queue>
// bruh // bruh
#ifdef GetMessage #ifdef GetMessage
@@ -147,6 +149,11 @@ private:
HeartbeatWaiter m_heartbeat_waiter; HeartbeatWaiter m_heartbeat_waiter;
std::atomic<bool> m_heartbeat_acked = true; std::atomic<bool> m_heartbeat_acked = true;
mutable std::mutex m_msg_mutex;
Glib::Dispatcher m_msg_dispatch;
std::queue<std::string> m_msg_queue;
void MessageDispatch();
// signals // signals
public: public:
typedef sigc::signal<void> type_signal_gateway_ready; typedef sigc::signal<void> type_signal_gateway_ready;

View File

@@ -2,7 +2,9 @@
//#define USE_LOCAL_PROXY //#define USE_LOCAL_PROXY
HTTPClient::HTTPClient(std::string api_base) HTTPClient::HTTPClient(std::string api_base)
: m_api_base(api_base) {} : m_api_base(api_base) {
m_dispatcher.connect(sigc::mem_fun(*this, &HTTPClient::RunCallbacks));
}
void HTTPClient::SetAuth(std::string auth) { void HTTPClient::SetAuth(std::string auth) {
m_authorization = auth; m_authorization = auth;
@@ -98,10 +100,20 @@ void HTTPClient::CleanupFutures() {
} }
} }
void HTTPClient::RunCallbacks() {
m_mutex.lock();
m_queue.front()();
m_queue.pop();
m_mutex.unlock();
}
void HTTPClient::OnResponse(cpr::Response r, std::function<void(cpr::Response r)> cb) { void HTTPClient::OnResponse(cpr::Response r, std::function<void(cpr::Response r)> cb) {
CleanupFutures(); CleanupFutures();
try { try {
cb(r); m_mutex.lock();
m_queue.push([this, r, cb] { cb(r); });
m_dispatcher.emit();
m_mutex.unlock();
} catch (std::exception &e) { } catch (std::exception &e) {
fprintf(stderr, "error handling response (%s, code %d): %s\n", r.url.c_str(), r.status_code, e.what()); fprintf(stderr, "error handling response (%s, code %d): %s\n", r.url.c_str(), r.status_code, e.what());
} }

View File

@@ -5,14 +5,9 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <memory> #include <memory>
#include <mutex>
template<typename F> #include <queue>
void fire_and_forget(F &&func) { #include <glibmm.h>
auto ptr = std::make_shared<std::future<void>>();
*ptr = std::async(std::launch::async, [ptr, func]() {
func();
});
}
class HTTPClient { class HTTPClient {
public: public:
@@ -28,6 +23,11 @@ private:
void OnResponse(cpr::Response r, std::function<void(cpr::Response r)> cb); void OnResponse(cpr::Response r, std::function<void(cpr::Response r)> cb);
void CleanupFutures(); void CleanupFutures();
mutable std::mutex m_mutex;
Glib::Dispatcher m_dispatcher;
std::queue<std::function<void()>> m_queue;
void RunCallbacks();
std::vector<std::future<void>> m_futures; std::vector<std::future<void>> m_futures;
std::string m_api_base; std::string m_api_base;
std::string m_authorization; std::string m_authorization;

View File

@@ -66,7 +66,7 @@ std::string Cache::GetPathIfCached(std::string url) {
// this just seems really yucky // this just seems really yucky
void Cache::CleanupFutures() { void Cache::CleanupFutures() {
for (auto it = m_futures.begin(); it != m_futures.end();) { for (auto it = m_futures.begin(); it != m_futures.end();) {
if (it->wait_for(std::chrono::seconds(0)) == std::future_status::ready) if (it->valid() && it->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
it = m_futures.erase(it); it = m_futures.erase(it);
else else
it++; it++;

View File

@@ -214,9 +214,11 @@ std::string RegexReplaceMany(std::string str, std::string regexstr, F func) {
offset += diff; offset += diff;
if (it + 1 != matches.end()) {
std::get<0>(*(it + 1)) -= offset; std::get<0>(*(it + 1)) -= offset;
std::get<1>(*(it + 1)) -= offset; std::get<1>(*(it + 1)) -= offset;
} }
}
return str; return str;
} }