Hi to all! Worker threads in scalable.c use a timeout in shttpd_poll to be notified when a new client socket is accepted. I cannot have threads polling every 100ms for new sockets, it delays and adds necessary syscalls.
A solution is to add a pipe between the main listener thread and the
worker, and send the sock in that pipe. For that I need a custom socket
accept callback in shttpd_poll. The patch in attachment is just that, a
custom accept callback. Existing code isn't broken, since there are no
changes to the current API, only a new function is added. I use it to
read the socked from the pipe and add it to the context, but you can use
it for anything.
I added:
typedef int (*shttpd_accept_callback_t)(struct shttpd_ctx *,
int, int);
int shttpd_add_listener(struct shttpd_ctx *ctx, int sock, int
is_ssl,
shttpd_accept_callback_t accept_callback);
It gets called in shttpd_poll:
/* Check for incoming connections on listener sockets */
LL_FOREACH(&ctx->listeners, lp) {
l = LL_ENTRY(lp, struct listener, link);
...
if (l->accept_callback != NULL) {
l->accept_callback(ctx, l->sock, l->is_ssl);
continue;
}
The scalable.c would look like this:
int shttpd_pipe_dispatch(int pipe, int sock)
{
return send(pipe, (char*)&sock, sizeof(sock), 0);
}
int shttpd_pipe_accept_callback(struct shttpd_ctx *ctx, int
sock, int is_ssl)
{
int clnt_sock;
if (recv(sock, (char*)&clnt_sock, sizeof(clnt_sock), 0) == -1)
return -1;
shttpd_add_socket(ctx, clnt_sock, is_ssl);
return 0;
}
struct thread
...
int pipe[2];
spawn_new_thread(void)
...
pipe(thread->pipe);
static DWORD WINAPI thread_function(void *param)
...
shttpd_pipe_listen(thread->ctx, thread->pipe, 0,
shttpd_pipe_accept_callback);
int main(int argc, char *argv[])
...
-- shttpd_add_socket(thread->ctx, sock);
++ shttpd_pipe_dispatch(thread->pipe[1], sock);
Patch is in attachment. Feedback is appreciated. Feel free to add it to
shttpd's svn code :)
Thanks in advanced,
Rui
PS: I know that in windows you cannot select on named pipes, but you can
implement a unix pipe using threads and sockets (we use it internally at
my company).
shttpd-1.39-ACCEPT_CALLBACK.patch
Description: shttpd-1.39-ACCEPT_CALLBACK.patch
------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________ shttpd-general mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/shttpd-general
