web sockets are hard

This commit is contained in:
2019-07-08 23:29:08 +02:00
parent c3f4f5a180
commit 91bdd23a72
12 changed files with 670 additions and 86 deletions

View File

@@ -5,6 +5,7 @@
#include <dc/account.h>
#include <dc/guild.h>
#include <dc/channel.h>
#include <dc/gateway.h>
#include <stdbool.h>
@@ -35,6 +36,11 @@ json_t *dc_api_call_sync(dc_api_t api, char const *token,
char const *verb, char const *method,
json_t *j);
/**
* Establish a GATEWAY to the discord servers.
*/
dc_gateway_t dc_api_establish_gateway(dc_api_t api, dc_account_t login);
/**
* Authenticate a given user account. The user account should have
* email, and password set. If the auth succeeds the account will have
@@ -44,6 +50,13 @@ json_t *dc_api_call_sync(dc_api_t api, char const *token,
*/
bool dc_api_authenticate(dc_api_t api, dc_account_t account);
/**
* Log the account in. Will first call dc_api_authenticate(), then
* dc_api_get_userinfo(), then dc_api_get_friends(). If
* any of these steps fail, it returns false.
*/
bool dc_api_login(dc_api_t api, dc_account_t account);
/**
* Inverse of dc_api_authenticate(). Logs the given user out, destroying the
* login token in the process.

View File

@@ -0,0 +1,56 @@
#ifndef DC_GATEWAY_H
#define DC_GATEWAY_H
#include <stdint.h>
#include <stdlib.h>
#include <dc/account.h>
#include <curl/curl.h>
struct dc_gateway_;
typedef struct dc_gateway_ *dc_gateway_t;
typedef enum {
GATEWAY_OPCODE_HEARTBEAT = 1,
GATEWAY_OPCODE_IDENTIFY = 2,
GATEWAY_OPCODE_HELLO = 10,
} dc_gateway_opcode_t;
dc_gateway_t dc_gateway_new(void);
void dc_gateway_set_login(dc_gateway_t gw, dc_account_t login);
/**
* Set all required CURL handles. The object will delete the easy handle
* and make sure it is removed from the multi handle upon unref. Do not
* free the multi handle before you remove the gateway.
*/
void dc_gateway_set_curl(dc_gateway_t gw, CURLM *multi, CURL *easy);
CURL *dc_gateway_curl(dc_gateway_t gw);
/**
* Returns a CURL slist that lasts as long as the handle itself lasts
*/
struct curl_slist * dc_gateway_slist(dc_gateway_t gw);
/**
* To be used as a WRITEFUNCTION for a curl handle. Don't forget to set the
* gateway handle as a WRITEDATA too, otherwise this will have no effect.
*/
size_t dc_gateway_writefunc(char *ptr, size_t sz, size_t nmemb, void *data);
/**
* Process the queue of data that came from the websocket.
*/
void dc_gateway_process(dc_gateway_t gw);
/**
* utility function to make a websocket frame
*/
uint8_t *
dc_gateway_makeframe(uint8_t const *d, size_t data_len,
uint8_t type, size_t *outlen);
#endif

View File

@@ -2,6 +2,7 @@
#define DC_LOOP_H
#include <dc/api.h>
#include <dc/gateway.h>
#include <event.h>
#include <curl/curl.h>
@@ -39,6 +40,11 @@ struct event_base *dc_loop_event_base(dc_loop_t l);
*/
void dc_loop_add_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);
/**
* Loop once, and process one message in the queues of the event
* base, and one message from the queue of the CURL multi events.