summaryrefslogtreecommitdiff
path: root/discord
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2021-07-12 19:06:00 -0400
committerouwou <26526779+ouwou@users.noreply.github.com>2021-07-12 19:06:00 -0400
commitf60e2cd6bd0ebbb0dfc9ce4f599a5daabf646b90 (patch)
tree4fdf02d17dc525c5e6dad4664598e185ec0d018a /discord
parentecf8fb6a5f0cdd674c79fec78698aa66099f5fc7 (diff)
parentccf7c414be476d9be601a3dfe0e0030dedc0a91c (diff)
downloadabaddon-portaudio-f60e2cd6bd0ebbb0dfc9ce4f599a5daabf646b90.tar.gz
abaddon-portaudio-f60e2cd6bd0ebbb0dfc9ce4f599a5daabf646b90.zip
Merge branch 'master' into channels-list
Diffstat (limited to 'discord')
-rw-r--r--discord/discord.cpp48
-rw-r--r--discord/discord.hpp24
2 files changed, 42 insertions, 30 deletions
diff --git a/discord/discord.cpp b/discord/discord.cpp
index 78036ad..fab2c49 100644
--- a/discord/discord.cpp
+++ b/discord/discord.cpp
@@ -24,6 +24,8 @@ DiscordClient::DiscordClient(bool mem_store)
}
void DiscordClient::Start() {
+ if (m_client_started) return;
+
m_http.SetBase(GetAPIURL());
std::memset(&m_zstream, 0, sizeof(m_zstream));
@@ -32,27 +34,31 @@ void DiscordClient::Start() {
m_last_sequence = -1;
m_heartbeat_acked = true;
m_client_connected = true;
+ m_client_started = true;
m_websocket.StartConnection(GetGatewayURL());
}
void DiscordClient::Stop() {
- if (!m_client_connected) return;
+ if (m_client_started) {
+ inflateEnd(&m_zstream);
+ m_compressed_buf.clear();
- inflateEnd(&m_zstream);
- m_compressed_buf.clear();
+ m_heartbeat_waiter.kill();
+ if (m_heartbeat_thread.joinable()) m_heartbeat_thread.join();
+ m_client_connected = false;
+ m_reconnecting = false;
- m_heartbeat_waiter.kill();
- if (m_heartbeat_thread.joinable()) m_heartbeat_thread.join();
- m_client_connected = false;
+ m_store.ClearAll();
+ m_guild_to_users.clear();
- m_store.ClearAll();
- m_guild_to_users.clear();
+ m_websocket.Stop();
+ }
- m_websocket.Stop();
+ m_client_started = false;
}
bool DiscordClient::IsStarted() const {
- return m_client_connected;
+ return m_client_started;
}
bool DiscordClient::IsStoreValid() const {
@@ -233,19 +239,19 @@ std::optional<RoleData> DiscordClient::GetMemberHighestRole(Snowflake guild_id,
});
}
-std::unordered_set<Snowflake> DiscordClient::GetUsersInGuild(Snowflake id) const {
+std::set<Snowflake> DiscordClient::GetUsersInGuild(Snowflake id) const {
auto it = m_guild_to_users.find(id);
if (it != m_guild_to_users.end())
return it->second;
- return std::unordered_set<Snowflake>();
+ return {};
}
-std::unordered_set<Snowflake> DiscordClient::GetChannelsInGuild(Snowflake id) const {
+std::set<Snowflake> DiscordClient::GetChannelsInGuild(Snowflake id) const {
auto it = m_guild_to_channels.find(id);
if (it != m_guild_to_channels.end())
return it->second;
- return std::unordered_set<Snowflake>();
+ return {};
}
bool DiscordClient::HasGuildPermission(Snowflake user_id, Snowflake guild_id, Permission perm) const {
@@ -953,12 +959,12 @@ PresenceStatus DiscordClient::GetUserStatus(Snowflake id) const {
return PresenceStatus::Offline;
}
-std::unordered_map<Snowflake, RelationshipType> DiscordClient::GetRelationships() const {
+std::map<Snowflake, RelationshipType> DiscordClient::GetRelationships() const {
return m_user_relationships;
}
-std::unordered_set<Snowflake> DiscordClient::GetRelationships(RelationshipType type) const {
- std::unordered_set<Snowflake> ret;
+std::set<Snowflake> DiscordClient::GetRelationships(RelationshipType type) const {
+ std::set<Snowflake> ret;
for (const auto &[id, rtype] : m_user_relationships)
if (rtype == type)
ret.insert(id);
@@ -1682,7 +1688,8 @@ void DiscordClient::HandleGatewayInvalidSession(const GatewayMessage &msg) {
m_websocket.Stop(1000);
- m_websocket.StartConnection(GetGatewayURL());
+ if (m_client_started)
+ Glib::signal_timeout().connect_once([this] { if (m_client_started) m_websocket.StartConnection(GetGatewayURL()); }, 1000);
}
bool IsCompleteMessageObject(const nlohmann::json &j) {
@@ -1886,6 +1893,11 @@ void DiscordClient::HandleSocketClose(uint16_t code) {
m_store.ClearAll();
m_guild_to_users.clear();
+ if (m_client_started && !m_reconnecting && close_code == GatewayCloseCode::Abnormal) {
+ Glib::signal_timeout().connect_once([this] { if (m_client_started) HandleGatewayReconnect(GatewayMessage()); }, 1000);
+ m_reconnecting = true;
+ }
+
m_signal_disconnected.emit(m_reconnecting, close_code);
};
m_generic_mutex.lock();
diff --git a/discord/discord.hpp b/discord/discord.hpp
index cfce016..918b1cb 100644
--- a/discord/discord.hpp
+++ b/discord/discord.hpp
@@ -6,9 +6,8 @@
#include <sigc++/sigc++.h>
#include <nlohmann/json.hpp>
#include <thread>
-#include <unordered_map>
+#include <map>
#include <set>
-#include <unordered_set>
#include <mutex>
#include <zlib.h>
#include <glibmm.h>
@@ -85,8 +84,8 @@ public:
std::optional<BanData> GetBan(Snowflake guild_id, Snowflake user_id) const;
Snowflake GetMemberHoistedRole(Snowflake guild_id, Snowflake user_id, bool with_color = false) const;
std::optional<RoleData> GetMemberHighestRole(Snowflake guild_id, Snowflake user_id) const;
- std::unordered_set<Snowflake> GetUsersInGuild(Snowflake id) const;
- std::unordered_set<Snowflake> GetChannelsInGuild(Snowflake id) const;
+ std::set<Snowflake> GetUsersInGuild(Snowflake id) const;
+ std::set<Snowflake> GetChannelsInGuild(Snowflake id) const;
bool HasGuildPermission(Snowflake user_id, Snowflake guild_id, Permission perm) const;
@@ -184,8 +183,8 @@ public:
PresenceStatus GetUserStatus(Snowflake id) const;
- std::unordered_map<Snowflake, RelationshipType> GetRelationships() const;
- std::unordered_set<Snowflake> GetRelationships(RelationshipType type) const;
+ std::map<Snowflake, RelationshipType> GetRelationships() const;
+ std::set<Snowflake> GetRelationships(RelationshipType type) const;
std::optional<RelationshipType> GetRelationship(Snowflake id) const;
private:
@@ -255,14 +254,14 @@ private:
std::string m_token;
void AddUserToGuild(Snowflake user_id, Snowflake guild_id);
- std::unordered_map<Snowflake, std::unordered_set<Snowflake>> m_guild_to_users;
+ std::map<Snowflake, std::set<Snowflake>> m_guild_to_users;
- std::unordered_map<Snowflake, std::unordered_set<Snowflake>> m_guild_to_channels;
- std::unordered_map<Snowflake, GuildApplicationData> m_guild_join_requests;
+ std::map<Snowflake, std::set<Snowflake>> m_guild_to_channels;
+ std::map<Snowflake, GuildApplicationData> m_guild_join_requests;
- std::unordered_map<Snowflake, PresenceStatus> m_user_to_status;
+ std::map<Snowflake, PresenceStatus> m_user_to_status;
- std::unordered_map<Snowflake, RelationshipType> m_user_relationships;
+ std::map<Snowflake, RelationshipType> m_user_relationships;
UserData m_user_data;
UserSettings m_user_settings;
@@ -272,8 +271,9 @@ private:
Websocket m_websocket;
std::atomic<bool> m_client_connected = false;
std::atomic<bool> m_ready_received = false;
+ bool m_client_started = false;
- std::unordered_map<std::string, GatewayEvent> m_event_map;
+ std::map<std::string, GatewayEvent> m_event_map;
void LoadEventMap();
std::thread m_heartbeat_thread;