From 6de65ba8178a0abead311539ea558c927da3e35c Mon Sep 17 00:00:00 2001 From: Dima Korolev Date: Thu, 8 Aug 2024 23:37:16 +0200 Subject: [PATCH] Added client context, with a disclaimer that it can be re-used. --- include/ws.h | 17 +++++++++++++++++ src/ws.c | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/include/ws.h b/include/ws.h index 2173ad9..a709170 100644 --- a/include/ws.h +++ b/include/ws.h @@ -227,6 +227,18 @@ extern "C" { /* Opaque client connection type. */ typedef struct ws_connection ws_cli_conn_t; + /** + * @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); + + /** + * @brief Get client context. + * Note that the same `ws_cli_conn_t` instance can be reused across connections. + */ + void *ws_get_client_context(ws_cli_conn_t *cli); + /** * @brief events Web Socket events types. */ @@ -274,6 +286,11 @@ extern "C" { * @brief Server events. */ struct ws_events evs; + /** + * @brief Client context. + * Note that the same `ws_cli_conn_t` instance can be reused across connections. + */ + void* client_context; }; /* Forward declarations. */ diff --git a/src/ws.c b/src/ws.c index d474f91..0af77c8 100644 --- a/src/ws.c +++ b/src/ws.c @@ -89,6 +89,24 @@ struct ws_connection 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; +} + +/** + * @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) +{ + return cli->ws_srv.client_context; +} + /** * @brief Clients list. */