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 <stdint.h>
13#include <curl/curl.h>
14
21struct websockets;
22
23/* forward declaration */
24struct logmod;
25
26
41};
42
64};
65
71 void (*on_connect)(void *data, struct websockets *ws);
72
80 void (*on_text)(void *data,
81 struct websockets *ws,
82 const char *text,
83 size_t len);
84
86 void (*on_binary)(void *data,
87 struct websockets *ws,
88 const void *mem,
89 size_t len);
96 void (*on_ping)(void *data,
97 struct websockets *ws,
98 const char *reason,
99 size_t len);
100
102 void (*on_pong)(void *data,
103 struct websockets *ws,
104 const char *reason,
105 size_t len);
106
113 void (*on_close)(void *data,
114 struct websockets *ws,
115 enum ws_close_reason wscode,
116 const char *reason,
117 size_t len);
118
120 void *data;
121};
122
132#define ws_is_alive(ws) (ws_get_status(ws) != WS_DISCONNECTED)
133
142#define ws_is_functional(ws) (ws_get_status(ws) == WS_CONNECTED)
143
154struct websockets *ws_init(struct ws_callbacks *cbs,
155 CURLM *mhandle,
156 struct logmod *logmod,
157 FILE *fp);
158
164void ws_cleanup(struct websockets *ws);
165
172void ws_set_url(struct websockets *ws, const char base_url[]);
173
185_Bool ws_send_binary(struct websockets *ws, const char msg[], size_t msglen);
197_Bool ws_send_text(struct websockets *ws, const char text[], size_t len);
207_Bool ws_ping(struct websockets *ws, const char reason[], size_t len);
220_Bool ws_pong(struct websockets *ws, const char reason[], size_t len);
221
228CURL *ws_start(struct websockets *ws);
229
235void ws_end(struct websockets *ws);
236
249_Bool ws_easy_run(struct websockets *ws, uint64_t wait_ms, uint64_t *tstamp);
250
259_Bool ws_multi_socket_run(struct websockets *ws, uint64_t *tstamp);
260
268
275const char *ws_close_opcode_print(enum ws_close_reason opcode);
276
285uint64_t ws_timestamp(struct websockets *ws);
286
293uint64_t ws_timestamp_update(struct websockets *ws);
294
306void ws_close(struct websockets *ws,
307 const enum ws_close_reason code,
308 const char reason[],
309 const size_t len);
310
318void ws_add_header(struct websockets *ws,
319 const char field[],
320 const char value[]);
321
322#ifdef __cplusplus
323}
324#endif /* __cplusplus */
325
326#endif /* WEBSOCKETS_H */
Main logging context structure.
Definition: logmod.h:240
Opaque handler for WebSockets.
WebSockets callbacks.
Definition: websockets.h:67
void(* on_binary)(void *data, struct websockets *ws, const void *mem, size_t len)
reports binary data.
Definition: websockets.h:86
void * data
user arbitrary data to be passed around callbacks
Definition: websockets.h:120
void(* on_text)(void *data, struct websockets *ws, const char *text, size_t len)
Reports UTF-8 text messages.
Definition: websockets.h:80
void(* on_connect)(void *data, struct websockets *ws)
Called upon connection.
Definition: websockets.h:71
void(* on_close)(void *data, struct websockets *ws, enum ws_close_reason wscode, const char *reason, size_t len)
reports server closed the connection with the given reason.
Definition: websockets.h:113
void(* on_ping)(void *data, struct websockets *ws, const char *reason, size_t len)
reports PING.
Definition: websockets.h:96
void(* on_pong)(void *data, struct websockets *ws, const char *reason, size_t len)
reports PONG.
Definition: websockets.h:102
void ws_set_url(struct websockets *ws, const char base_url[])
Set the URL for the WebSockets handle to connect.
_Bool ws_multi_socket_run(struct websockets *ws, uint64_t *tstamp)
Reads/Write available data from WebSockets.
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:32
@ WS_DISCONNECTING
Definition: websockets.h:38
@ WS_CONNECTING
Definition: websockets.h:40
@ WS_DISCONNECTED
Definition: websockets.h:34
@ WS_CONNECTED
Definition: websockets.h:36
uint64_t ws_timestamp_update(struct websockets *ws)
Update the WebSockets event-loop concept of "now".
_Bool ws_pong(struct websockets *ws, const char reason[], size_t len)
Send a PONG (opcode 0xA) frame with reason as payload.
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_ping(struct websockets *ws, const char reason[], size_t len)
Send a PING (opcode 0x9) frame with reason as payload.
uint64_t ws_timestamp(struct websockets *ws)
The WebSockets event-loop concept of "now".
_Bool ws_send_binary(struct websockets *ws, const char msg[], size_t msglen)
Send a binary message of given size.
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.
struct websockets * ws_init(struct ws_callbacks *cbs, CURLM *mhandle, struct logmod *logmod, FILE *fp)
Create a new (CURL-based) WebSockets handle.
_Bool ws_easy_run(struct websockets *ws, uint64_t wait_ms, uint64_t *tstamp)
Reads/Write available data from WebSockets.
_Bool ws_send_text(struct websockets *ws, const char text[], size_t len)
Send a text message of given size.
ws_close_reason
WebSockets CLOSE opcodes.
Definition: websockets.h:48
@ WS_CLOSE_REASON_INCONSISTENT_DATA
Definition: websockets.h:55
@ WS_CLOSE_REASON_ABRUPTLY
Definition: websockets.h:54
@ WS_CLOSE_REASON_TOO_BIG
Definition: websockets.h:57
@ WS_CLOSE_REASON_MISSING_EXTENSION
Definition: websockets.h:58
@ WS_CLOSE_REASON_PRIVATE_START
Definition: websockets.h:62
@ WS_CLOSE_REASON_UNEXPECTED_DATA
Definition: websockets.h:52
@ WS_CLOSE_REASON_NO_REASON
Definition: websockets.h:53
@ WS_CLOSE_REASON_GOING_AWAY
Definition: websockets.h:50
@ WS_CLOSE_REASON_PRIVATE_END
Definition: websockets.h:63
@ WS_CLOSE_REASON_POLICY_VIOLATION
Definition: websockets.h:56
@ WS_CLOSE_REASON_IANA_REGISTRY_START
Definition: websockets.h:60
@ WS_CLOSE_REASON_NORMAL
Definition: websockets.h:49
@ WS_CLOSE_REASON_IANA_REGISTRY_END
Definition: websockets.h:61
@ WS_CLOSE_REASON_PROTOCOL_ERROR
Definition: websockets.h:51
@ WS_CLOSE_REASON_SERVER_ERROR
Definition: websockets.h:59
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.