Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions examples/echo/echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
* in order to send messages and retrieve informations about the
* client.
*/
void onopen(ws_cli_conn_t *client)
void onopen(ws_cli_conn_t *client, void **user_object_ptr)
{
((void)user_object_ptr);
char *cli;
cli = ws_getaddress(client);
#ifndef DISABLE_VERBOSE
Expand All @@ -56,8 +57,9 @@ void onopen(ws_cli_conn_t *client)
* in order to send messages and retrieve informations about the
* client.
*/
void onclose(ws_cli_conn_t *client)
void onclose(ws_cli_conn_t *client, void **user_object_ptr)
{
((void)user_object_ptr);
char *cli;
cli = ws_getaddress(client);
#ifndef DISABLE_VERBOSE
Expand All @@ -80,8 +82,9 @@ void onclose(ws_cli_conn_t *client)
* @param type Message type.
*/
void onmessage(ws_cli_conn_t *client,
const unsigned char *msg, uint64_t size, int type)
const unsigned char *msg, uint64_t size, int type, void **user_object_ptr)
{
((void)user_object_ptr);
char *cli;
cli = ws_getaddress(client);
#ifndef DISABLE_VERBOSE
Expand Down
9 changes: 6 additions & 3 deletions examples/ping/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
*
* @param client Client connection.
*/
void onopen(ws_cli_conn_t *client)
void onopen(ws_cli_conn_t *client, void **user_object_ptr)
{
((void)client);
((void)user_object_ptr);
printf("Connected!\n");
}

Expand All @@ -45,9 +46,10 @@ void onopen(ws_cli_conn_t *client)
*
* @param client Client connection.
*/
void onclose(ws_cli_conn_t *client)
void onclose(ws_cli_conn_t *client, void **user_object_ptr)
{
((void)client);
((void)user_object_ptr);
printf("Disconnected!\n");
}

Expand All @@ -61,12 +63,13 @@ void onclose(ws_cli_conn_t *client)
* @param type Message type.
*/
void onmessage(ws_cli_conn_t *client,
const unsigned char *msg, uint64_t size, int type)
const unsigned char *msg, uint64_t size, int type, void **user_object_ptr)
{
((void)client);
((void)msg);
((void)size);
((void)type);
((void)user_object_ptr);
}

/**
Expand Down
25 changes: 11 additions & 14 deletions examples/vtouchpad/vtouchpad.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
* @brief Main file.
*/

/* Mouse global structure. */
mouse_t *mouse;

/**
* @brief Given a string @p ev containing a single event,
* parses it as expected.
Expand Down Expand Up @@ -120,8 +117,10 @@ struct mouse_event parse_event(const char *ev, int *error)
*
* @param client Client connection.
*/
void onopen(ws_cli_conn_t *client) {
(void)client;
void onopen(ws_cli_conn_t *client, void **user_object_ptr) {
((void)client);
mouse_t * client_mouse = mouse_new();
*user_object_ptr = client_mouse;
printf("Connected!\n");
}

Expand All @@ -131,8 +130,10 @@ void onopen(ws_cli_conn_t *client) {
*
* @param client Client connection.
*/
void onclose(ws_cli_conn_t *client) {
(void)client;
void onclose(ws_cli_conn_t *client, void **user_object_ptr) {
((void)client);
mouse_t * client_mouse = (mouse_t *) *user_object_ptr;
mouse_free(client_mouse);
printf("Disconnected!\n");
}

Expand All @@ -149,14 +150,15 @@ void onclose(ws_cli_conn_t *client) {
* @param type Message type. (ignored)
*/
void onmessage(ws_cli_conn_t *client,
const unsigned char *msg, uint64_t size, int type)
const unsigned char *msg, uint64_t size, int type, void **user_object_ptr)
{
((void)client);
((void)size);
((void)type);

((void)user_object_ptr);
struct mouse_event mev;
int err;
mouse_t * mouse = (mouse_t *) *user_object_ptr;

/* Parse the event. */
mev = parse_event((const char *)msg, &err);
Expand Down Expand Up @@ -199,12 +201,7 @@ int main(void)
evs.onclose = &onclose;
evs.onmessage = &onmessage;

/* Mouse. */
mouse = mouse_new();

ws_socket(&evs, 8080, 0, 1000);

mouse_free(mouse);

return (0);
}
6 changes: 3 additions & 3 deletions include/ws.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,19 @@ extern "C" {
/**
* @brief On open event, called when a new client connects.
*/
void (*onopen)(ws_cli_conn_t *client);
void (*onopen)(ws_cli_conn_t *client, void * * user_object_ptr);

/**
* @brief On close event, called when a client disconnects.
*/
void (*onclose)(ws_cli_conn_t *client);
void (*onclose)(ws_cli_conn_t *client, void * * user_object_ptr);

/**
* @brief On message event, called when a client sends a text
* or binary message.
*/
void (*onmessage)(ws_cli_conn_t *client,
const unsigned char *msg, uint64_t msg_size, int type);
const unsigned char *msg, uint64_t msg_size, int type, void * * user_object_ptr);
};

/* Forward declarations. */
Expand Down
11 changes: 6 additions & 5 deletions src/ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ static inline int is_control_frame(int frame)
* @attention This is part of the internal API and is documented just
* for completeness.
*/
static int do_handshake(struct ws_frame_data *wfd)
static int do_handshake(struct ws_frame_data *wfd, void **user_object_ptr)
{
char *response; /* Handshake response message. */
char *p; /* Last request line pointer. */
Expand Down Expand Up @@ -834,7 +834,7 @@ static int do_handshake(struct ws_frame_data *wfd)
}

/* Trigger events and clean up buffers. */
cli_events.onopen(wfd->client);
cli_events.onopen(wfd->client, user_object_ptr);
free(response);
return (0);
}
Expand Down Expand Up @@ -1442,6 +1442,7 @@ static void *ws_establishconnection(void *vclient)
{
struct ws_frame_data wfd; /* WebSocket frame data. */
ws_cli_conn_t *client; /* Client structure. */
void *user_object = NULL; /* User defined object */
int clse_thrd; /* Time-out close thread. */

client = vclient;
Expand All @@ -1451,7 +1452,7 @@ static void *ws_establishconnection(void *vclient)
wfd.client = client;

/* Do handshake. */
if (do_handshake(&wfd) < 0)
if (do_handshake(&wfd, &user_object) < 0)
goto closed;

/* Change state. */
Expand All @@ -1464,7 +1465,7 @@ static void *ws_establishconnection(void *vclient)
if ((wfd.frame_type == WS_FR_OP_TXT || wfd.frame_type == WS_FR_OP_BIN) &&
!wfd.error)
{
cli_events.onmessage(client, wfd.msg, wfd.frame_size, wfd.frame_type);
cli_events.onmessage(client, wfd.msg, wfd.frame_size, wfd.frame_type, &user_object);
}

/* Close event. */
Expand Down Expand Up @@ -1494,7 +1495,7 @@ static void *ws_establishconnection(void *vclient)
* or server closure, as the server is expected to
* always know when the client disconnects.
*/
cli_events.onclose(client);
cli_events.onclose(client, &user_object);

closed:
clse_thrd = client->close_thrd;
Expand Down