Concord - C Discord API library
A Discord API wrapper library written in C
discord-internal.h File Reference

Internal functions and datatypes. More...

#include <pthread.h>
#include "json-build.h"
#include "jsmn.h"
#include "jsmn-find.h"
#include "logconf.h"
#include "user-agent.h"
#include "websockets.h"
#include "cog-utils.h"
#include "io_poller.h"
#include "queue.h"
#include "priority_queue.h"
Include dependency graph for discord-internal.h:

Go to the source code of this file.

Data Structures

struct  discord_timers
 
struct  discord_ratelimiter
 The ratelimiter struct for handling ratelimiting. More...
 
struct  discord_bucket
 The Discord bucket for handling per-group ratelimits. More...
 
struct  discord_ret_dispatch
 Generic request dispatcher. More...
 
struct  discord_ret_response
 Attributes of response datatype. More...
 
struct  discord_attributes
 Request to be performed. More...
 
struct  discord_request
 Individual requests that are scheduled to run asynchronously. More...
 
struct  discord_requestor
 The handle used for handling asynchronous requests. More...
 
struct  discord_rest
 The handle used for interfacing with Discord's REST API. More...
 
struct  discord_gateway_session
 The handle for storing the Discord Gateway session. More...
 
struct  discord_gateway_payload
 The handle for storing the Discord response payload. More...
 
struct  discord_gateway
 The handle used for interfacing with Discord's Gateway API. More...
 
struct  discord_refcounter
 Automatically cleanup user data. More...
 
struct  discord_message_commands
 The handle for storing user's message commands. More...
 
struct  discord_cache
 The Discord Cache control handler. More...
 
struct  discord
 The Discord client handler. More...
 

Macros

#define JSONB_HEADER
 
#define JSMN_STRICT
 
#define JSMN_HEADER
 
#define NOT_EMPTY_STR(str)   ((str) && *(str))
 Return 1 if string isn't considered empty. More...
 
#define CONTAINEROF(ptr, type, path)    ((type *)((char *)(ptr)-offsetof(type, path)))
 Get container type from a field ptr More...
 
#define CLIENT(ptr, path)   CONTAINEROF(ptr, struct discord, path)
 Get client from its nested field. More...
 
#define CCORD_EXPECT(client, expect, code, reason)
 log and return code if expect condition is false More...
 
#define ASSERT_NOT_OOB(nbytes, destsz)    ASSERT_S((size_t)nbytes < (size_t)destsz, "Out of bounds write attempt");
 Shortcut for checking OOB-write attempts. More...
 
#define DISCORD_ENDPT_LEN   512
 
#define DISCORD_ROUTE_LEN   256
 
#define DISCORD_BUCKET_TIMEOUT   (void *)(0xf)
 Value assigned to discord_bucket busy_req field in case it's being timed-out. More...
 
#define DISCORD_ATTRIBUTES_FIELDS
 Macro containing discord_attributes fields. More...
 
#define DISCORD_SESSION_OFFLINE   0u
 
#define DISCORD_SESSION_RESUMABLE   1u << 0
 
#define DISCORD_SESSION_SHUTDOWN   1u << 1
 

Typedefs

typedef void(* discord_ev_event) (struct discord *client, const void *event)
 
typedef void(* discord_ev_message) (struct discord *client, const struct discord_message *event)
 

Functions

void discord_timers_init (struct discord_timers *timers, struct io_poller *io)
 Prepare timers for usage. More...
 
void discord_timers_cleanup (struct discord *client, struct discord_timers *timers)
 Cleanup timers and call cancel any running ones. More...
 
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. More...
 
void discord_timers_run (struct discord *client, struct discord_timers *timers)
 Run all timers that are due. More...
 
unsigned _discord_timer_ctl (struct discord *client, struct discord_timers *timers, struct discord_timer *timer)
 Modifies or creates a timer. More...
 
unsigned discord_internal_timer_ctl (struct discord *client, struct discord_timer *timer)
 Modifies or creates a timer. More...
 
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. More...
 
void discord_ratelimiter_init (struct discord_ratelimiter *rl, struct logconf *conf)
 Initialize ratelimiter handle. More...
 
void discord_ratelimiter_cleanup (struct discord_ratelimiter *rl)
 Cleanup all buckets that have been discovered. More...
 
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. More...
 
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. More...
 
void discord_ratelimiter_set_global_timeout (struct discord_ratelimiter *rl, struct discord_bucket *bucket, u64unix_ms wait_ms)
 Update global ratelimiting value. More...
 
void discord_bucket_set_timeout (struct discord_bucket *bucket, u64unix_ms wait_ms)
 Set bucket timeout. More...
 
struct discord_bucketdiscord_bucket_get (struct discord_ratelimiter *rl, const char key[])
 Get a struct discord_bucket assigned to key More...
 
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. More...
 
void discord_bucket_request_selector (struct discord_ratelimiter *rl, void *data, void(*iter)(void *data, struct discord_request *req))
 Iterate and select next requests. More...
 
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() More...
 
