Reduced compiletime

This commit is contained in:
Rebekah 2019-06-11 06:13:02 -04:00
parent d8aeec28a2
commit c9d10bb1f7
4 changed files with 26 additions and 29 deletions

View File

@ -41,7 +41,7 @@ public:
enum class State { enum class State {
kReady, // We recieved our READY event kReady, // We recieved our READY event
kNearly, // We sent our IDENTITY, waiting for ready kHello, // We sent our IDENTITY, waiting for ready
kConnecting, // Trying to connect to the socket kConnecting, // Trying to connect to the socket
kReconnecting, // Got disconnected, trying again kReconnecting, // Got disconnected, trying again
kDisconnected, // We are not connected at all kDisconnected, // We are not connected at all

View File

@ -22,13 +22,15 @@
#include <rapidjson/document.h> #include <rapidjson/document.h>
#include "api/rest.hpp" #include "api/rest.hpp"
#include "api/shard.hpp"
#include "presence.hpp" #include "presence.hpp"
#include "snowflake.hpp" #include "snowflake.hpp"
namespace neko::discord { namespace neko::discord {
namespace json = rapidjson; namespace json = rapidjson;
namespace api {
class Shard;
}
class Guild; class Guild;
class GuildMember; class GuildMember;
class Message; class Message;
@ -41,6 +43,8 @@ public:
BaseClient(); BaseClient();
void Login(std::string_view token, int num_shards = 1); void Login(std::string_view token, int num_shards = 1);
void Disconnect(); void Disconnect();
void GetToken();
// Events // Events
protected: protected:
virtual void onReady(){} virtual void onReady(){}
@ -49,29 +53,28 @@ protected:
virtual void onChannelCreate(Channel*){} virtual void onChannelCreate(Channel*){}
virtual void onGuildUpdate(Guild*) {} virtual void onGuildUpdate(Guild*) {}
virtual void onDisconnect() {} virtual void onDisconnect() {}
// Info retrieval // Info retrieval
public: public:
const User* FetchClientUser(); const User* FetchClientUser();
//Invite FetchInvite(std::string_view);
User* FetchUser(Snowflake id, bool cache = true); // Whether or not to use the cache User* FetchUser(Snowflake id, bool cache = true); // Whether or not to use the cache
Guild* FetchGuild(Snowflake id, bool cache = true); Guild* FetchGuild(Snowflake id, bool cache = true);
Emoji* FetchEmoji(Snowflake id); Emoji* FetchEmoji(Snowflake id);
Channel* FetchChannel(Snowflake id); Channel* FetchChannel(Snowflake id);
//std::vector<VoiceRegion> FetchVoiceRegions(); // This fetch user takes in data and caches it
//Webhook FetchWebhook(Snowflake id, std::string_view = std::string_view()); User* FetchUser(Snowflake, const json::Value&);
//std::string GenerateInvite() User* FetchUser(const json::Value& v);
//void SyncGuilds(); Guild* FetchGuild(Snowflake, const json::Value&);
Guild* FetchGuild(const json::Value& v);
// Internal events // Internal events
private: private:
// Cache
void onReady(const json::Value&); void onReady(const json::Value&);
void onGuildCreate(const json::Value&); void onGuildCreate(const json::Value&);
void onMessageCreate(const json::Value&); void onMessageCreate(const json::Value&);
void onChannelCreate(const json::Value&); void onChannelCreate(const json::Value&);
void onGuildUpdate(const json::Value&); void onGuildUpdate(const json::Value&);
// Cache
User* user; User* user;
std::unordered_map<Snowflake, Channel*> channels; std::unordered_map<Snowflake, Channel*> channels;
std::unordered_map<Snowflake, Emoji*> emojis; std::unordered_map<Snowflake, Emoji*> emojis;
@ -79,28 +82,18 @@ private:
std::unordered_map<Snowflake, Presence> presences; std::unordered_map<Snowflake, Presence> presences;
std::unordered_map<Snowflake, User*> users; std::unordered_map<Snowflake, User*> users;
friend Guild; // So they can fill the cache themselves // Connection info
friend Channel;
friend DMChannel;
friend GroupDMChannel;
friend Emoji;
friend GuildMember;
friend Message;
// This fetch user takes in data and caches it
User* FetchUser(Snowflake, const json::Value&);
User* FetchUser(const json::Value& v);
Guild* FetchGuild(Snowflake, const json::Value&);
Guild* FetchGuild(const json::Value& v);
// Connection info
std::string token; std::string token;
friend api::RestAPI; friend api::RestAPI;
api::RestAPI http; api::RestAPI http;
friend api::Shard; friend api::Shard;
std::vector<api::Shard*> shards; std::vector<api::Shard*> shards;
void EmitEvent(std::string_view event, const json::Value& msg); void EmitEvent(std::string_view event, const json::Value& data);
// easy access
friend Channel;
friend Guild;
friend Emoji;
friend GuildMember;
}; };
} }

View File

@ -22,6 +22,8 @@
#include "client.hpp" #include "client.hpp"
#include "api/shard.hpp"
namespace neko::discord::api { namespace neko::discord::api {
using namespace std::string_view_literals; using namespace std::string_view_literals;
using namespace std::chrono_literals; using namespace std::chrono_literals;
@ -156,7 +158,7 @@ void Shard::Send(const json::Value& msg) {
void Shard::Heartbeat() { void Shard::Heartbeat() {
// Check for acknowledge // Check for acknowledge
if (!this->last_heartbeat_ack) { if (this->state == State::kReady && !this->last_heartbeat_ack) {
std::cerr << "Shard: Heartbeat not acknowledged, reconnecting!" << std::endl; std::cerr << "Shard: Heartbeat not acknowledged, reconnecting!" << std::endl;
this->Disconnect(true); this->Disconnect(true);
return; return;
@ -252,13 +254,14 @@ void Shard::RecieveMessage(std::string_view raw) {
}, hb_int); }, hb_int);
} }
this->state = State::kNearly; this->state = State::kHello;
if(!this->session_id.empty()) { if(!this->session_id.empty()) {
this->Resume(); this->Resume();
} else { } else {
this->Identify(); this->Identify();
} }
this->last_heartbeat_ack = true;
this->Heartbeat(); this->Heartbeat();
break; break;
} }

View File

@ -22,6 +22,7 @@
#include <neko/filesystem.hpp> #include <neko/filesystem.hpp>
#include <rapidjson/writer.h> #include <rapidjson/writer.h>
#include "api/shard.hpp"
#include "guild/guild.hpp" #include "guild/guild.hpp"
#include "channel.hpp" #include "channel.hpp"
#include "message.hpp" #include "message.hpp"