wsServer constants and functions. More...
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
Go to the source code of this file.
Classes | |
struct | ws_events |
events Web Socket events types. More... | |
struct | ws_server_t |
server Web Socket server parameters More... | |
Macros | |
#define | WS_CLSE_INVUTF8 1007 |
Inconsistent message (invalid utf-8) More... | |
#define | SEND(client, buf, len) send_all((client), (buf), (len), MSG_NOSIGNAL) |
#define | RECV(fd, buf, len) recv((fd)->client_sock, (buf), (len), 0) |
Global configurations | |
#define | MAX_CLIENTS 8 |
Max clients connected simultaneously. More... | |
Key and message configurations. | |
#define | MESSAGE_LENGTH 2048 |
Message buffer length. More... | |
#define | MAX_FRAME_LENGTH (16*1024*1024) |
Maximum frame/message length. More... | |
#define | WS_KEY_LEN 24 |
WebSocket key length. More... | |
#define | WS_MS_LEN 36 |
Magic string length. More... | |
#define | WS_KEYMS_LEN (WS_KEY_LEN + WS_MS_LEN) |
Accept message response length. More... | |
#define | MAGIC_STRING "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" |
Magic string. More... | |
Handshake constants. | |
#define | WS_HS_REQ "Sec-WebSocket-Key" |
Alias for 'Sec-WebSocket-Key'. More... | |
#define | WS_HS_ACCLEN 130 |
Handshake accept message length. More... | |
#define | WS_HS_ACCEPT |
Handshake accept message. More... | |
#define | DEBUG(...) |
Debug. More... | |
Frame types. | |
#define | WS_FIN 128 |
Frame FIN. More... | |
#define | WS_FIN_SHIFT 7 |
Frame FIN shift. More... | |
#define | WS_FR_OP_CONT 0 |
Continuation frame. More... | |
#define | WS_FR_OP_TXT 1 |
Text frame. More... | |
#define | WS_FR_OP_BIN 2 |
Binary frame. More... | |
#define | WS_FR_OP_CLSE 8 |
Close frame. More... | |
#define | WS_FR_OP_PING 0x9 |
Ping frame. More... | |
#define | WS_FR_OP_PONG 0xA |
Pong frame. More... | |
#define | WS_FR_OP_UNSUPPORTED 0xF |
Unsupported frame. More... | |
Close codes | |
#define | WS_CLSE_NORMAL 1000 |
Normal close. More... | |
#define | WS_CLSE_PROTERR 1002 |
Protocol error. More... | |
Connection states | |
#define | WS_STATE_CONNECTING 0 |
Connection not established yet. More... | |
#define | WS_STATE_OPEN 1 |
Communicating. More... | |
#define | WS_STATE_CLOSING 2 |
Closing state. More... | |
#define | WS_STATE_CLOSED 3 |
Closed. More... | |
Timeout util | |
#define | MS_TO_NS(x) ((x)*1000000) |
Nanoseconds macro converter. More... | |
#define | TIMEOUT_MS (500) |
Timeout in milliseconds. More... | |
Functions | |
void * | ws_get_server_context (ws_cli_conn_t *cli) |
Get server context. More... | |
void | ws_set_connection_context (ws_cli_conn_t *cli, void *ptr) |
Set connection context. More... | |
void * | ws_get_connection_context (ws_cli_conn_t *cli) |
Get connection context. More... | |
int | get_handshake_accept (char *wsKey, unsigned char **dest) |
Gets the field Sec-WebSocket-Accept on response, by an previously informed key. More... | |
int | get_handshake_response (char *hsrequest, char **hsresponse) |
Gets the complete response to accomplish a succesfully handshake. More... | |
char * | ws_getaddress (ws_cli_conn_t *client) |
Gets the IP address relative to a client connection opened by the server. More... | |
char * | ws_getport (ws_cli_conn_t *client) |
Gets the IP port relative to a client connection opened by the server. More... | |
int | ws_sendframe (ws_cli_conn_t *cli, const char *msg, uint64_t size, int type) |
Send an WebSocket frame with some payload data. More... | |
int | ws_sendframe_bcast (uint16_t port, const char *msg, uint64_t size, int type) |
Send an WebSocket frame with some payload data to all clients connected into the same port. More... | |
int | ws_sendframe_txt (ws_cli_conn_t *cli, const char *msg) |
Sends a WebSocket text frame. More... | |
int | ws_sendframe_txt_bcast (uint16_t port, const char *msg) |
Sends a broadcast WebSocket text frame. More... | |
int | ws_sendframe_bin (ws_cli_conn_t *cli, const char *msg, uint64_t size) |
Sends a WebSocket binary frame. More... | |
int | ws_sendframe_bin_bcast (uint16_t port, const char *msg, uint64_t size) |
Sends a broadcast WebSocket binary frame. More... | |
int | ws_get_state (ws_cli_conn_t *cli) |
For a given client , gets the current state for the connection, or -1 if invalid. More... | |
int | ws_close_client (ws_cli_conn_t *cli) |
Close the client connection for the given client with normal close code (1000) and no reason string. More... | |
int | ws_socket (struct ws_server *ws_srv) |
Main loop for the server. More... | |
void | ws_ping (ws_cli_conn_t *cli, int threshold) |
Sends a PING frame to the client cli with threshold threshold . More... | |
wsServer constants and functions.
#define DEBUG | ( | ... | ) |
Debug.
#define MAGIC_STRING "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" |
Magic string.
#define MAX_CLIENTS 8 |
Max clients connected simultaneously.
#define MAX_FRAME_LENGTH (16*1024*1024) |
Maximum frame/message length.
#define MESSAGE_LENGTH 2048 |
Message buffer length.
#define MS_TO_NS | ( | x | ) | ((x)*1000000) |
Nanoseconds macro converter.
#define RECV | ( | fd, | |
buf, | |||
len | |||
) | recv((fd)->client_sock, (buf), (len), 0) |
#define SEND | ( | client, | |
buf, | |||
len | |||
) | send_all((client), (buf), (len), MSG_NOSIGNAL) |
#define TIMEOUT_MS (500) |
Timeout in milliseconds.
#define WS_CLSE_INVUTF8 1007 |
Inconsistent message (invalid utf-8)
#define WS_CLSE_NORMAL 1000 |
Normal close.
#define WS_CLSE_PROTERR 1002 |
Protocol error.
#define WS_FIN 128 |
Frame FIN.
#define WS_FIN_SHIFT 7 |
Frame FIN shift.
#define WS_FR_OP_BIN 2 |
Binary frame.
#define WS_FR_OP_CLSE 8 |
Close frame.
#define WS_FR_OP_CONT 0 |
Continuation frame.
#define WS_FR_OP_PING 0x9 |
Ping frame.
#define WS_FR_OP_PONG 0xA |
Pong frame.
#define WS_FR_OP_TXT 1 |
Text frame.
#define WS_FR_OP_UNSUPPORTED 0xF |
Unsupported frame.
#define WS_HS_ACCEPT |
Handshake accept message.
#define WS_HS_ACCLEN 130 |
Handshake accept message length.
#define WS_HS_REQ "Sec-WebSocket-Key" |
Alias for 'Sec-WebSocket-Key'.
#define WS_KEY_LEN 24 |
WebSocket key length.
#define WS_KEYMS_LEN (WS_KEY_LEN + WS_MS_LEN) |
Accept message response length.
#define WS_MS_LEN 36 |
Magic string length.
#define WS_STATE_CLOSED 3 |
Closed.
#define WS_STATE_CLOSING 2 |
Closing state.
#define WS_STATE_CONNECTING 0 |
Connection not established yet.
#define WS_STATE_OPEN 1 |
Communicating.
int get_handshake_accept | ( | char * | wsKey, |
unsigned char ** | dest | ||
) |
Gets the field Sec-WebSocket-Accept on response, by an previously informed key.
wsKey | Sec-WebSocket-Key |
dest | source to be stored the value. |
int get_handshake_response | ( | char * | hsrequest, |
char ** | hsresponse | ||
) |
Gets the complete response to accomplish a succesfully handshake.
hsrequest | Client request. |
hsresponse | Server response. |
int ws_close_client | ( | ws_cli_conn_t * | client | ) |
Close the client connection for the given client
with normal close code (1000) and no reason string.
client | Client connection. |
void* ws_get_connection_context | ( | ws_cli_conn_t * | cli | ) |
Get connection context.
void* ws_get_server_context | ( | ws_cli_conn_t * | cli | ) |
int ws_get_state | ( | ws_cli_conn_t * | client | ) |
For a given client
, gets the current state for the connection, or -1 if invalid.
client | Client connection. |
client
.char* ws_getaddress | ( | ws_cli_conn_t * | client | ) |
Gets the IP address relative to a client connection opened by the server.
client | Client connection. |
char* ws_getport | ( | ws_cli_conn_t * | client | ) |
Gets the IP port relative to a client connection opened by the server.
client | Client connection. |
void ws_ping | ( | ws_cli_conn_t * | cli, |
int | threshold | ||
) |
Sends a PING frame to the client cli
with threshold threshold
.
This routine sends a PING to a single client pointed to by cli
or a broadcast PING if cli
is NULL. If the specified client does not respond up to threshold
PINGs, the connection is aborted.
ws_ping() is not automatic: the user who wants to send keep-alive PINGs must call this routine in a timely manner, whether on a different thread or inside an event.
See examples/ping/ping.c for a minimal example usage.
cli | Client to be sent, if NULL, broadcast. |
threshold | How many ignored PINGs should tolerate? (should be positive and greater than 0). |
It is also important to note that for devices with unstable connections (such as a weak WiFi signal or 3/4/5G from a cell phone), a threshold greater than 1 is advisable.
int ws_sendframe | ( | ws_cli_conn_t * | client, |
const char * | msg, | ||
uint64_t | size, | ||
int | type | ||
) |
Send an WebSocket frame with some payload data.
client | Target to be send. If NULL, broadcast the message. |
msg | Message to be send. |
size | Binary message size. |
type | Frame type. |
size
is -1, it is assumed that a text frame is being sent, otherwise, a binary frame. In the later case, the size
is used. int ws_sendframe_bcast | ( | uint16_t | port, |
const char * | msg, | ||
uint64_t | size, | ||
int | type | ||
) |
Send an WebSocket frame with some payload data to all clients connected into the same port.
port | Server listen port to broadcast message. |
msg | Message to be send. |
size | Binary message size. |
type | Frame type. |
size
is -1, it is assumed that a text frame is being sent, otherwise, a binary frame. In the later case, the size
is used. int ws_sendframe_bin | ( | ws_cli_conn_t * | client, |
const char * | msg, | ||
uint64_t | size | ||
) |
Sends a WebSocket binary frame.
client | Target to be send. |
msg | Message to be send. |
size | Binary message size. |
int ws_sendframe_bin_bcast | ( | uint16_t | port, |
const char * | msg, | ||
uint64_t | size | ||
) |
Sends a broadcast WebSocket binary frame.
port | Server listen port to broadcast message. |
msg | Message to be send. |
size | Binary message size. |
int ws_sendframe_txt | ( | ws_cli_conn_t * | client, |
const char * | msg | ||
) |
Sends a WebSocket text frame.
client | Target to be send. |
msg | Message to be send, null terminated. |
int ws_sendframe_txt_bcast | ( | uint16_t | port, |
const char * | msg | ||
) |
Sends a broadcast WebSocket text frame.
port | Server listen port to broadcast message. |
msg | Message to be send, null terminated. |
void ws_set_connection_context | ( | ws_cli_conn_t * | cli, |
void * | ptr | ||
) |
Set connection context.
int ws_socket | ( | struct ws_server * | ws_srv | ) |
Main loop for the server.
ws_srv | Web Socket server parameters. |
thread_loop
!= 0, returns 0. Otherwise, never returns.