diff --git a/include/ws.h b/include/ws.h index a709170..0b2aaab 100644 --- a/include/ws.h +++ b/include/ws.h @@ -227,17 +227,24 @@ 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 Get server context. + * Set when initializing `.context` in `struct ws_server`. + */ + void *ws_get_server_context(ws_cli_conn_t *cli); + /** - * @brief Set client context. - * Note that the same `ws_cli_conn_t` instance can be reused across connections. + * @brief Set connection context. */ - void ws_set_client_context(ws_cli_conn_t *cli, void *ptr); + void ws_set_connection_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. + * @brief Get connection context. */ - void *ws_get_client_context(ws_cli_conn_t *cli); + void *ws_get_connection_context(ws_cli_conn_t *cli); /** * @brief events Web Socket events types. @@ -287,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 0af77c8..16611aa 100644 --- a/src/ws.c +++ b/src/ws.c @@ -87,24 +87,34 @@ struct ws_connection int32_t last_pong_id; int32_t current_ping_id; pthread_mutex_t mtx_ping; + + /* Connection context */ + void *connection_context; }; /** - * @brief Set 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_server_context(ws_cli_conn_t *cli) +{ + return cli->ws_srv.context; +} + +/** + * @brief Set connection context. */ -void ws_set_client_context(ws_cli_conn_t *cli, void *ptr) +void ws_set_connection_context(ws_cli_conn_t *cli, void *ptr) { - cli->ws_srv.client_context = ptr; + cli->connection_context = ptr; } /** - * @brief Get client context. - * Note that the same `ws_cli_conn_t` instance can be reused across connections. + * @brief Get connection context. */ -void *ws_get_client_context(struct ws_connection* cli) +void *ws_get_connection_context(ws_cli_conn_t *cli) { - return cli->ws_srv.client_context; + return cli->connection_context; } /**