8#ifndef DISCORD_INTERNAL_H
9#define DISCORD_INTERNAL_H
25#include "user-agent.h"
34#define NOT_EMPTY_STR(str) ((str) && *(str))
42#define CONTAINEROF(ptr, type, path) \
43 ((type *)((char *)(ptr)-offsetof(type, path)))
53#define CLIENT(ptr, path) CONTAINEROF(ptr, struct discord, path)
64#define CCORD_EXPECT(client, expect, code, reason) \
67 logconf_error(&(client)->conf, "Expected: " #expect ": " reason); \
79#define ASSERT_NOT_OOB(nbytes, destsz) \
80 ASSERT_S((size_t)nbytes < (size_t)destsz, "Out of bounds write attempt");
83#define DISCORD_ENDPT_LEN 512
85#define DISCORD_ROUTE_LEN 256
201#define DISCORD_BUCKET_TIMEOUT (void *)(0xf)
265 const char endpoint_fmt[],
280 struct ua_info *info);
424#define DISCORD_ATTRIBUTES_FIELDS \
426 struct discord_ret_dispatch dispatch; \
428 struct discord_ret_response response; \
430 struct discord_attachments attachments; \
460 struct ua_conn *
conn;
466 pthread_cond_t *
cond;
476 struct user_agent *
ua;
577 enum http_method method,
639 enum http_method method,
661#define DISCORD_SESSION_OFFLINE 0u
663#define DISCORD_SESSION_RESUMABLE 1u << 0
665#define DISCORD_SESSION_SHUTDOWN 1u << 1
954 struct _discord_ref *
refs;
980 void (*cleanup)(
void *data),
994 void (*cleanup)(
struct discord *client,
1086 struct _discord_message_commands_entry *
entries;
1115 const char command[],
1127 const char command[],
1142 const char prefix[],
1244 pthread_mutex_t
lock;
1250 struct discord_voice *vcs;
1251 struct discord_voice_evcallbacks voice_cbs;
#define PRINTF_LIKE(a, b)
Definition: attributes.h:4
discord_gateway_opcodes
Definition: gateway.h:77
CCORDcode
Definition: error.h:12
uint64_t u64unix_ms
Unix time in milliseconds.
Definition: types.h:22
discord_gateway_events
Discord Gateway's events.
Definition: discord-events.h:65
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:155
@ DISCORD_EV_MAX
Definition: discord-events.h:129
void(* discord_ev_message)(struct discord *client, const struct discord_message *event)
Definition: discord-internal.h:732
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_init(struct discord_gateway *gw, struct logconf *conf, const char token[])
Initialize a Gateway handle.
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.
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:730
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.
void discord_message_commands_init(struct discord_message_commands *cmds, struct logconf *conf)
Initialize a Message Commands handle.
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_init(struct discord_ratelimiter *rl, struct logconf *conf)
Initialize ratelimiter handle.
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.
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:424
CCORDcode discord_requestor_info_read(struct discord_requestor *rqtor)
Poll for request's completion.
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_request_begin(struct discord_requestor *rqtor, struct discord_attributes *req, struct ccord_szbuf *body, enum http_method method, char endpoint[512], char key[256])
Begin a new request.
void discord_requestor_init(struct discord_requestor *rqtor, struct logconf *conf, const char token[])
Initialize the request handler.
CCORDcode discord_rest_run(struct discord_rest *rest, struct discord_attributes *req, struct ccord_szbuf *body, enum http_method method, char endpoint_fmt[],...) PRINTF_LIKE(5
Perform a request to Discord.
void discord_rest_cleanup(struct discord_rest *rest)
Free an REST handle.
CCORDcode void discord_rest_stop_buckets(struct discord_rest *rest)
Stop all bucket's on-going, pending and timed-out requests.
void discord_rest_init(struct discord_rest *rest, struct logconf *conf, const char token[])
Initialize an REST handle.
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
void discord_refcounter_init(struct discord_refcounter *rc, struct logconf *conf)
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.
void discord_timers_init(struct discord_timers *timers, struct io_poller *io)
Prepare timers for usage.
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.
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:85
#define DISCORD_ENDPT_LEN
Definition: discord-internal.h:83
int discord_dup_shutdown_fd(void)
dup shutdown fd to listen for ccord_shutdown_async()
void(* discord_ev_timer)(struct discord *client, struct discord_timer *ev)
callback to be used with struct discord_timer
Definition: discord.h:349
struct priority_queue priority_queue
Definition: priority_queue.h:13
void * QUEUE[2]
Definition: queue.h:21
Reusable generic sized buffer.
Definition: types.h:63
Generic sized buffer.
Definition: types.h:47
Request to be performed.
Definition: discord-internal.h:435
The Discord bucket for handling per-group ratelimits.
Definition: discord-internal.h:295
u64unix_ms reset_tstamp
Definition: discord-internal.h:303
struct discord_request * busy_req
Definition: discord-internal.h:309
long limit
Definition: discord-internal.h:299
long remaining
Definition: discord-internal.h:301
char hash[64]
Definition: discord-internal.h:297
QUEUE entry
Definition: discord-internal.h:317
struct discord_bucket::@2 queues
The Discord Cache control handler.
Definition: discord-internal.h:1166
void(* on_shard_resumed)(struct discord *client, const struct discord_identify *ident)
Definition: discord-internal.h:1175
struct _discord_cache_data * data
Definition: discord-internal.h:1167
void(* on_shard_disconnected)(struct discord *client, const struct discord_identify *ident, bool resumable)
Definition: discord-internal.h:1171
void(* cleanup)(struct discord *client)
Definition: discord-internal.h:1168
void(* on_shard_reconnected)(struct discord *client, const struct discord_identify *ident)
Definition: discord-internal.h:1178
The handle for storing the Discord response payload.
Definition: discord-internal.h:700
jsmnf_pair * pairs
Definition: discord-internal.h:712
unsigned ntokens
Definition: discord-internal.h:710
size_t size
Definition: discord-internal.h:706
struct discord_gateway_payload::@7 json
int seq
Definition: discord-internal.h:720
jsmnf_pair * data
Definition: discord-internal.h:726
char * start
Definition: discord-internal.h:704
jsmntok_t * tokens
Definition: discord-internal.h:708
enum discord_gateway_events event
Definition: discord-internal.h:724
char name[32]
Definition: discord-internal.h:722
enum discord_gateway_opcodes opcode
Definition: discord-internal.h:718
unsigned npairs
Definition: discord-internal.h:714
The handle for storing the Discord Gateway session.
Definition: discord-internal.h:668
char base_url[256]
Definition: discord-internal.h:676
struct discord_session_start_limit start_limit
Definition: discord-internal.h:680
int limit
Definition: discord-internal.h:695
char resume_url[256]
Definition: discord-internal.h:678
int attempt
Definition: discord-internal.h:693
int shards
Definition: discord-internal.h:674
int event_count
Definition: discord-internal.h:684
struct discord_gateway_session::@6 retry
unsigned status
Definition: discord-internal.h:686
int concurrent
Definition: discord-internal.h:682
bool enable
Definition: discord-internal.h:691
bool is_ready
Definition: discord-internal.h:670
The handle used for interfacing with Discord's Gateway API.
Definition: discord-internal.h:736
struct websockets * ws
Definition: discord-internal.h:740
u64unix_ms hbeat_last
Definition: discord-internal.h:766
struct discord_gateway_payload payload
Definition: discord-internal.h:796
discord_ev_scheduler scheduler
Definition: discord-internal.h:805
unsigned hbeat_timer
Definition: discord-internal.h:778
u64unix_ms now
Definition: discord-internal.h:760
struct discord_gateway_session * session
Definition: discord-internal.h:793
int64_t hbeat_interval
Definition: discord-internal.h:750
pthread_rwlock_t rwlock
Definition: discord-internal.h:786
struct logconf conf
Definition: discord-internal.h:738
int ping_ms
Definition: discord-internal.h:784
u64unix_ms identify_last
Definition: discord-internal.h:771
u64unix_ms event
Definition: discord-internal.h:776
struct discord_gateway::@8 * timer
bool hbeat_acknowledged
Definition: discord-internal.h:755
CURLM * mhandle
Definition: discord-internal.h:742
struct discord_identify id
Definition: discord-internal.h:790
discord_ev_event cbs[2][DISCORD_EV_MAX]
Definition: discord-internal.h:803
Definition: gateway.h:259
The handle for storing user's message commands.
Definition: discord-internal.h:1070
struct _discord_message_commands_entry * entries
Definition: discord-internal.h:1085
discord_ev_message fallback
Definition: discord-internal.h:1076
int length
Definition: discord-internal.h:1078
struct logconf conf
Definition: discord-internal.h:1072
struct ccord_szbuf prefix
Definition: discord-internal.h:1074
int capacity
Definition: discord-internal.h:1080
Definition: channel.h:195
Definition: gateway.h:214
The ratelimiter struct for handling ratelimiting.
Definition: discord-internal.h:207
struct discord_bucket * null
Definition: discord-internal.h:221
u64unix_ms * global_wait_tstamp
Definition: discord-internal.h:227
int length
Definition: discord-internal.h:211
struct discord_ratelimiter::@1 queues
struct discord_bucket * miss
Definition: discord-internal.h:224
int capacity
Definition: discord-internal.h:213
struct logconf conf
Definition: discord-internal.h:209
struct _discord_route * routes
Definition: discord-internal.h:218
Automatically cleanup user data.
Definition: discord-internal.h:942
pthread_mutex_t * g_lock
Definition: discord-internal.h:955
struct logconf conf
Definition: discord-internal.h:944
struct _discord_ref * refs
Definition: discord-internal.h:953
int length
Definition: discord-internal.h:946
int capacity
Definition: discord-internal.h:948
Definition: gateway.h:299
Individual requests that are scheduled to run asynchronously.
Definition: discord-internal.h:445
struct ccord_szbuf_reusable body
Definition: discord-internal.h:451
QUEUE entry
Definition: discord-internal.h:467
pthread_cond_t * cond
Definition: discord-internal.h:465
enum http_method method
Definition: discord-internal.h:453
char endpoint[512]
Definition: discord-internal.h:455
int retry_attempt
Definition: discord-internal.h:463
char key[256]
Definition: discord-internal.h:457
struct discord_bucket * b
Definition: discord-internal.h:449
CCORDcode code
Definition: discord-internal.h:461
struct ua_conn * conn
Definition: discord-internal.h:459
The handle used for handling asynchronous requests.
Definition: discord-internal.h:471
pthread_mutex_t recycling
Definition: discord-internal.h:500
CURLM * mhandle
Definition: discord-internal.h:477
struct discord_requestor::@5 * qlocks
pthread_mutex_t finished
Definition: discord-internal.h:504
pthread_mutex_t pending
Definition: discord-internal.h:502
int retry_limit
Definition: discord-internal.h:482
struct user_agent * ua
Definition: discord-internal.h:475
struct logconf conf
Definition: discord-internal.h:473
struct discord_ratelimiter ratelimiter
Definition: discord-internal.h:479
struct discord_requestor::@4 * 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:588
struct threadpool_t * tpool
Definition: discord-internal.h:598
struct logconf conf
Definition: discord-internal.h:590
struct io_poller * io_poller
Definition: discord-internal.h:596
struct discord_requestor requestor
Definition: discord-internal.h:592
struct discord_timers timers
Definition: discord-internal.h:594
Definition: gateway.h:290
Generic request dispatcher.
Definition: discord-internal.h:382
DISCORD_RET_DEFAULT_FIELDS
Definition: discord-internal.h:383
union discord_ret_dispatch::@3 done
void(* typeless)(struct discord *client, struct discord_response *resp)
Definition: discord-internal.h:396
void * sync
Definition: discord-internal.h:403
bool has_type
Definition: discord-internal.h:385
void(* typed)(struct discord *client, struct discord_response *resp, const void *ret)
Definition: discord-internal.h:393
Attributes of response datatype.
Definition: discord-internal.h:407
size_t size
Definition: discord-internal.h:411
size_t(* from_json)(const char *json, size_t len, void *data)
Definition: discord-internal.h:415
void(* init)(void *data)
Definition: discord-internal.h:413
void * data
Definition: discord-internal.h:409
void(* cleanup)(void *data)
Definition: discord-internal.h:417
Definition: gateway.h:686
struct used for modifying, and getting info about a timer
Definition: discord.h:373
void * data
Definition: discord.h:384
int64_t delay
Definition: discord.h:386
Definition: discord-internal.h:91
struct io_poller * io
Definition: discord-internal.h:93
pthread_mutex_t lock
Definition: discord-internal.h:100
bool skip_update_phase
Definition: discord-internal.h:98
bool is_active
Definition: discord-internal.h:95
priority_queue * q
Definition: discord-internal.h:92
struct discord_timers::@0 active
pthread_cond_t cond
Definition: discord-internal.h:101
struct discord_timer * timer
Definition: discord-internal.h:97
pthread_t thread
Definition: discord-internal.h:96
Definition: gateway.h:320
The Discord client handler.
Definition: discord-internal.h:1190
struct discord_user self
Definition: discord-internal.h:1210
unsigned id
Definition: discord-internal.h:1227
struct discord_cache cache
Definition: discord-internal.h:1212
void(* cb)(struct discord *client)
Definition: discord-internal.h:1225
void(* on_cycle)(struct discord *client)
Definition: discord-internal.h:1233
struct discord_message_commands commands
Definition: discord-internal.h:1201
struct discord_rest rest
Definition: discord-internal.h:1206
struct discord::@10 wakeup_timer
struct discord::@11 * workers
struct discord_refcounter refcounter
Definition: discord-internal.h:1203
struct io_poller * io_poller
Definition: discord-internal.h:1198
void(* on_idle)(struct discord *client)
Definition: discord-internal.h:1231
pthread_mutex_t lock
Definition: discord-internal.h:1243
bool is_original
Definition: discord-internal.h:1194
pthread_cond_t cond
Definition: discord-internal.h:1245
struct discord_timers internal
Definition: discord-internal.h:1218
struct logconf conf
Definition: discord-internal.h:1192
struct discord_gateway gw
Definition: discord-internal.h:1208
struct discord::@9 timers
void * data
Definition: discord-internal.h:1236
int shutdown_fd
Definition: discord-internal.h:1215
struct discord_timers user
Definition: discord-internal.h:1219
int count
Definition: discord-internal.h:1241
char * token
Definition: discord-internal.h:1196
A stackful and modularized wrapper over the popular 'log.c' facilities.
Definition: logconf.h:217
Opaque handler for WebSockets.
struct threadpool_t threadpool_t
Definition: threadpool.h:48