WSLAY_EVENT_CONTEXT_SERVER_INIT(3)
π WSLAY_EVENT_CONTEXT_SERVER_INIT π
okay so bestie, this is a page from the wslay manual, section 3 β and it's literally the whole origin story of your event-based API context. grab a snack, we're going in π«Ά
β¨ NAME β¨
wslay_event_context_server_init β Initialize an event-based API context π
π SYNOPSIS π
#include <wslay/wslay.h>
int wslay_event_context_server_init(wslay_event_context_ptr *ctx, const
struct wslay_event_callbacks *callbacks, void *user_data)
int wslay_event_context_client_init(wslay_event_context_ptr *ctx, const
struct wslay_event_callbacks *callbacks, void *user_data)
void wslay_event_context_free(wslay_event_context_ptr ctx)
πΈ DESCRIPTION πΈ
So wslay_event_context_server_init() initializes an event-based API context for WebSocket server use. And wslay_event_context_client_init() initializes an event-based API context for WebSocket client use. If they come back successful (iconic behavior, we love to see it), then ctx will point to a structure which holds any necessary resources needed to process WebSocket protocol transfers. no cap β¨
callbacks is a pointer to wslay_event_callbacks, which is defined as follows:
struct wslay_event_callbacks {
wslay_event_recv_callback recv_callback;
wslay_event_send_callback send_callback;
wslay_event_genmask_callback genmask_callback;
wslay_event_on_frame_recv_start_callback on_frame_recv_start_callback;
wslay_event_on_frame_recv_chunk_callback on_frame_recv_chunk_callback;
wslay_event_on_frame_recv_end_callback on_frame_recv_end_callback;
wslay_event_on_msg_recv_callback on_msg_recv_callback;
};
kay now let's meet the whole friend group one by one π¦
typedef ssize_t (*wslay_event_recv_callback)(wslay_event_context_ptr
ctx, uint8_t *buf, size_t len, int flags, void *user_data)
recv_callbackis invoked bywslay_event_recv()when it wants to receive more data from peer. The implementation of this callback function must read data at mostlenbytes from peer and store them inbufand return the number of bytes read.flagsis always0in this version. π- If there is an error, return
-1and set error codeWSLAY_ERR_CALLBACK_FAILUREusingwslay_event_set_error(). Wslay event-based API on the whole assumes non-blocking I/O. If the cause of error isEAGAINorEWOULDBLOCK, setWSLAY_ERR_WOULDBLOCKinstead. This is important because it tellswslay_event_recv()to stop receiving further data and return. like, genuinely important, do not skip this one π
typedef ssize_t (*wslay_event_send_callback)(wslay_event_context_ptr
ctx, const uint8_t *data, size_t len, int flags, void *user_data)
send_callbackis invoked bywslay_event_send()when it wants to send more data to peer. The implementation of this callback function must send data at mostlenbytes to peer and return the number of bytes sent.flagsis the bitwise OR of zero or more of the following flag:WSLAY_MSG_MOREβ There is more data to send
It provides some hints to tune performance and behaviour. cute little hints, love that for us π
If there is an error, return -1 and set error code WSLAY_ERR_CALLBACK_FAILURE using wslay_event_set_error(). Wslay event-based API on the whole assumes non-blocking I/O. If the cause of error is EAGAIN or EWOULDBLOCK, set WSLAY_ERR_WOULDBLOCK instead. This is important because it tells wslay_event_send() to stop sending data and return. π«‘
typedef int (*wslay_event_genmask_callback)(wslay_event_context_ptr ctx,
uint8_t *buf, size_t len, void *user_data)
genmask_callbackis invoked bywslay_event_send()when it wants new mask key. As described inRFC6455, only the traffic from WebSocket client is masked, so this callback function is only needed if an event-based API is initialized for WebSocket client use. The implementation of this callback function must fill exactlylenbytes of data inbufand return0on success. If there is an error, return-1and set error codeWSLAY_ERR_CALLBACK_FAILUREusingwslay_event_set_error().
typedef void
(*wslay_event_on_frame_recv_start_callback)(wslay_event_context_ptr ctx,
const struct wslay_event_on_frame_recv_start_arg *arg, void *user_data)
on_frame_recv_start_callbackis invoked bywslay_event_recv()when a new frame starts to be received. This callback function is only invoked once for each frame.wslay_event_on_frame_recv_start_argis defined as follows:
struct wslay_event_on_frame_recv_start_arg {
uint8_t fin;
uint8_t rsv;
uint8_t opcode;
uint64_t payload_length;
};
fin, rsv and opcode is fin bit and reserved bits and opcode of a frame. payload_length is a payload length of a frame. π
typedef void
(*wslay_event_on_frame_recv_chunk_callback)(wslay_event_context_ptr ctx,
const struct wslay_event_on_frame_recv_chunk_arg *arg, void *user_data)
on_frame_recv_chunk_callbackis invoked bywslay_event_recv()when a chunk of frame payload is received.wslay_event_on_frame_recv_chunk_argis defined as follows:
struct wslay_event_on_frame_recv_chunk_arg {
const uint8_t *data;
size_t data_length;
};
data points to a chunk of payload data. data_length is the length of a chunk. π
typedef void
(*wslay_event_on_frame_recv_end_callback)(wslay_event_context_ptr ctx,
void *user_data)
on_frame_recv_end_callbackis invoked bywslay_event_recv()when a frame is completely received. that's the lil end-of-frame bow π
typedef void (*wslay_event_on_msg_recv_callback)(wslay_event_context_ptr
ctx, const struct wslay_event_on_msg_recv_arg *arg, void *user_data)
on_msg_recv_callbackis invoked bywslay_event_recv()when a message is completely received.wslay_event_on_msg_recv_argis defined as follows:
struct wslay_event_on_msg_recv_arg {
uint8_t rsv;
uint8_t opcode;
const uint8_t *msg;
size_t msg_length;
uint16_t status_code;
};
The rsv member and the opcode member are reserved bits and opcode of received message respectively. The rsv member is constructed as follows:
rsv = (RSV1 << 2) | (RSV2 << 1) | RSV3
The msg member points to the message of the received message. The msg_length member is the length of message. If a message is close control frame, in other words, opcode == WSLAY_CONNECTION_CLOSE, status_code is set to the status code in the close control frame. If no status code is included in the close control frame, status_code set to 0. π
user_data is an arbitrary pointer, which is directly passed to each callback functions as user_data argument. it's your lil passport, it goes everywhere you go π
When initialized event-based API context ctx is no longer used, use wslay_event_context_free() to free any resources allocated for ctx. please do this, bestie, don't just ghost your memory π
β RETURN VALUE β
wslay_event_context_server_init() and wslay_event_context_client_init() returns 0 if it succeeds, or one of the following negative error codes:
WSLAY_ERR_NOMEMβ Out of memory. π
π SEE ALSO π
wslay_event_send(), wslay_event_recv(), wslay_event_set_error()
π§ AUTHOR π§
Tatsuhiro Tsujikawa β the main character, fr fr β¨
π COPYRIGHT π
2024, 2015, Tatsuhiro Tsujikawa
and that's the whole page, bestie π₯Ή this one shipped as version @PACKAGE_VERSION@, dated Apr 22, 202 β go forth and slay your event-based API context π
πΈπ¦
π¦ where this page comes from
- ubuntu noble: libwslay-doc 1.1.1-4build1 Β· MIT
- ubuntu questing: libwslay-doc 1.1.1-4build1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms