8#ifndef DISCORD_INTERNAL_H
9#define DISCORD_INTERNAL_H
26#include "user-agent.h"
39#define NOT_EMPTY_STR(_str) ((_str) && *(_str))
47#define CONTAINEROF(_ptr, _type, _path) \
48 ((_type *)((char *)(_ptr) - offsetof(_type, _path)))
60#define CLIENT(_ptr, _path) CONTAINEROF(_ptr, struct discord, _path)
71#define CCORD_EXPECT(_client, _expect, _code, _reason) \
74 logmod_log(ERROR, (_client)->logger, \
75 "Expected: " #_expect " | %s (%s)", \
76 discord_strerror(_code, _client), \
77 discord_code_as_string(_code)); \
91#define CCORD_DATA_TO_JSON(_client, _type, _body, _params) \
93 struct reflectc_wrap *w_params = \
94 reflectc_from_##_type((_client)->registry, _params, NULL); \
95 CCORDcode code = discord_data_wrap_to_json(w_params, &(_body)->start, \
97 reflectc_cleanup((_client)->registry, w_params); \
98 if (code != CCORD_OK) { \
99 logmod_log(ERROR, (_client)->logger, \
100 "Expected: CCORD_OK == discord_data_wrap_to_json | " \
102 discord_strerror(code, _client), \
103 discord_code_as_string(code)); \
109#define DISCORD_ENDPT_LEN 512
111#define DISCORD_ROUTE_LEN 256
119 struct io_poller *
io;
137 struct io_poller *io);
229#define DISCORD_BUCKET_TIMEOUT (void *)(0xf)
292 const char endpoint_fmt[],
307 struct ua_info *info);
440 struct reflectc_wrap *(*init)(
struct reflectc *registry,
442 struct reflectc_wrap *root);
449#define DISCORD_ATTRIBUTES_FIELDS \
451 struct discord_ret_dispatch dispatch; \
453 struct discord_ret_response response; \
455 struct discord_attachments attachments; \
485 struct ua_conn *
conn;
493 pthread_cond_t *
cond;
503 struct user_agent *
ua;
603 const enum http_method method,
663 const enum http_method method,
664 const char endpoint_fmt[],
685#define DISCORD_SESSION_OFFLINE 0u
687#define DISCORD_SESSION_RESUMABLE 1u << 0
689#define DISCORD_SESSION_SHUTDOWN 1u << 1
739 enum discord_gateway_opcodes
opcode;
747 const jsmnf_pair *
data;
754 const struct discord_message *event);
811 struct discord_identify
id;
894 struct discord_identify *
event);
903 struct discord_resume *
event);
972 struct _discord_ref *
refs;
997 void (*cleanup)(
void *data),
1011 void (*cleanup)(
struct discord *client,
1103 struct _discord_message_commands_entry *
entries;
1131 const char command[],
1143 const char command[],
1158 const char prefix[],
1189 const struct discord_identify *ident,
1193 const struct discord_identify *ident);
1196 const struct discord_identify *ident);
1239 struct discord_user
self;
1264 pthread_mutex_t
lock;
#define PRINTF_LIKE(a, b)
Definition: attributes.h:9
int CCORDcode
Concord error codes.
Definition: concord-error.h:11
uint64_t u64unix_ms
Unix time in milliseconds.
Definition: types.h:22
void discord_update_voice_state(struct discord *client, struct discord_update_voice_state *update)
Sent when a client wants to join, move or disconnect from a voice channel.
void discord_request_guild_members(struct discord *client, struct discord_request_guild_members *request)
Request all members for a guild or a list of guilds.
discord_gateway_events
Discord Gateway's events.
Definition: discord-events.h:54
enum discord_event_scheduler(* discord_ev_scheduler)(struct discord *client, const char data[], size_t size, enum discord_gateway_events event)
Event Handling Mode callback.
Definition: discord-events.h:141
@ DISCORD_EV_MAX
Definition: discord-events.h:118
void(* discord_ev_message)(struct discord *client, const struct discord_message *event)
Definition: discord-internal.h:752
void discord_gateway_reconnect(struct discord_gateway *gw, bool resume)
Gracefully reconnect a ongoing Discord connection over WebSockets.
void discord_gateway_send_update_voice_state(struct discord_gateway *gw, struct discord_update_voice_state *event)
Sent when a client wants to join, move or disconnect from a voice channel.
CCORDcode discord_gateway_start(struct discord_gateway *gw)
Initialize handle with the new session primitives.
void discord_gateway_dispatch(struct discord_gateway *gw)
Dispatch user callback matched to event.
bool discord_gateway_end(struct discord_gateway *gw)
Cleanup and reset gw session primitives.
void discord_gateway_shutdown(struct discord_gateway *gw)
Gracefully shutdown a ongoing Discord connection over WebSockets.
CCORDcode discord_gateway_perform(struct discord_gateway *gw)
Check and manage on-going Gateway session.
void discord_gateway_send_resume(struct discord_gateway *gw, struct discord_resume *event)
Replay missed events when a disconnected client resumes.
void discord_gateway_send_request_guild_members(struct discord_gateway *gw, struct discord_request_guild_members *event)
Request all members for a guild or a list of guilds.
void discord_gateway_cleanup(struct discord_gateway *gw)
Free a Gateway handle.
void discord_gateway_send_presence_update(struct discord_gateway *gw, struct discord_presence_update *event)
Send client's presence status update payload.
CCORDcode discord_gateway_init(struct discord_gateway *gw, const char token[])
Initialize a Gateway handle.
void discord_gateway_send_heartbeat(struct discord_gateway *gw, int seq)
Maintain an active gateway connection.
void discord_gateway_send_identify(struct discord_gateway *gw, struct discord_identify *event)
Trigger the initial handshake with the gateway.
void(* discord_ev_event)(struct discord *client, const void *event)
Definition: discord-internal.h:750
CCORDcode discord_message_commands_init(struct discord_message_commands *cmds)
Initialize a Message Commands handle.
void discord_message_commands_set_prefix(struct discord_message_commands *cmds, const char prefix[], size_t length)
Set a mandatory prefix before commands.
void discord_message_commands_append(struct discord_message_commands *cmds, const char command[], size_t length, discord_ev_message callback)
Add a new command/callback pair, or update an existing command.
discord_ev_message discord_message_commands_find(struct discord_message_commands *cmds, const char command[], size_t length)
Search for a callback matching the command.
bool discord_message_commands_try_perform(struct discord_message_commands *cmds, struct discord_gateway_payload *payload)
Read the current DISCORD_EV_MESSAGE_CREATE payload and attempt to perform its matching callback.
void discord_message_commands_cleanup(struct discord_message_commands *cmds)
Free a Message Commands handle.
void discord_ratelimiter_set_global_timeout(struct discord_ratelimiter *rl, struct discord_bucket *bucket, u64unix_ms wait_ms)
Update global ratelimiting value.
void discord_ratelimiter_build(struct discord_ratelimiter *rl, struct discord_bucket *bucket, const char key[], struct ua_info *info)
Update the bucket with response header data.
void discord_ratelimiter_build_key(enum http_method method, char key[256], const char endpoint_fmt[], va_list args)
Build unique key formed from the HTTP method and endpoint.
void discord_bucket_request_unselect(struct discord_ratelimiter *rl, struct discord_bucket *b, struct discord_request *req)
Unselect a request provided at discord_ratelimiter_request_selector()
void discord_bucket_insert(struct discord_ratelimiter *rl, struct discord_bucket *b, struct discord_request *req, bool high_priority)
Insert into bucket's next requests queue.
void discord_ratelimiter_cleanup(struct discord_ratelimiter *rl)
Cleanup all buckets that have been discovered.
void discord_bucket_set_timeout(struct discord_bucket *bucket, u64unix_ms wait_ms)
Set bucket timeout.
struct discord_bucket * discord_bucket_get(struct discord_ratelimiter *rl, const char key[])
Get a struct discord_bucket assigned to key
void discord_bucket_request_selector(struct discord_ratelimiter *rl, void *data, void(*iter)(void *data, struct discord_request *req))
Iterate and select next requests.
CCORDcode discord_ratelimiter_init(struct discord_ratelimiter *rl)
Initialize ratelimiter handle.
CCORDcode discord_requestor_init(struct discord_requestor *rqtor, const char token[])
Initialize the request handler.
void discord_requestor_dispatch_responses(struct discord_requestor *rqtor)
Run pending callbacks from completed requests.
CCORDcode discord_requestor_start_pending(struct discord_requestor *rqtor)
Check for and start pending bucket's requests.
#define DISCORD_ATTRIBUTES_FIELDS
Macro containing discord_attributes fields.
Definition: discord-internal.h:449
CCORDcode discord_requestor_info_read(struct discord_requestor *rqtor)
Poll for request's completion.
CCORDcode discord_request_begin(struct discord_requestor *rqtor, const struct discord_attributes *req, const struct ccord_szbuf *body, const enum http_method method, const char endpoint[512], const char key[256])
Begin a new request.
void discord_request_cancel(struct discord_requestor *rqtor, struct discord_request *req)
Mark request as canceled and move it to the recycling queue.
void discord_requestor_cleanup(struct discord_requestor *rqtor)
Free the request handler.
CCORDcode discord_rest_init(struct discord_rest *rest, const char token[])
Initialize an REST handle.
void discord_rest_cleanup(struct discord_rest *rest)
Free an REST handle.
CCORDcode discord_rest_run(struct discord_rest *rest, const struct discord_attributes *req, const struct ccord_szbuf *body, const enum http_method method, const char endpoint_fmt[],...) PRINTF_LIKE(5
Perform a request to Discord.
CCORDcode void discord_rest_stop_buckets(struct discord_rest *rest)
Stop all bucket's on-going, pending and timed-out requests.
CCORDcode discord_refcounter_unclaim(struct discord_refcounter *rc, void *data)
Unclaim ownership of data
CCORDcode discord_refcounter_claim(struct discord_refcounter *rc, const void *data)
Claim ownership of data
void discord_refcounter_cleanup(struct discord_refcounter *rc)
Cleanup refcounter and all user data currently held.
CCORDcode discord_refcounter_incr(struct discord_refcounter *rc, void *data)
Increment the reference counter for ret->data
void discord_refcounter_add_internal(struct discord_refcounter *rc, void *data, void(*cleanup)(void *data), bool should_free)
Add a new internal reference to the reference counter.
CCORDcode discord_refcounter_decr(struct discord_refcounter *rc, void *data)
Decrement the reference counter for data
CCORDcode discord_refcounter_init(struct discord_refcounter *rc)
Initialize reference counter handle.
void discord_refcounter_add_client(struct discord_refcounter *rc, void *data, void(*cleanup)(struct discord *client, void *data), bool should_free)
Add a new client reference to the reference counter.
int64_t discord_timers_get_next_trigger(struct discord_timers *const timers[], size_t n, int64_t now, int64_t max_time)
Get earliest trigger time from a group of timers.
unsigned discord_internal_timer_ctl(struct discord *client, struct discord_timer *timer)
Modifies or creates a timer.
unsigned _discord_timer_ctl(struct discord *client, struct discord_timers *timers, struct discord_timer *timer)
Modifies or creates a timer.
void discord_timers_cleanup(struct discord *client, struct discord_timers *timers)
Cleanup timers and call cancel any running ones.
void discord_timers_run(struct discord *client, struct discord_timers *timers)
Run all timers that are due.
CCORDcode discord_timers_init(struct discord_timers *timers, struct io_poller *io)
Prepare timers for usage.
unsigned discord_internal_timer(struct discord *client, discord_ev_timer on_tick_cb, discord_ev_timer on_status_changed_cb, void *data, int64_t delay)
Creates a one shot timer that automatically deletes itself upon completion.
#define DISCORD_ROUTE_LEN
Definition: discord-internal.h:111
#define DISCORD_ENDPT_LEN
Definition: discord-internal.h:109
void(* discord_ev_timer)(struct discord *client, struct discord_timer *ev)
callback to be used with struct discord_timer
Definition: discord.h:654
struct priority_queue priority_queue
Definition: priority_queue.h:13
void * QUEUE[2]
Definition: queue.h:21
Read-only generic sized buffer.
Definition: types.h:57
Generic sized buffer.
Definition: types.h:47
Request to be performed.
Definition: discord-internal.h:460
The Discord bucket for handling per-group ratelimits.
Definition: discord-internal.h:322
u64unix_ms reset_tstamp
Definition: discord-internal.h:330
struct discord_request * busy_req
Definition: discord-internal.h:336
long limit
Definition: discord-internal.h:326
long remaining
Definition: discord-internal.h:328
char hash[64]
Definition: discord-internal.h:324
QUEUE entry
Definition: discord-internal.h:344
struct discord_bucket::@4 queues
The Discord Cache control handler.
Definition: discord-internal.h:1182
void(* on_shard_resumed)(struct discord *client, const struct discord_identify *ident)
Definition: discord-internal.h:1191
struct _discord_cache_data * data
Definition: discord-internal.h:1183
void(* on_shard_disconnected)(struct discord *client, const struct discord_identify *ident, bool resumable)
Definition: discord-internal.h:1187
void(* cleanup)(struct discord *client)
Definition: discord-internal.h:1184
void(* on_shard_reconnected)(struct discord *client, const struct discord_identify *ident)
Definition: discord-internal.h:1194
The Discord configuration handler.
Definition: discord.h:250
The handle for storing the Discord response payload.
Definition: discord-internal.h:724
const jsmnf_pair * data
Definition: discord-internal.h:746
size_t ntable
Definition: discord-internal.h:734
struct discord_gateway_payload::@9 json
jsmnf_table * table
Definition: discord-internal.h:732
size_t size
Definition: discord-internal.h:730
int seq
Definition: discord-internal.h:740
char * start
Definition: discord-internal.h:728
enum discord_gateway_events event
Definition: discord-internal.h:744
char name[32]
Definition: discord-internal.h:742
enum discord_gateway_opcodes opcode
Definition: discord-internal.h:738
The handle for storing the Discord Gateway session.
Definition: discord-internal.h:692
char base_url[256]
Definition: discord-internal.h:700
struct discord_session_start_limit start_limit
Definition: discord-internal.h:704
int limit
Definition: discord-internal.h:719
char resume_url[256]
Definition: discord-internal.h:702
int attempt
Definition: discord-internal.h:717
int shards
Definition: discord-internal.h:698
int event_count
Definition: discord-internal.h:708
unsigned status
Definition: discord-internal.h:710
struct discord_gateway_session::@8 retry
int concurrent
Definition: discord-internal.h:706
bool enable
Definition: discord-internal.h:715
bool is_ready
Definition: discord-internal.h:694
The handle used for interfacing with Discord's Gateway API.
Definition: discord-internal.h:756
struct websockets * ws
Definition: discord-internal.h:760
u64unix_ms hbeat_last
Definition: discord-internal.h:786
struct discord_gateway_payload payload
Definition: discord-internal.h:816
discord_ev_scheduler scheduler
Definition: discord-internal.h:825
unsigned hbeat_timer
Definition: discord-internal.h:798
u64unix_ms now
Definition: discord-internal.h:780
struct discord_gateway_session * session
Definition: discord-internal.h:813
int64_t hbeat_interval
Definition: discord-internal.h:770
pthread_rwlock_t rwlock
Definition: discord-internal.h:806
struct discord_gateway::@10 * timer
int ping_ms
Definition: discord-internal.h:804
u64unix_ms identify_last
Definition: discord-internal.h:791
struct logmod_logger * logger
Definition: discord-internal.h:758
u64unix_ms event
Definition: discord-internal.h:796
bool hbeat_acknowledged
Definition: discord-internal.h:775
CURLM * mhandle
Definition: discord-internal.h:762
struct discord_identify id
Definition: discord-internal.h:810
discord_ev_event cbs[2][DISCORD_EV_MAX]
Definition: discord-internal.h:823
The handle for storing user's message commands.
Definition: discord-internal.h:1087
const struct logmod_logger * logger
Definition: discord-internal.h:1089
struct _discord_message_commands_entry * entries
Definition: discord-internal.h:1102
discord_ev_message fallback
Definition: discord-internal.h:1093
int length
Definition: discord-internal.h:1095
struct ccord_szbuf prefix
Definition: discord-internal.h:1091
int capacity
Definition: discord-internal.h:1097
The ratelimiter struct for handling ratelimiting.
Definition: discord-internal.h:235
struct discord_bucket * null
Definition: discord-internal.h:249
struct discord_ratelimiter::@3 queues
u64unix_ms * global_wait_tstamp
Definition: discord-internal.h:255
struct logmod_logger * logger
Definition: discord-internal.h:237
int length
Definition: discord-internal.h:239
struct discord_bucket * miss
Definition: discord-internal.h:252
int capacity
Definition: discord-internal.h:241
struct _discord_route * routes
Definition: discord-internal.h:246
Automatically cleanup user data.
Definition: discord-internal.h:960
pthread_mutex_t * g_lock
Definition: discord-internal.h:973
struct _discord_ref * refs
Definition: discord-internal.h:971
int length
Definition: discord-internal.h:964
int capacity
Definition: discord-internal.h:966
struct logmod_logger * logger
Definition: discord-internal.h:962
Individual requests that are scheduled to run asynchronously.
Definition: discord-internal.h:470
QUEUE entry
Definition: discord-internal.h:494
struct ccord_szbuf body
Definition: discord-internal.h:476
pthread_cond_t * cond
Definition: discord-internal.h:492
enum http_method method
Definition: discord-internal.h:478
char endpoint[512]
Definition: discord-internal.h:480
int retry_attempt
Definition: discord-internal.h:490
char key[256]
Definition: discord-internal.h:482
struct discord_bucket * b
Definition: discord-internal.h:474
CCORDcode code
Definition: discord-internal.h:486
struct ua_conn * conn
Definition: discord-internal.h:484
struct ccord_szbuf_readonly json
Definition: discord-internal.h:488
The handle used for handling asynchronous requests.
Definition: discord-internal.h:498
pthread_mutex_t recycling
Definition: discord-internal.h:527
CURLM * mhandle
Definition: discord-internal.h:504
struct discord_requestor::@7 * qlocks
struct logmod_logger * logger
Definition: discord-internal.h:500
pthread_mutex_t finished
Definition: discord-internal.h:531
pthread_mutex_t pending
Definition: discord-internal.h:529
int retry_limit
Definition: discord-internal.h:509
struct user_agent * ua
Definition: discord-internal.h:502
struct discord_ratelimiter ratelimiter
Definition: discord-internal.h:506
struct discord_requestor::@6 * queues
The response for the completed request.
Definition: discord-response.h:12
The handle used for interfacing with Discord's REST API.
Definition: discord-internal.h:614
struct threadpool_t * tpool
Definition: discord-internal.h:624
struct logmod_logger * logger
Definition: discord-internal.h:616
struct io_poller * io_poller
Definition: discord-internal.h:622
struct discord_requestor requestor
Definition: discord-internal.h:618
struct discord_timers timers
Definition: discord-internal.h:620
Generic request dispatcher.
Definition: discord-internal.h:409
DISCORD_RET_DEFAULT_FIELDS
Definition: discord-internal.h:410
void(* typeless)(struct discord *client, struct discord_response *resp)
Definition: discord-internal.h:423
void * sync
Definition: discord-internal.h:430
bool has_type
Definition: discord-internal.h:412
void(* typed)(struct discord *client, struct discord_response *resp, const void *ret)
Definition: discord-internal.h:420
union discord_ret_dispatch::@5 done
Attributes of response datatype.
Definition: discord-internal.h:434
size_t size
Definition: discord-internal.h:438
void * data
Definition: discord-internal.h:436
struct used for modifying, and getting info about a timer
Definition: discord.h:678
void * data
Definition: discord.h:689
int64_t delay
Definition: discord.h:691
Definition: discord-internal.h:117
struct io_poller * io
Definition: discord-internal.h:119
pthread_mutex_t lock
Definition: discord-internal.h:126
bool skip_update_phase
Definition: discord-internal.h:124
struct discord_timers::@2 active
bool is_active
Definition: discord-internal.h:121
priority_queue * q
Definition: discord-internal.h:118
pthread_cond_t cond
Definition: discord-internal.h:127
struct discord_timer * timer
Definition: discord-internal.h:123
pthread_t thread
Definition: discord-internal.h:122
The Discord client handler.
Definition: discord-internal.h:1206
struct discord_user self
Definition: discord-internal.h:1238
struct discord::@12 * workers
struct discord::@11 timers
struct discord_cache cache
Definition: discord-internal.h:1240
void(* on_cycle)(struct discord *client)
Definition: discord-internal.h:1253
struct discord_message_commands commands
Definition: discord-internal.h:1229
struct logmod_logger * logger
Definition: discord-internal.h:1214
struct discord_rest rest
Definition: discord-internal.h:1234
struct reflectc * registry
Definition: discord-internal.h:1212
struct discord_refcounter refcounter
Definition: discord-internal.h:1231
struct io_poller * io_poller
Definition: discord-internal.h:1226
struct ccord_szbuf file
Definition: discord-internal.h:1270
unsigned pid
Definition: discord-internal.h:1222
struct discord_config config
Definition: discord-internal.h:1220
void(* on_idle)(struct discord *client)
Definition: discord-internal.h:1251
pthread_mutex_t lock
Definition: discord-internal.h:1263
bool is_original
Definition: discord-internal.h:1224
pthread_cond_t cond
Definition: discord-internal.h:1265
struct discord_timers internal
Definition: discord-internal.h:1246
struct logmod_logger table[64]
Definition: discord-internal.h:1216
struct discord_gateway gw
Definition: discord-internal.h:1236
void * data
Definition: discord-internal.h:1256
int shutdown_fd
Definition: discord-internal.h:1243
struct discord_timers user
Definition: discord-internal.h:1247
int count
Definition: discord-internal.h:1261
Immutable logger structure.
Definition: logmod.h:229
Main logging context structure.
Definition: logmod.h:240
Opaque handler for WebSockets.
struct threadpool_t threadpool_t
Definition: threadpool.h:48