12 #include "aegis/config.hpp"
13 #include "aegis/utility.hpp"
14 #include "aegis/snowflake.hpp"
15 #include "aegis/error.hpp"
16 #include "aegis/shards/shard.hpp"
17 #include <websocketpp/config/asio_client.hpp>
18 #include <websocketpp/common/connection_hdl.hpp>
19 #include <websocketpp/client.hpp>
20 #include "spdlog/spdlog.h"
26 #include <asio/bind_executor.hpp>
34 using json = nlohmann::json;
35 using namespace std::literals;
45 using websocket = websocketpp::client<websocketpp::config::asio_tls_client>;
48 using connection_ptr = websocketpp::client<websocketpp::config::asio_tls_client>::connection_type::ptr;
51 using message_ptr = websocketpp::config::asio_client::message_type::ptr;
63 AEGIS_DECL
shard_mgr(std::string token, asio::io_context & _io, std::shared_ptr<spdlog::logger> log);
76 AEGIS_DECL
void setup_callbacks(
shard * _shard) noexcept;
83 AEGIS_DECL
void debug_trace(
shard * _shard,
bool extended =
false) noexcept;
89 asio::io_context & get_io_context()
96 AEGIS_DECL
void shutdown();
109 bot_status
get_state() const noexcept {
return _status; }
122 AEGIS_DECL std::string uptime() const noexcept;
128 AEGIS_DECL
void send_all_shards(const std::
string & msg);
134 AEGIS_DECL
void send_all_shards(const json & msg);
137 AEGIS_DECL
void start();
140 using
t_on_message = std::function<
void(websocketpp::connection_hdl hdl, std::
string msg,
shard * _shard)>;
144 using
t_on_close = std::function<
void(websocketpp::connection_hdl hdl,
shard * _shard)>;
198 AEGIS_DECL
void reset_shard(
shard * _shard, shard_status _status = shard_status::closing) noexcept;
204 AEGIS_DECL
void queue_reconnect(
shard * _shard) noexcept;
210 void queue_reconnect(
shard & _shard)
212 queue_reconnect(&_shard);
219 AEGIS_DECL
void connect(
shard * _shard) noexcept;
227 AEGIS_DECL
shard & get_shard(uint16_t shard_id);
233 AEGIS_DECL
const std::vector<std::unique_ptr<shard>> &
get_shards() const noexcept
246 AEGIS_DECL
void close(
shard * _shard, int32_t code = 1001,
const std::string & reason =
"", shard_status connection_state = shard_status::closing) noexcept;
256 void close(
shard & _shard, int32_t code = 1001, const std::
string & reason = "", shard_status connection_state = shard_status::closing) noexcept
258 close(&_shard, code, reason, connection_state);
267 return _shards.size();
284 std::shared_ptr<spdlog::logger>
log;
289 std::chrono::time_point<std::chrono::steady_clock> _last_ready;
290 std::chrono::time_point<std::chrono::steady_clock> _last_identify;
291 std::chrono::time_point<std::chrono::steady_clock> _connect_time;
292 shard * _connecting_shard;
294 std::vector<std::unique_ptr<shard>> _shards;
300 std::function<void(aegis::shards::shard*)> i_shard_disconnect;
301 std::function<void(aegis::shards::shard*)> i_shard_connect;
303 AEGIS_DECL
void _on_message(websocketpp::connection_hdl hdl,
message_ptr msg,
shard * _shard);
304 AEGIS_DECL
void _on_connect(websocketpp::connection_hdl hdl,
shard * _shard);
305 AEGIS_DECL
void _on_close(websocketpp::connection_hdl hdl,
shard * _shard);
306 AEGIS_DECL
void ws_status(
const asio::error_code & ec);
308 std::chrono::steady_clock::time_point starttime;
311 std::string gateway_url;
321 std::shared_ptr<asio::steady_timer> ws_timer;
322 std::shared_ptr<asio::steady_timer> ws_connect_timer;
323 std::deque<shard*> _shards_to_connect;
330 #if defined(AEGIS_HEADER_ONLY)
331 #include "aegis/shards/impl/shard_mgr.cpp"
websocketpp::client< websocketpp::config::asio_tls_client > websocket
Type of a pointer to the Websocket++ client.
Definition: shard_mgr.hpp:45
void set_gateway_url(const std::string &url) noexcept
Set the gateway url the shards will connect to.
Definition: shard_mgr.hpp:180
void set_state(bot_status s) noexcept
Set the current shard manager state.
Definition: shard_mgr.hpp:116
std::unordered_map< std::string, uint64_t > message_count
Websocket event message counters.
Definition: shard_mgr.hpp:277
std::function< void(websocketpp::connection_hdl hdl, shard *_shard)> t_on_connect
Websocket on_connect handler type.
Definition: shard_mgr.hpp:142
void set_on_close(t_on_close cb) noexcept
Set handler for websocket disconnections.
Definition: shard_mgr.hpp:171
std::function< void(websocketpp::connection_hdl hdl, shard *_shard)> t_on_close
Websocket on_close handler type.
Definition: shard_mgr.hpp:144
Primary class for managing a bot interface.
Definition: core.hpp:157
uint32_t shard_count() const noexcept
Get the amount of shards that exist.
Definition: shard_mgr.hpp:265
bot_status get_state() const noexcept
Get the current state of the shard manager.
Definition: shard_mgr.hpp:109
void set_on_connect(t_on_connect cb) noexcept
Set handler for websocket connections.
Definition: shard_mgr.hpp:161
Primary class for managing a bot interface.
Definition: shard_mgr.hpp:41
uint32_t force_shard_count
Shard count to force manager to use.
Definition: shard_mgr.hpp:280
std::string ws_gateway
Gateway URL.
Definition: shard_mgr.hpp:274
std::shared_ptr< spdlog::logger > log
Logging instance.
Definition: shard_mgr.hpp:284
std::string get_gateway_url() const noexcept
Get the gateway url the shards will connect to.
Definition: shard_mgr.hpp:189
websocketpp::config::asio_client::message_type::ptr message_ptr
Type of a pointer to the Websocket++ message payload.
Definition: shard_mgr.hpp:51
uint32_t shard_max_count
Shard count retrieved from gateway.
Definition: shard_mgr.hpp:282
asio::io_context & _io_context
Asio context.
Definition: shard_mgr.hpp:271
websocket & get_websocket() noexcept
Get the internal Websocket++ instance.
Definition: shard_mgr.hpp:102
websocketpp::client< websocketpp::config::asio_tls_client >::connection_type::ptr connection_ptr
Type of a pointer to the Websocket++ TLS connection.
Definition: shard_mgr.hpp:48
const std::vector< std::unique_ptr< shard > > & get_shards() const noexcept
Get a const vector of all the shards.
Definition: shard_mgr.hpp:233
std::function< void(websocketpp::connection_hdl hdl, std::string msg, shard *_shard)> t_on_message
Websocket on_message handler type.
Definition: shard_mgr.hpp:140
Tracks websocket shards and their connections.
Definition: shard.hpp:42