Orca
A bot framework that is easy to reason about, easy to debug, and easy to use.
websockets.h
Go to the documentation of this file.
1 
8 #ifndef WEBSOCKETS_H
9 #define WEBSOCKETS_H
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif // __cplusplus
14 
15 #include "logconf.h" /* struct logconf */
16 
17 /* FORWARD DECLARATIONS */
18 struct websockets;
19 
25 enum ws_status {
30 };
31 
32 enum ws_user_cmd {
33  WS_USER_CMD_NONE,
34  WS_USER_CMD_EXIT,
35 };
36 
41  WS_CLOSE_REASON_NORMAL = 1000,
42  WS_CLOSE_REASON_GOING_AWAY = 1001,
43  WS_CLOSE_REASON_PROTOCOL_ERROR = 1002,
44  WS_CLOSE_REASON_UNEXPECTED_DATA = 1003,
45  WS_CLOSE_REASON_NO_REASON = 1005,
46  WS_CLOSE_REASON_ABRUPTLY = 1006,
47  WS_CLOSE_REASON_INCONSISTENT_DATA = 1007,
48  WS_CLOSE_REASON_POLICY_VIOLATION = 1008,
49  WS_CLOSE_REASON_TOO_BIG = 1009,
50  WS_CLOSE_REASON_MISSING_EXTENSION = 1010,
51  WS_CLOSE_REASON_SERVER_ERROR = 1011,
52  WS_CLOSE_REASON_IANA_REGISTRY_START = 3000,
53  WS_CLOSE_REASON_IANA_REGISTRY_END = 3999,
54  WS_CLOSE_REASON_PRIVATE_START = 4000,
55  WS_CLOSE_REASON_PRIVATE_END = 4999
56 };
57 
58 struct ws_callbacks {
64  void (*on_connect)(void *data, const char *protocols);
72  void (*on_text)(void *data, const char *text, size_t len);
76  void (*on_binary)(void *data, const void *mem, size_t len);
83  void (*on_ping)(void *data, const char *reason, size_t len);
87  void (*on_pong)(void *data, const char *reason, size_t len);
94  void (*on_close)(void *data, enum ws_close_reason wscode, const char *reason, size_t len);
98  void *data;
99 };
100 
108 struct websockets* ws_init(struct ws_callbacks *cbs, struct logconf *config);
109 
115 void ws_cleanup(struct websockets *ws);
116 
124 void ws_set_url(struct websockets *ws, const char base_url[], const char ws_protocols[]);
125 
137 bool ws_send_text(struct websockets *ws, char text[], size_t len);
138 
145 void ws_start(struct websockets *ws);
146 
157 void ws_perform(struct websockets *ws, _Bool *is_running, uint64_t wait_ms);
158 
166 uint64_t ws_timestamp(struct websockets *ws);
167 
174 enum ws_status ws_get_status(struct websockets *ws);
175 
182 char* ws_close_opcode_print(enum ws_close_reason opcode);
183 
191 bool ws_is_alive(struct websockets *ws);
192 
199 bool ws_is_functional(struct websockets *ws);
200 
201 void ws_exit_event_loop(struct websockets *ws);
202 
203 bool ws_same_thread(struct websockets *ws);
204 
205 #ifdef __cplusplus
206 }
207 #endif // __cplusplus
208 
209 #endif // WEBSOCKETS_H
WS_DISCONNECTED
@ WS_DISCONNECTED
client disconnected from ws
Definition: websockets.h:26
ws_timestamp
uint64_t ws_timestamp(struct websockets *ws)
The WebSockets handle concept of "now".
ws_init
struct websockets * ws_init(struct ws_callbacks *cbs, struct logconf *config)
Create a new (CURL-based) WebSockets handle.
WS_CONNECTING
@ WS_CONNECTING
client in the process of connecting from ws
Definition: websockets.h:29
ws_start
void ws_start(struct websockets *ws)
Signals connecting state before entering the WebSockets event loop.
ws_set_url
void ws_set_url(struct websockets *ws, const char base_url[], const char ws_protocols[])
Set the URL for the WebSockets handle to connect.
ws_callbacks::on_text
void(* on_text)(void *data, const char *text, size_t len)
Reports UTF-8 text messages.
Definition: websockets.h:72
ws_send_text
bool ws_send_text(struct websockets *ws, char text[], size_t len)
Send a text message of given size.
ws_callbacks::on_connect
void(* on_connect)(void *data, const char *protocols)
Called upon connection.
Definition: websockets.h:64
ws_get_status
enum ws_status ws_get_status(struct websockets *ws)
Returns the WebSockets handle connection status.
ws_callbacks::data
void * data
user arbitrary data to be passed around callbacks
Definition: websockets.h:98
ws_is_functional
bool ws_is_functional(struct websockets *ws)
Check if WebSockets connection is active.
WS_DISCONNECTING
@ WS_DISCONNECTING
client in the process of disconnecting to ws
Definition: websockets.h:28
ws_status
ws_status
The WebSockets client status.
Definition: websockets.h:25
ws_close_reason
ws_close_reason
Definition: websockets.h:40
ws_perform
void ws_perform(struct websockets *ws, _Bool *is_running, uint64_t wait_ms)
Reads/Write available data from WebSockets.
ws_callbacks::on_ping
void(* on_ping)(void *data, const char *reason, size_t len)
reports PING.
Definition: websockets.h:83
ws_cleanup
void ws_cleanup(struct websockets *ws)
Free a WebSockets handle created with ws_init()
ws_is_alive
bool ws_is_alive(struct websockets *ws)
Check if a WebSockets connection is alive.
ws_callbacks::on_pong
void(* on_pong)(void *data, const char *reason, size_t len)
reports PONG.
Definition: websockets.h:87
ws_callbacks::on_close
void(* on_close)(void *data, enum ws_close_reason wscode, const char *reason, size_t len)
reports server closed the connection with the given reason.
Definition: websockets.h:94
ws_callbacks
Definition: websockets.h:58
ws_callbacks::on_binary
void(* on_binary)(void *data, const void *mem, size_t len)
reports binary data.
Definition: websockets.h:76
ws_close_opcode_print
char * ws_close_opcode_print(enum ws_close_reason opcode)
Returns a enum ws_close_reason opcode in a string format.
WS_CONNECTED
@ WS_CONNECTED
client connected to ws
Definition: websockets.h:27