Ns_Sls} - Socket local storage
TABLE OF CONTENTS
SYNOPSIS
DESCRIPTION
FUNCTIONS
ARGUMENTS
EXAMPLES
SEE ALSO
KEYWORDS
|
The underlying TCP socket used by a NaviServer connection may be kept open and reused by following connections, e.g. during HTTP keep-alive . Using SLS you can save data with the socket, for the lifetime of the TCP connection.
There are two ways to save data: 1) by first allocating a slot you can save a pointer to arbitrary data; 2) using a string key you can save string data, which is also easily available via the Tcl ns_sls API.
The following functions require an active connection -- they should be called from e.g. a registered proc or filter.
Log the number of connections handled by a single TCP socket:
#include "ns.h"
typedef struct Data {
int counter;
} Data;
static Ns_Callback Cleanup;
static Ns_FilterProc Filter;
static Ns_Sls slot;
int
Ns_ModuleInit(CONST char *server, CONST char *module)
{
Ns_SlsAlloc(&slot, Cleanup);
(void) Ns_RegisterFilter(server, "GET", "/*", Filter,
NS_FILTER_PRE_AUTH, NULL);
}
static int
Filter(void *arg, Ns_Conn *conn, int why)
{
Ns_Sock *sock = Ns_ConnSockPtr(conn);
Ns_Sls *slot;
Data *data;
data = Ns_SlsGet(&slot, sock);
if (!data) {
data = ns_malloc(sizeof(Data));
data->counter = 0;
Ns_SlsSet(&slot, sock, data)
}
data->counter++;
return NS_OK;
}
static int
Cleanup(void *arg)
{
Data *data = arg;
Ns_Log(Notice, "Example: sls counter: %d", data->counter);
ns_free(data);
}
|
Ns_Cls, Ns_Tls, ns_sls
Ns_SlsAlloc , Ns_SlsGet , Ns_SlsGetKeyed , Ns_SlsSet , Ns_SlsSetKeyed , data , sls , storage