From 5431548ce560c04c20f62cd90af09df5ed18fecf Mon Sep 17 00:00:00 2001 From: terziev-viktor Date: Mon, 2 May 2022 12:44:28 +0300 Subject: [PATCH 1/2] Fixed the ws_getaddress calls in the example code --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b9ac075..a1cda5c 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ folder, ;-). void onopen(ws_cli_conn_t *client) { char *cli; - cli = ws_getaddress(fd); + cli = ws_getaddress(client); printf("Connection opened, addr: %s\n", cli); } @@ -109,7 +109,7 @@ void onopen(ws_cli_conn_t *client) void onclose(ws_cli_conn_t *client) { char *cli; - cli = ws_getaddress(fd); + cli = ws_getaddress(client); printf("Connection closed, addr: %s\n", cli); } @@ -124,7 +124,7 @@ void onmessage(ws_cli_conn_t *client, const unsigned char *msg, uint64_t size, int type) { char *cli; - cli = ws_getaddress(fd); + cli = ws_getaddress(client); printf("I receive a message: %s (%zu), from: %s\n", msg, size, cli); From 4915cd32c53af4502878cc8bc648fd883f93295d Mon Sep 17 00:00:00 2001 From: "viktor.terziev" Date: Thu, 12 May 2022 15:22:30 +0300 Subject: [PATCH 2/2] Added user defind object to onopen, onclose and onmessage --- examples/echo/echo.c | 9 ++++++--- examples/ping/ping.c | 9 ++++++--- examples/vtouchpad/vtouchpad.c | 25 +++++++++++-------------- include/ws.h | 6 +++--- src/ws.c | 11 ++++++----- 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/examples/echo/echo.c b/examples/echo/echo.c index 5cd768e..9bf42d8 100644 --- a/examples/echo/echo.c +++ b/examples/echo/echo.c @@ -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 @@ -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 @@ -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 diff --git a/examples/ping/ping.c b/examples/ping/ping.c index 69ed654..1dba9f8 100644 --- a/examples/ping/ping.c +++ b/examples/ping/ping.c @@ -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"); } @@ -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"); } @@ -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); } /** diff --git a/examples/vtouchpad/vtouchpad.c b/examples/vtouchpad/vtouchpad.c index 074b0af..13f4ea8 100644 --- a/examples/vtouchpad/vtouchpad.c +++ b/examples/vtouchpad/vtouchpad.c @@ -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. @@ -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"); } @@ -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"); } @@ -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); @@ -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); } diff --git a/include/ws.h b/include/ws.h index c6cccf6..d6e7753 100644 --- a/include/ws.h +++ b/include/ws.h @@ -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. */ diff --git a/src/ws.c b/src/ws.c index 8e976d4..0fa02c8 100644 --- a/src/ws.c +++ b/src/ws.c @@ -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. */ @@ -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); } @@ -1441,6 +1441,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; @@ -1450,7 +1451,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. */ @@ -1463,7 +1464,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. */ @@ -1493,7 +1494,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;