Skip to content
Merged
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
25 changes: 16 additions & 9 deletions include/ws.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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. */
Expand Down
26 changes: 18 additions & 8 deletions src/ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down