void discord_requestor_init (struct discord_requestor *rqtor, struct logconf *conf, const char token[])
 Initialize the request handler. More...
 
void discord_requestor_cleanup (struct discord_requestor *rqtor)
 Free the request handler. More...
 
CCORDcode discord_requestor_start_pending (struct discord_requestor *rqtor)
 Check for and start pending bucket's requests. More...
 
CCORDcode discord_requestor_info_read (struct discord_requestor *rqtor)
 Poll for request's completion. More...
 
void discord_requestor_dispatch_responses (struct discord_requestor *rqtor)
 Run pending callbacks from completed requests. More...
 
void discord_request_cancel (struct discord_requestor *rqtor, struct discord_request *req)
 Mark request as canceled and move it to the recycling queue. More...
 
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. More...
 
void discord_rest_init (struct discord_rest *rest, struct logconf *conf, const char token[])
 Initialize an REST handle. More...
 
void discord_rest_cleanup (struct discord_rest *rest)
 Free an REST handle. More...
 
CCORDcode discord_rest_run (struct discord_rest *rest, struct discord_attributes *req, struct ccord_szbuf *body, enum http_method method, char endpoint_fmt[],...)
 Perform a request to Discord. More...
 
void discord_rest_stop_buckets (struct discord_rest *rest)
 Stop all bucket's on-going, pending and timed-out requests. More...
 
void discord_gateway_init (struct discord_gateway *gw, struct logconf *conf, const char token[])
 Initialize a Gateway handle. More...
 
void discord_gateway_cleanup (struct discord_gateway *gw)
 Free a Gateway handle. More...
 
CCORDcode discord_gateway_start (struct discord_gateway *gw)
 Initialize handle with the new session primitives. More...
 
bool discord_gateway_end (struct discord_gateway *gw)
 Cleanup and reset gw session primitives. More...
 
CCORDcode discord_gateway_perform (struct discord_gateway *gw)
 Check and manage on-going Gateway session. More...
 
void discord_gateway_shutdown (struct discord_gateway *gw)
 Gracefully shutdown a ongoing Discord connection over WebSockets. More...
 
void discord_gateway_reconnect (struct discord_gateway *gw, bool resume)
 Gracefully reconnect a ongoing Discord connection over WebSockets. More...
 
void discord_gateway_send_identify (struct discord_gateway *gw, struct discord_identify *event)
 Trigger the initial handshake with the gateway. More...
 
void discord_gateway_send_resume (struct discord_gateway *gw, struct discord_resume *event)
 Replay missed events when a disconnected client resumes. More...
 
void discord_gateway_send_heartbeat (struct discord_gateway *gw, int seq)
 Maintain an active gateway connection. More...
 
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. More...
 
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. More...
 
void discord_gateway_send_presence_update (struct discord_gateway *gw, struct discord_presence_update *event)
 Send client's presence status update payload. More...
 
void discord_gateway_dispatch (struct discord_gateway *gw)
 Dispatch user callback matched to event. More...
 
void discord_refcounter_init (struct discord_refcounter *rc, struct logconf *conf)
 Initialize reference counter handle. More...
 
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. More...
 
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. More...
 
void discord_refcounter_cleanup (struct discord_refcounter *rc)
 Cleanup refcounter and all user data currently held. More...
 
CCORDcode discord_refcounter_claim (struct discord_refcounter *rc, const void *data)
 Claim ownership of data More...
 
CCORDcode discord_refcounter_unclaim (struct discord_refcounter *rc, void *data)
 Unclaim ownership of data More...
 
CCORDcode discord_refcounter_incr (struct discord_refcounter *rc, void *data)
 Increment the reference counter for ret->data More...
 
CCORDcode discord_refcounter_decr (struct discord_refcounter *rc, void *data)
 Decrement the reference counter for data More...
 
void discord_message_commands_init (struct discord_message_commands *cmds, struct logconf *conf)
 Initialize a Message Commands handle. More...
 
void discord_message_commands_cleanup (struct discord_message_commands *cmds)
 Free a Message Commands handle. More...
 
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. More...
 
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. More...
 
void discord_message_commands_set_prefix (struct discord_message_commands *cmds, const char prefix[], size_t length)
 Set a mandatory prefix before commands. More...
 
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. More...
 

Detailed Description

Internal functions and datatypes.

Author
Cogmasters

Macro Definition Documentation

◆ JSONB_HEADER

#define JSONB_HEADER

◆ JSMN_STRICT

#define JSMN_STRICT

◆ JSMN_HEADER

#define JSMN_HEADER

◆ NOT_EMPTY_STR

#define NOT_EMPTY_STR (   str)    ((str) && *(str))

Return 1 if string isn't considered empty.

◆ CONTAINEROF

#define CONTAINEROF (   ptr,
  type,
  path 
)     ((type *)((char *)(ptr)-offsetof(type, path)))

Get container type from a field ptr

Parameters
[in]ptrthe field contained in type
[in]typethe container datatype
[in]paththe path to the field from the container POV