Orca
A bot framework that is easy to reason about, easy to debug, and easy to use.
Classes | Enumerations | Functions
websockets.h File Reference

File containing internal functions and datatypes for WebSockets client. More...

#include "logconf.h"
Include dependency graph for websockets.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  ws_callbacks
 

Enumerations

enum  ws_status { WS_DISCONNECTED = 0, WS_CONNECTED, WS_DISCONNECTING, WS_CONNECTING }
 The WebSockets client status. More...
 
enum  ws_user_cmd { WS_USER_CMD_NONE, WS_USER_CMD_EXIT }
 
enum  ws_close_reason {
  WS_CLOSE_REASON_NORMAL = 1000, WS_CLOSE_REASON_GOING_AWAY = 1001, WS_CLOSE_REASON_PROTOCOL_ERROR = 1002, WS_CLOSE_REASON_UNEXPECTED_DATA = 1003,
  WS_CLOSE_REASON_NO_REASON = 1005, WS_CLOSE_REASON_ABRUPTLY = 1006, WS_CLOSE_REASON_INCONSISTENT_DATA = 1007, WS_CLOSE_REASON_POLICY_VIOLATION = 1008,
  WS_CLOSE_REASON_TOO_BIG = 1009, WS_CLOSE_REASON_MISSING_EXTENSION = 1010, WS_CLOSE_REASON_SERVER_ERROR = 1011, WS_CLOSE_REASON_IANA_REGISTRY_START = 3000,
  WS_CLOSE_REASON_IANA_REGISTRY_END = 3999, WS_CLOSE_REASON_PRIVATE_START = 4000, WS_CLOSE_REASON_PRIVATE_END = 4999
}
 

Functions

struct websockets * ws_init (struct ws_callbacks *cbs, struct logconf *config)
 Create a new (CURL-based) WebSockets handle. More...
 
void ws_cleanup (struct websockets *ws)
 Free a WebSockets handle created with ws_init() More...
 
void ws_set_url (struct websockets *ws, const char base_url[], const char ws_protocols[])
 Set the URL for the WebSockets handle to connect. More...
 
bool ws_send_text (struct websockets *ws, char text[], size_t len)
 Send a text message of given size. More...
 
void ws_start (struct websockets *ws)
 Signals connecting state before entering the WebSockets event loop. More...
 
void ws_perform (struct websockets *ws, _Bool *is_running, uint64_t wait_ms)
 Reads/Write available data from WebSockets. More...
 
uint64_t ws_timestamp (struct websockets *ws)
 The WebSockets handle concept of "now". More...
 
enum ws_status ws_get_status (struct websockets *ws)
 Returns the WebSockets handle connection status. More...
 
char * ws_close_opcode_print (enum ws_close_reason opcode)
 Returns a enum ws_close_reason opcode in a string format. More...
 
bool ws_is_alive (struct websockets *ws)
 Check if a WebSockets connection is alive. More...
 
bool ws_is_functional (struct websockets *ws)
 Check if WebSockets connection is active. More...
 
void ws_exit_event_loop (struct websockets *ws)
 
bool ws_same_thread (struct websockets *ws)
 

Detailed Description

File containing internal functions and datatypes for WebSockets client.

Author
cee-studio
Date
21 Jun 2021

Enumeration Type Documentation

◆ ws_close_reason

◆ ws_status

enum ws_status

The WebSockets client status.

See also
ws_get_status()
Enumerator
WS_DISCONNECTED 

client disconnected from ws

WS_CONNECTED 

client connected to ws

WS_DISCONNECTING 

client in the process of disconnecting to ws

WS_CONNECTING 

client in the process of connecting from ws

Function Documentation

◆ ws_cleanup()

void ws_cleanup ( struct websockets *  ws)

Free a WebSockets handle created with ws_init()

Parameters
wsthe WebSockets handle created with ws_init()

◆ 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.

Parameters
opcodethe opcode to be converted to string
Returns
a read-only string literal of the opcode

◆ ws_get_status()

enum ws_status ws_get_status ( struct websockets *  ws)

Returns the WebSockets handle connection status.

Parameters
wsthe WebSockets handle created with ws_init()
Returns
a ws_status opcode

◆ ws_init()

struct websockets* ws_init ( struct ws_callbacks cbs,
struct logconf *  config 
)

Create a new (CURL-based) WebSockets handle.

Parameters
cbsset of functions to call back when server report events.
configoptional pointer to a pre-initialized logconf
Returns
newly created WebSockets handle, free with ws_cleanup()

◆ ws_is_alive()

bool ws_is_alive ( struct websockets *  ws)

Check if a WebSockets connection is alive.

Parameters
wsthe WebSockets handle created with ws_init()
Returns
TRUE if WebSockets status is different than WS_DISCONNECTED, FALSE otherwise.

◆ ws_is_functional()

bool ws_is_functional ( struct websockets *  ws)

Check if WebSockets connection is active.

Parameters
wsthe WebSockets handle created with ws_init()
Returns
true if is function, false otherwise

◆ ws_perform()

void ws_perform ( struct websockets *  ws,
_Bool *  is_running,
uint64_t  wait_ms 
)

Reads/Write available data from WebSockets.

Helper over curl_multi_perform()

Parameters
wsthe WebSockets handle created with ws_init()
is_runningreceives true if the client is running and false otherwise
wait_mslimit amount in milliseconds to wait for until activity
See also
https://curl.se/libcurl/c/curl_multi_perform.html

◆ ws_send_text()

bool ws_send_text ( struct websockets *  ws,
char  text[],
size_t  len 
)

Send a text message of given size.

Text messages do not need to include the null terminator (\0), they will be read up to len.

Parameters
wsthe WebSockets handle created with ws_init()
textthe pointer to memory (linear) to send.
lenthe length in bytes of text.
Returns
true if sent, false on errors.

◆ 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.

Parameters
wsthe WebSockets handle created with ws_init()
base_urlthe URL to connect, such as ws://echo.websockets.org
ws_protocolsNULL or something like "chat", "superchat",...

◆ ws_start()

void ws_start ( struct websockets *  ws)

Signals connecting state before entering the WebSockets event loop.

Parameters
wsthe WebSockets handle created with ws_init()
Note
Helper over _ws_set_status(ws, WS_CONNECTING)

◆ ws_timestamp()

uint64_t ws_timestamp ( struct websockets *  ws)

The WebSockets handle concept of "now".

Parameters
wsthe WebSockets handle created with ws_init()
Returns
the timestamp in milliseconds from when ws_perform() was last called
Note
the timestamp is updated at every ws_perform() call