switch over to use sessions

This commit is contained in:
2019-07-10 14:30:37 +02:00
parent ffcc9f60f7
commit 30fb4a73df
17 changed files with 432 additions and 109 deletions

25
libdc/include/dc/event.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef DC_EVENT_H
#define DC_EVENT_H
#include <stdint.h>
#include <jansson.h>
struct dc_event_;
typedef struct dc_event_ *dc_event_t;
dc_event_t dc_event_new(char const *type, json_t *payload);
/**
* Returns the event type. This is an upper case string, with
* words separated by underscores. For a list of available event
* types please see the Discord documentation.
*/
char const *dc_event_type(dc_event_t e);
/**
* The JSON payload associated with the given event type. Note this
* could be json_null() if the event has no associated payload.
*/
json_t *dc_event_payload(dc_event_t e);
#endif

View File

@@ -5,12 +5,22 @@
#include <stdlib.h>
#include <dc/account.h>
#include <curl/curl.h>
#include <dc/event.h>
struct dc_gateway_;
typedef struct dc_gateway_ *dc_gateway_t;
/**
* The event callback that will be called by the gateway when a new event
* arrives. First parameter is the gateway responsible for sending this
* event, second parameter is the event in question, third parameter is
* user defined callback data.
*
* Note that the event will be allocated, and dc_unref()'d by the gateway,
* so if you need the event around you need to dc_ref() it.
*/
typedef void (*dc_gateway_event_callback_t)(dc_gateway_t, dc_event_t, void*);
typedef enum {
GATEWAY_OPCODE_EVENT = 0,
GATEWAY_OPCODE_PING = 1,
@@ -31,6 +41,13 @@ dc_gateway_t dc_gateway_new(void);
void dc_gateway_set_login(dc_gateway_t gw, dc_account_t login);
void dc_gateway_set_callback(dc_gateway_t gw, dc_gateway_event_callback_t c,
void *userdata);
/**
* Connect the given gateway. Does nothing if the gateway is already
* connected.
*/
bool dc_gateway_connect(dc_gateway_t gw);
/**
@@ -45,7 +62,10 @@ void dc_gateway_disconnect(dc_gateway_t gw);
bool dc_gateway_connected(dc_gateway_t gw);
/**
* Process the queue of data that came from the websocket.
* Process the queue of data that came from the websocket. Since the
* gateway handle is not part of whole event_base_loop() shebang, this
* must be called individually. dc_loop_once() will do this for you, if
* you opt to use dc_loop() (which you should).
*/
void dc_gateway_process(dc_gateway_t gw);

View File

@@ -40,11 +40,21 @@ struct event_base *dc_loop_event_base(dc_loop_t l);
*/
void dc_loop_add_api(dc_loop_t loop, dc_api_t api);
/**
* Remove the given API handle from the loop.
*/
void dc_loop_remove_api(dc_loop_t loop, dc_api_t api);
/**
* Add a gateway to be handled with the rest.
*/
void dc_loop_add_gateway(dc_loop_t loop, dc_gateway_t gw);
/**
* Remove the given gateway from the loop.
*/
void dc_loop_remove_gateway(dc_loop_t loop, dc_gateway_t gw);
/**
* Loop once, and process one message in the queues of the event
* base, and one message from the queue of the CURL multi events.

View File

@@ -0,0 +1,58 @@
#ifndef DC_SESSION_H
#define DC_SESSION_H
#include <stdint.h>
#include <dc/api.h>
#include <dc/loop.h>
#include <dc/account.h>
#include <dc/channel.h>
#include <dc/gateway.h>
/**
* A session object will contain all information gathered after a user
* authenticated to the services. It is the "god emperor object" of libdiscord,
* that stores, caches, and provides all information after a log in. From
* available channel objects, account objects of friends, and guild mates,
* to a list of available guilds.
*
* If you consider writing a fully fledged client, or a very sophisticated
* bot, you should consider using this session object in your application.
*
* Please note that the session object attempts to use as little API calls as
* necessary, relying heavily on data provided by the gateway. So it may take
* a bit before appropriate information (i.e. friends) have been loaded.
*/
struct dc_session_;
typedef struct dc_session_ *dc_session_t;
/**
* Creates a new session that will attach itself to the given loop.
*/
dc_session_t dc_session_new(dc_loop_t loop);
/**
* Logs the given user out, and clears all internal data.
*/
bool dc_session_logout(dc_session_t s);
/**
* Logs the given user into the system, starts a websocket and begins to
* collect information about the given login user (channels, guilds,
* friends).
*/
bool dc_session_login(dc_session_t s, dc_account_t login);
bool dc_session_has_token(dc_session_t s);
/**
* Returns the currently logged in user. Which is often called "@me" in
* Discord API.
*/
dc_account_t dc_session_me(dc_session_t s);
bool dc_session_equal_me(dc_session_t s, dc_account_t a);
bool dc_session_equal_me_fullname(dc_session_t s, char const *a);
#endif