ncdc/libdc/include/dc/gateway.h

115 lines
3.6 KiB
C
Raw Permalink Normal View History

2019-07-20 15:22:51 +02:00
/*
* Part of ncdc - a discord client for the console
* Copyright (C) 2019 Florian Stinglmayr <fstinglmayr@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-07-08 23:29:08 +02:00
#ifndef DC_GATEWAY_H
#define DC_GATEWAY_H
#include <stdint.h>
#include <stdlib.h>
#include <dc/account.h>
2019-07-10 14:30:37 +02:00
#include <dc/event.h>
2019-07-08 23:29:08 +02:00
struct dc_gateway_;
typedef struct dc_gateway_ *dc_gateway_t;
2019-07-10 14:30:37 +02:00
/**
* 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*);
2019-07-08 23:29:08 +02:00
typedef enum {
2019-07-09 21:46:17 +02:00
GATEWAY_OPCODE_EVENT = 0,
2019-07-09 21:44:23 +02:00
GATEWAY_OPCODE_PING = 1,
2019-07-08 23:29:08 +02:00
GATEWAY_OPCODE_IDENTIFY = 2,
2019-07-09 21:44:23 +02:00
GATEWAY_OPCODE_UPDATE = 3,
2019-07-08 23:29:08 +02:00
GATEWAY_OPCODE_HELLO = 10,
2019-07-09 21:44:23 +02:00
GATEWAY_OPCODE_PONG = 11,
2019-07-08 23:29:08 +02:00
} dc_gateway_opcode_t;
2019-07-09 21:44:23 +02:00
typedef enum {
GATEWAY_FRAME_TEXT_DATA = 129,
GATEWAY_FRAME_DISCONNECT = 136,
GATEWAY_FRAME_PING = 137,
GATEWAY_FRAME_PONG = 138,
} dc_gateway_frames_t;
2019-07-08 23:29:08 +02:00
dc_gateway_t dc_gateway_new(void);
void dc_gateway_set_login(dc_gateway_t gw, dc_account_t login);
2019-07-10 14:30:37 +02:00
void dc_gateway_set_callback(dc_gateway_t gw, dc_gateway_event_callback_t c,
void *userdata);
/**
* Returns the socket of the current gateway handle.
*/
int dc_gateway_socket(dc_gateway_t gw);
2019-07-10 14:30:37 +02:00
/**
* Connect the given gateway. Does nothing if the gateway is already
* connected.
*/
2019-07-09 21:44:23 +02:00
bool dc_gateway_connect(dc_gateway_t gw);
2019-07-08 23:29:08 +02:00
/**
2019-07-09 21:44:23 +02:00
* Cleans up the easy handle, and thus disconnects from the socket handle
* immediately. After this call dc_gateway_connected() will return false.
2019-07-08 23:29:08 +02:00
*/
2019-07-09 21:44:23 +02:00
void dc_gateway_disconnect(dc_gateway_t gw);
2019-07-08 23:29:08 +02:00
/**
2019-07-09 21:44:23 +02:00
* Returns true if the gateway is still connected.
2019-07-08 23:29:08 +02:00
*/
2019-07-09 21:44:23 +02:00
bool dc_gateway_connected(dc_gateway_t gw);
2019-07-08 23:29:08 +02:00
/**
* This method should be called whenever data is available on the socket of
* the gateway that should be read and processed. This function returns false
* if a disconnect happened, and no more calls to this function should be made
* in the feature. This method is useful if you are using an event loop (or
* select) and wish to notify the gateway that data is ready.
*/
bool dc_gateway_process_read(dc_gateway_t gw);
/**
* This method should be called whenever the socket is ready to send data. The
* method will check internal queues for messages that require sending, and will
* also handle the heartbeat. If the gateway closed this function returns false.
2019-07-08 23:29:08 +02:00
*/
bool dc_gateway_process_write(dc_gateway_t gw);
2019-07-08 23:29:08 +02:00
/**
* 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);
2019-07-09 21:44:23 +02:00
size_t
dc_gateway_parseframe(uint8_t const *data, size_t datalen,
uint8_t *type, uint8_t **outdata, size_t *outlen);
2019-07-08 23:29:08 +02:00
#endif