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)))
50#define CLIENT(ptr, path) CONTAINEROF(ptr, struct discord, path)
61#define CCORD_EXPECT(client, expect, code, reason) \
64 logconf_error(&(client)->conf, "Expected: " #expect ": " reason); \
76#define ASSERT_NOT_OOB(nbytes, destsz) \
77 ASSERT_S((size_t)nbytes < (size_t)destsz, "Out of bounds write attempt");
80#define DISCORD_ENDPT_LEN 512
82#define DISCORD_ROUTE_LEN 256
198#define DISCORD_BUCKET_TIMEOUT (void *)(0xf)
262 const char endpoint_fmt[],
277 struct ua_info *info);
421#define DISCORD_ATTRIBUTES_FIELDS \
423 struct discord_ret_dispatch dispatch; \
425 struct discord_ret_response response; \
427 struct discord_attachments attachments; \
457 struct ua_conn *
conn;
463 pthread_cond_t *
cond;
473 struct user_agent *
ua;
574 enum http_method method,
636 enum http_method method,
658#define DISCORD_SESSION_OFFLINE 0u
660#define DISCORD_SESSION_RESUMABLE 1u << 0
662#define DISCORD_SESSION_SHUTDOWN 1u << 1
946 struct _discord_ref *
refs;
972 void (*cleanup)(
void *data),
986 void (*cleanup)(
struct discord *client,
1078 struct _discord_message_commands_entry *
entries;
1107 const char command[],
1119 const char command[],
1134 const char prefix[],
1233 pthread_mutex_t
lock;
1239 struct discord_voice *vcs;
1240 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:729
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:727
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:421
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:82
#define DISCORD_ENDPT_LEN
Definition: discord-internal.h:80
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:432
The Discord bucket for handling per-group ratelimits.
Definition: discord-internal.h:292
u64unix_ms reset_tstamp
Definition: discord-internal.h:300
struct discord_request * busy_req
Definition: discord-internal.h:306
long limit
Definition: discord-internal.h:296
long remaining
Definition: discord-internal.h:298
char hash[64]
Definition: discord-internal.h:294
QUEUE entry
Definition: discord-internal.h:314
struct discord_bucket::@2 queues
The Discord Cache control handler.
Definition: discord-internal.h:1158
void(* on_shard_resumed)(struct discord *client, const struct discord_identify *ident)
Definition: discord-internal.h:1167
struct _discord_cache_data * data
Definition: discord-internal.h:1159
void(* on_shard_disconnected)(struct discord *client, const struct discord_identify *ident, bool resumable)
Definition: discord-internal.h:1163
void(* cleanup)(struct discord *client)
Definition: discord-internal.h:1160
void(* on_shard_reconnected)(struct discord *client, const struct discord_identify *ident)
Definition: discord-internal.h:1170
The handle for storing the Discord response payload.
Definition: discord-internal.h:697
jsmnf_pair * pairs
Definition: discord-internal.h:709
unsigned ntokens
Definition: discord-internal.h:707
size_t size
Definition: discord-internal.h:703
struct discord_gateway_payload::@7 json
int seq
Definition: discord-internal.h:717
jsmnf_pair * data
Definition: discord-internal.h:723
char * start
Definition: discord-internal.h:701
jsmntok_t * tokens
Definition: discord-internal.h:705
enum discord_gateway_events event
Definition: discord-internal.h:721
char name[32]
Definition: discord-internal.h:719
enum discord_gateway_opcodes opcode
Definition: discord-internal.h:715
unsigned npairs
Definition: discord-internal.h:711
The handle for storing the Discord Gateway session.
Definition: discord-internal.h:665
char base_url[256]
Definition: discord-internal.h:673
struct discord_session_start_limit start_limit
Definition: discord-internal.h:677
int limit
Definition: discord-internal.h:692
char resume_url[256]
Definition: discord-internal.h:675
int attempt
Definition: discord-internal.h:690
int shards
Definition: discord-internal.h:671
int event_count
Definition: discord-internal.h:681
struct discord_gateway_session::@6 retry
unsigned status
Definition: discord-internal.h:683
int concurrent
Definition: discord-internal.h:679
bool enable
Definition: discord-internal.h:688
bool is_ready
Definition: discord-internal.h:667
The handle used for interfacing with Discord's Gateway API.
Definition: discord-internal.h:733
struct websockets * ws
Definition: discord-internal.h:737
u64unix_ms hbeat_last
Definition: discord-internal.h:758
struct discord_gateway_payload payload
Definition: discord-internal.h:788
discord_ev_scheduler scheduler
Definition: discord-internal.h:797
unsigned hbeat_timer
Definition: discord-internal.h:770
u64unix_ms now
Definition: discord-internal.h:752
struct discord_gateway_session * session
Definition: discord-internal.h:785
int64_t hbeat_interval
Definition: discord-internal.h:747
pthread_rwlock_t rwlock
Definition: discord-internal.h:778
struct logconf conf
Definition: discord-internal.h:735
int ping_ms
Definition: discord-internal.h:776
u64unix_ms identify_last
Definition: discord-internal.h:763
u64unix_ms event
Definition: discord-internal.h:768
struct discord_gateway::@8 * timer
CURLM * mhandle
Definition: discord-internal.h:739
struct discord_identify id
Definition: discord-internal.h:782
discord_ev_event cbs[2][DISCORD_EV_MAX]
Definition: discord-internal.h:795
Definition: gateway.h:259
The handle for storing user's message commands.
Definition: discord-internal.h:1062
struct _discord_message_commands_entry * entries
Definition: discord-internal.h:1077
discord_ev_message fallback
Definition: discord-internal.h:1068
int length
Definition: discord-internal.h:1070
struct logconf conf
Definition: discord-internal.h:1064
struct ccord_szbuf prefix
Definition: discord-internal.h:1066
int capacity
Definition: discord-internal.h:1072
Definition: channel.h:195
Definition: gateway.h:214
The ratelimiter struct for handling ratelimiting.
Definition: discord-internal.h:204
struct discord_bucket * null
Definition: discord-internal.h:218
u64unix_ms * global_wait_tstamp
Definition: discord-internal.h:224
int length
Definition: discord-internal.h:208
struct discord_ratelimiter::@1 queues
struct discord_bucket * miss
Definition: discord-internal.h:221
int capacity
Definition: discord-internal.h:210
struct logconf conf
Definition: discord-internal.h:206
struct _discord_route * routes
Definition: discord-internal.h:215
Automatically cleanup user data.
Definition: discord-internal.h:934
pthread_mutex_t * g_lock
Definition: discord-internal.h:947
struct logconf conf
Definition: discord-internal.h:936
struct _discord_ref * refs
Definition: discord-internal.h:945
int length
Definition: discord-internal.h:938
int capacity
Definition: discord-internal.h:940
Definition: gateway.h:299
Individual requests that are scheduled to run asynchronously.
Definition: discord-internal.h:442
struct ccord_szbuf_reusable body
Definition: discord-internal.h:448
QUEUE entry
Definition: discord-internal.h:464
pthread_cond_t * cond
Definition: discord-internal.h:462
enum http_method method
Definition: discord-internal.h:450
char endpoint[512]
Definition: discord-internal.h:452
int retry_attempt
Definition: discord-internal.h:460
char key[256]
Definition: discord-internal.h:454
struct discord_bucket * b
Definition: discord-internal.h:446
CCORDcode code
Definition: discord-internal.h:458
struct ua_conn * conn
Definition: discord-internal.h:456
The handle used for handling asynchronous requests.
Definition: discord-internal.h:468
pthread_mutex_t recycling
Definition: discord-internal.h:497
CURLM * mhandle
Definition: discord-internal.h:474
struct discord_requestor::@5 * qlocks
pthread_mutex_t finished
Definition: discord-internal.h:501
pthread_mutex_t pending
Definition: discord-internal.h:499
int retry_limit
Definition: discord-internal.h:479
struct user_agent * ua
Definition: discord-internal.h:472
struct logconf conf
Definition: discord-internal.h:470
struct discord_ratelimiter ratelimiter
Definition: discord-internal.h:476
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:585
struct threadpool_t * tpool
Definition: discord-internal.h:595
struct logconf conf
Definition: discord-internal.h:587
struct io_poller * io_poller
Definition: discord-internal.h:593
struct discord_requestor requestor
Definition: discord-internal.h:589
struct discord_timers timers
Definition: discord-internal.h:591
Definition: gateway.h:290
Generic request dispatcher.
Definition: discord-internal.h:379
DISCORD_RET_DEFAULT_FIELDS
Definition: discord-internal.h:380
union discord_ret_dispatch::@3 done
void(* typeless)(struct discord *client, struct discord_response *resp)
Definition: discord-internal.h:393
void * sync
Definition: discord-internal.h:400
bool has_type
Definition: discord-internal.h:382
void(* typed)(struct discord *client, struct discord_response *resp, const void *ret)
Definition: discord-internal.h:390
Attributes of response datatype.
Definition: discord-internal.h:404
size_t size
Definition: discord-internal.h:408
size_t(* from_json)(const char *json, size_t len, void *data)
Definition: discord-internal.h:412
void(* init)(void *data)
Definition: discord-internal.h:410
void * data
Definition: discord-internal.h:406
void(* cleanup)(void *data)
Definition: discord-internal.h:414
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:88
struct io_poller * io
Definition: discord-internal.h:90
pthread_mutex_t lock
Definition: discord-internal.h:97
bool skip_update_phase
Definition: discord-internal.h:95
bool is_active
Definition: discord-internal.h:92
priority_queue * q
Definition: discord-internal.h:89
struct discord_timers::@0 active
pthread_cond_t cond
Definition: discord-internal.h:98
struct discord_timer * timer
Definition: discord-internal.h:94
pthread_t thread
Definition: discord-internal.h:93
Definition: gateway.h:320
The Discord client handler.
Definition: discord-internal.h:1182
struct discord_user self
Definition: discord-internal.h:1202
unsigned id
Definition: discord-internal.h:1216
struct discord_cache cache
Definition: discord-internal.h:1204
void(* cb)(struct discord *client)
Definition: discord-internal.h:1214
void(* on_cycle)(struct discord *client)
Definition: discord-internal.h:1222
struct discord_message_commands commands
Definition: discord-internal.h:1193
struct discord_rest rest
Definition: discord-internal.h:1198
struct discord::@10 wakeup_timer
struct discord::@11 * workers
struct discord_refcounter refcounter
Definition: discord-internal.h:1195
struct io_poller * io_poller
Definition: discord-internal.h:1190
void(* on_idle)(struct discord *client)
Definition: discord-internal.h:1220
pthread_mutex_t lock
Definition: discord-internal.h:1232
bool is_original
Definition: discord-internal.h:1186
pthread_cond_t cond
Definition: discord-internal.h:1234
struct discord_timers internal
Definition: discord-internal.h:1207
struct logconf conf
Definition: discord-internal.h:1184
struct discord_gateway gw
Definition: discord-internal.h:1200
struct discord::@9 timers
void * data
Definition: discord-internal.h:1225
struct discord_timers user
Definition: discord-internal.h:1208
int count
Definition: discord-internal.h:1230
char * token
Definition: discord-internal.h:1188
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