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. You must create a listening TCP socket (named sock
). 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:
The proc
is your socket callback function in the following format:
typedef int (Ns_SockProc) (int sock, void *arg, int why);
The sock will be a readable, writable socket. The arg is the ctx you passed to Ns_SockCallback. The why argument is the when you passed to Ns_SockCallback.
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 share a single thread for that purpose.
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);