Register a socket callback function
int Ns_SockCallback ( SOCKET sock, Ns_SockProc *proc, void *ctx, int when );
Ns_SockCallback registers a user-defined socket callback function and should be called by your module at startup time.
The proc
is your socket callback function. The ctx
argument is your context which will be passed back as the second argument of your callback function.
The when
argument is a bitmask with one or more of the following options specified:
At startup time, AOLserver creates a single socket service thread dedicated to handling socket callbacks. Since several sockets are needed to listen for connection requests, and because connection requests are handled so quickly, all the socket drivers in all the virtual servers share a single thread for that purpose.
The result of this optimization is that AOLserver requires no per-server threads, which can greatly increase the number of servers per process. This is especially beneficial on the SGI platform, where threads use up system process entries.
typedef int (Ns_SockProc) (SOCKET sock, void *context, int why);
the reason the function was called, which is one of the following: NS_SOCK_READ: the socket is readable NS_SOCK_WRITE: the socket is writeable |
int MySock(SOCKET sock, void *context, int why) { if (why == NS_SOCK_READ) { .. handle read .. if (error) { return NS_FALSE; } else { return NS_TRUE; } } else if (why == NS_SOCK_EXIT) { .. free(context) .. return NS_FALSE; } }
Ns_SockCallback(sock, MySock, myCtx, NS_SOCK_READ | NS_SOCK_EXIT);