Concord - C Discord API library
A Discord API wrapper library written in C
websockets.h
Go to the documentation of this file.
1
5#ifndef WEBSOCKETS_H
6#define WEBSOCKETS_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif /* __cplusplus */
11
12#include <curl/curl.h>
13
14#include "error.h" /* CCORDcode */
15#include "logconf.h" /* logging facilities */
16
23struct websockets;
24
28struct ws_info {
33};
34
49};
50
72};
73
81 void (*on_connect)(void *data,
82 struct websockets *ws,
83 struct ws_info *info,
84 const char *protocols);
85
93 void (*on_text)(void *data,
94 struct websockets *ws,
95 struct ws_info *info,
96 const char *text,
97 size_t len);
98
100 void (*on_binary)(void *data,
101 struct websockets *ws,
102 struct ws_info *info,
103 const void *mem,
104 size_t len);
111 void (*on_ping)(void *data,
112 struct websockets *ws,
113 struct ws_info *info,
114 const char *reason,
115 size_t len);
116
118 void (*on_pong)(void *data,
119 struct websockets *ws,
120 struct ws_info *info,
121 const char *reason,
122 size_t len);
123
130 void (*on_close)(void *data,
131 struct websockets *ws,
132 struct ws_info *info,
133 enum ws_close_reason wscode,
134 const char *reason,
135 size_t len);
136
138 void *data;
139};
140
142struct ws_attr {
144 struct logconf *conf;
145};
146
156#define ws_is_alive(ws) (ws_get_status(ws) != WS_DISCONNECTED)
157
166#define ws_is_functional(ws) (ws_get_status(ws) == WS_CONNECTED)
167
177struct websockets *ws_init(struct ws_callbacks *cbs,
178 CURLM *mhandle,
179 struct ws_attr *attr);
180
186void ws_cleanup(struct websockets *ws);
187
195void ws_set_url(struct websockets *ws,
196 const char base_url[],
197 const char ws_protocols[]);
198
211_Bool ws_send_binary(struct websockets *ws,
212 struct ws_info *info,
213 const char msg[],
214 size_t msglen);
227_Bool ws_send_text(struct websockets *ws,
228 struct ws_info *info,
229 const char text[],
230 size_t len);
241_Bool ws_ping(struct websockets *ws,
242 struct ws_info *info,
243 const char reason[],
244 size_t len);
258_Bool ws_pong(struct websockets *ws,
259 struct ws_info *info,
260 const char reason[],
261 size_t len);
262
269CURL* ws_start(struct websockets *ws);
270
276void ws_end(struct websockets *ws);
277
290_Bool ws_easy_run(struct websockets *ws, uint64_t wait_ms, uint64_t *tstamp);
291
300_Bool ws_multi_socket_run(struct websockets *ws, uint64_t *tstamp);
301
309
316const char *ws_close_opcode_print(enum ws_close_reason opcode);
317
326uint64_t ws_timestamp(struct websockets *ws);
327
334uint64_t ws_timestamp_update(struct websockets *ws);
335
347void ws_close(struct websockets *ws,
348 const enum ws_close_reason code,
349 const char reason[],
350 const size_t len);
351
359void ws_add_header(struct websockets *ws,
360 const char field[],
361 const char value[]);
362
363#ifdef __cplusplus
364}
365#endif /* __cplusplus */
366
367#endif /* WEBSOCKETS_H */
CCORDcode
Definition: error.h:10
A stackful and modularized wrapper over the popular 'log.c' facilities.
Definition: logconf.h:216
Store logging information from log_http()
Definition: logconf.h:248
Opaque handler for WebSockets.
WebSockets handle initialization attributes.
Definition: websockets.h:142
struct logconf * conf
Definition: websockets.h:144
WebSockets callbacks.
Definition: websockets.h:75
void(* on_close)(void *data, struct websockets *ws, struct ws_info *info, enum ws_close_reason wscode, const char *reason, size_t len)
reports server closed the connection with the given reason.
Definition: websockets.h:130
void * data
user arbitrary data to be passed around callbacks
Definition: websockets.h:138
void(* on_pong)(void *data, struct websockets *ws, struct ws_info *info, const char *reason, size_t len)
reports PONG.
Definition: websockets.h:118
void(* on_text)(void *data, struct websockets *ws, struct ws_info *info, const char *text, size_t len)
Reports UTF-8 text messages.
Definition: websockets.h:93
void(* on_binary)(void *data, struct websockets *ws, struct ws_info *info, const void *mem, size_t len)
reports binary data.
Definition: websockets.h:100
void(* on_connect)(void *data, struct websockets *ws, struct ws_info *info, const char *protocols)
Called upon connection.
Definition: websockets.h:81
void(* on_ping)(void *data, struct websockets *ws, struct ws_info *info, const char *reason, size_t len)
reports PING.
Definition: websockets.h:111
Stores info on the latest transfer performed via websockets.
Definition: websockets.h:28
CCORDcode code
Definition: websockets.h:32
_Bool ws_multi_socket_run(struct websockets *ws, uint64_t *tstamp)
Reads/Write available data from WebSockets.
_Bool ws_send_text(struct websockets *ws, struct ws_info *info, const char text[], size_t len)
Send a text message of given size.
void ws_add_header(struct websockets *ws, const char field[], const char value[])
Add a header field/value pair.
ws_status
The WebSockets client status.
Definition: websockets.h:40
@ WS_DISCONNECTING
Definition: websockets.h:46
@ WS_CONNECTING
Definition: websockets.h:48
@ WS_DISCONNECTED
Definition: websockets.h:42
@ WS_CONNECTED
Definition: websockets.h:44
uint64_t ws_timestamp_update(struct websockets *ws)
Update the WebSockets event-loop concept of "now".
void ws_close(struct websockets *ws, const enum ws_close_reason code, const char reason[], const size_t len)
Thread-safe way to stop websockets connection.
_Bool ws_pong(struct websockets *ws, struct ws_info *info, const char reason[], size_t len)
Send a PONG (opcode 0xA) frame with reason as payload.
uint64_t ws_timestamp(struct websockets *ws)
The WebSockets event-loop concept of "now".
_Bool ws_ping(struct websockets *ws, struct ws_info *info, const char reason[], size_t len)
Send a PING (opcode 0x9) frame with reason as payload.
const char * ws_close_opcode_print(enum ws_close_reason opcode)
Returns a enum ws_close_reason opcode in a string format.
CURL * ws_start(struct websockets *ws)
Signals connecting state before entering the WebSockets event loop.
_Bool ws_easy_run(struct websockets *ws, uint64_t wait_ms, uint64_t *tstamp)
Reads/Write available data from WebSockets.
ws_close_reason
WebSockets CLOSE opcodes.
Definition: websockets.h:56
@ WS_CLOSE_REASON_INCONSISTENT_DATA
Definition: websockets.h:63
@ WS_CLOSE_REASON_ABRUPTLY
Definition: websockets.h:62
@ WS_CLOSE_REASON_TOO_BIG
Definition: websockets.h:65
@ WS_CLOSE_REASON_MISSING_EXTENSION
Definition: websockets.h:66
@ WS_CLOSE_REASON_PRIVATE_START
Definition: websockets.h:70
@ WS_CLOSE_REASON_UNEXPECTED_DATA
Definition: websockets.h:60
@ WS_CLOSE_REASON_NO_REASON
Definition: websockets.h:61
@ WS_CLOSE_REASON_GOING_AWAY
Definition: websockets.h:58
@ WS_CLOSE_REASON_PRIVATE_END
Definition: websockets.h:71
@ WS_CLOSE_REASON_POLICY_VIOLATION
Definition: websockets.h:64
@ WS_CLOSE_REASON_IANA_REGISTRY_START
Definition: websockets.h:68
@ WS_CLOSE_REASON_NORMAL
Definition: websockets.h:57
@ WS_CLOSE_REASON_IANA_REGISTRY_END
Definition: websockets.h:69
@ WS_CLOSE_REASON_PROTOCOL_ERROR
Definition: websockets.h:59
@ WS_CLOSE_REASON_SERVER_ERROR
Definition: websockets.h:67
_Bool ws_send_binary(struct websockets *ws, struct ws_info *info, const char msg[], size_t msglen)
Send a binary message of given size.
void ws_set_url(struct websockets *ws, const char base_url[], const char ws_protocols[])
Set the URL for the WebSockets handle to connect.
enum ws_status ws_get_status(struct websockets *ws)
Returns the WebSockets handle connection status.
void ws_cleanup(struct websockets *ws)
Free a WebSockets handle created with ws_init()
void ws_end(struct websockets *ws)
Cleanup and reset ws connection resources.
struct websockets * ws_init(struct ws_callbacks *cbs, CURLM *mhandle, struct ws_attr *attr)
Create a new (CURL-based) WebSockets handle.