From 55b1cd3d2ce9074b96a62b9ce13d23b578aeb44b Mon Sep 17 00:00:00 2001 From: Dima Korolev Date: Fri, 9 Aug 2024 10:47:08 +0200 Subject: [PATCH 1/2] Made it possible to set client context for the listening server too. --- include/ws.h | 15 +++++++++++++++ src/ws.c | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/ws.h b/include/ws.h index a709170..40bbca4 100644 --- a/include/ws.h +++ b/include/ws.h @@ -227,6 +227,9 @@ extern "C" { /* Opaque client connection type. */ typedef struct ws_connection ws_cli_conn_t; + /* Opaque server instance type. */ + typedef struct ws_server ws_server_t; + /** * @brief Set client context. * Note that the same `ws_cli_conn_t` instance can be reused across connections. @@ -239,6 +242,18 @@ extern "C" { */ void *ws_get_client_context(ws_cli_conn_t *cli); + /** + * @brief Set client context for the server. + * Note that it can outlive a single connection. + */ + void ws_server_set_client_context(ws_server_t *ws_srv, void *ptr); + + /** + * @brief Get client context for the server. + * Note that it can outlive a single connection. + */ + void *ws_server_get_client_context(ws_server_t *ws_srv); + /** * @brief events Web Socket events types. */ diff --git a/src/ws.c b/src/ws.c index 0af77c8..814e736 100644 --- a/src/ws.c +++ b/src/ws.c @@ -102,11 +102,29 @@ void ws_set_client_context(ws_cli_conn_t *cli, void *ptr) * @brief Get client context. * Note that the same `ws_cli_conn_t` instance can be reused across connections. */ -void *ws_get_client_context(struct ws_connection* cli) +void *ws_get_client_context(ws_cli_conn_t *cli) { return cli->ws_srv.client_context; } +/** + * @brief Set client context for the server. + * Note that it can outlive a single connection. + */ +void ws_server_set_client_context(ws_server_t *ws_srv, void *ptr) +{ + ws_srv->client_context = ptr; +} + +/** + * @brief Get client context for the server. + * Note that it can outlive a single connection. + */ +void *ws_server_get_client_context(ws_server_t* ws_srv) +{ + return ws_srv->client_context; +} + /** * @brief Clients list. */ From 075cefebadf4d5bf8724fe373cbf663c6d7f7606 Mon Sep 17 00:00:00 2001 From: Dima Korolev Date: Mon, 12 Aug 2024 00:14:20 +0200 Subject: [PATCH 2/2] Separated the server context from connection context. --- include/ws.h | 28 ++++++++++------------------ src/ws.c | 34 +++++++++++++--------------------- 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/include/ws.h b/include/ws.h index 40bbca4..0b2aaab 100644 --- a/include/ws.h +++ b/include/ws.h @@ -231,28 +231,20 @@ extern "C" { typedef struct ws_server ws_server_t; /** - * @brief Set client context. - * Note that the same `ws_cli_conn_t` instance can be reused across connections. + * @brief Get server context. + * Set when initializing `.context` in `struct ws_server`. */ - void ws_set_client_context(ws_cli_conn_t *cli, void *ptr); + void *ws_get_server_context(ws_cli_conn_t *cli); /** - * @brief Get client context. - * Note that the same `ws_cli_conn_t` instance can be reused across connections. + * @brief Set connection context. */ - void *ws_get_client_context(ws_cli_conn_t *cli); + void ws_set_connection_context(ws_cli_conn_t *cli, void *ptr); /** - * @brief Set client context for the server. - * Note that it can outlive a single connection. + * @brief Get connection context. */ - void ws_server_set_client_context(ws_server_t *ws_srv, void *ptr); - - /** - * @brief Get client context for the server. - * Note that it can outlive a single connection. - */ - void *ws_server_get_client_context(ws_server_t *ws_srv); + void *ws_get_connection_context(ws_cli_conn_t *cli); /** * @brief events Web Socket events types. @@ -302,10 +294,10 @@ extern "C" { */ struct ws_events evs; /** - * @brief Client context. - * Note that the same `ws_cli_conn_t` instance can be reused across connections. + * @brief Server context. + * Provided by the user, can be accessed via `ws_get_server_context` from `onopen`. */ - void* client_context; + void* context; }; /* Forward declarations. */ diff --git a/src/ws.c b/src/ws.c index 814e736..16611aa 100644 --- a/src/ws.c +++ b/src/ws.c @@ -87,42 +87,34 @@ struct ws_connection int32_t last_pong_id; int32_t current_ping_id; pthread_mutex_t mtx_ping; -}; -/** - * @brief Set client context. - * Note that the same `ws_cli_conn_t` instance can be reused across connections. - */ -void ws_set_client_context(ws_cli_conn_t *cli, void *ptr) -{ - cli->ws_srv.client_context = ptr; -} + /* Connection context */ + void *connection_context; +}; /** - * @brief Get client context. - * Note that the same `ws_cli_conn_t` instance can be reused across connections. + * @brief Get server context. + * Assumed to be set once, when initializing `.context` in `struct ws_server`. */ -void *ws_get_client_context(ws_cli_conn_t *cli) +void *ws_get_server_context(ws_cli_conn_t *cli) { - return cli->ws_srv.client_context; + return cli->ws_srv.context; } /** - * @brief Set client context for the server. - * Note that it can outlive a single connection. + * @brief Set connection context. */ -void ws_server_set_client_context(ws_server_t *ws_srv, void *ptr) +void ws_set_connection_context(ws_cli_conn_t *cli, void *ptr) { - ws_srv->client_context = ptr; + cli->connection_context = ptr; } /** - * @brief Get client context for the server. - * Note that it can outlive a single connection. + * @brief Get connection context. */ -void *ws_server_get_client_context(ws_server_t* ws_srv) +void *ws_get_connection_context(ws_cli_conn_t *cli) { - return ws_srv->client_context; + return cli->connection_context; } /**