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);
if (m_channels_requested.find(id) == m_channels_requested.end()) {
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_main_window->UpdateChatWindowContents();
}
m_main_window->UpdateChatWindowContents();
m_channels_requested.insert(id);
});
} else {

View File

@@ -367,7 +367,7 @@ std::string ChatMessageItemContainer::ParseMessageContent(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 {
const Snowflake id(idstr);

View File

@@ -5,6 +5,9 @@
DiscordClient::DiscordClient()
: m_http(DiscordAPI)
, m_decompress_buf(InflateChunkSize) {
m_msg_dispatch.connect(sigc::mem_fun(*this, &DiscordClient::MessageDispatch));
LoadEventMap();
}
@@ -396,7 +399,10 @@ void DiscordClient::HandleGatewayMessageRaw(std::string str) {
if (err != Z_OK) {
fprintf(stderr, "Error decompressing input buffer %d (%d/%d)\n", err, m_zstream.avail_in, m_zstream.avail_out);
} 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)
m_decompress_buf.resize(InflateChunkSize);
}
@@ -407,6 +413,14 @@ void DiscordClient::HandleGatewayMessageRaw(std::string str) {
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) {
GatewayMessage m;
try {

View File

@@ -11,6 +11,8 @@
#include <unordered_set>
#include <mutex>
#include <zlib.h>
#include <glibmm.h>
#include <queue>
// bruh
#ifdef GetMessage
@@ -147,6 +149,11 @@ private:
HeartbeatWaiter m_heartbeat_waiter;
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
public:
typedef sigc::signal<void> type_signal_gateway_ready;

View File

@@ -2,7 +2,9 @@
//#define USE_LOCAL_PROXY
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) {
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) {
CleanupFutures();
try {
cb(r);
m_mutex.lock();
m_queue.push([this, r, cb] { cb(r); });
m_dispatcher.emit();
m_mutex.unlock();
} catch (std::exception &e) {
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 <unordered_map>
#include <memory>
template<typename F>
void fire_and_forget(F &&func) {
auto ptr = std::make_shared<std::future<void>>();
*ptr = std::async(std::launch::async, [ptr, func]() {
func();
});
}
#include <mutex>
#include <queue>
#include <glibmm.h>
class HTTPClient {
public:
@@ -28,6 +23,11 @@ private:
void OnResponse(cpr::Response r, std::function<void(cpr::Response r)> cb);
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::string m_api_base;
std::string m_authorization;

View File

@@ -66,7 +66,7 @@ std::string Cache::GetPathIfCached(std::string url) {
// this just seems really yucky
void Cache::CleanupFutures() {
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);
else
it++;

View File

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