From 44f694198ba0d2b9b7172468989a83f6e1e51929 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 30 Aug 2007 01:20:29 +0000 Subject: do not accept forever --- src/daemon/daemon.c | 155 +++++++++++++++++++++++++--------------------------- 1 file changed, 73 insertions(+), 82 deletions(-) diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 79963d6b..35eeccce 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -447,7 +447,6 @@ MHD_select (struct MHD_Daemon *daemon, int may_block) unsigned long long ltimeout; int ds; time_t now; - int go_again; timeout.tv_sec = 0; timeout.tv_usec = 0; @@ -455,88 +454,80 @@ MHD_select (struct MHD_Daemon *daemon, int may_block) abort (); if (daemon->shutdown == MHD_YES) return MHD_NO; - go_again = MHD_YES; - while (go_again == MHD_YES) + FD_ZERO (&rs); + FD_ZERO (&ws); + FD_ZERO (&es); + max = 0; + + if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - go_again = MHD_NO; - FD_ZERO (&rs); - FD_ZERO (&ws); - FD_ZERO (&es); - max = 0; - - if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) - { - /* single-threaded, go over everything */ - if (MHD_NO == MHD_get_fdset (daemon, &rs, &ws, &es, &max)) - return MHD_NO; - } - else - { - /* accept only, have one thread per connection */ - max = daemon->socket_fd; - if (max == -1) - return MHD_NO; - FD_SET (max, &rs); - } - if (may_block == MHD_NO) - { - timeout.tv_usec = 0; - timeout.tv_sec = 0; - } - else - { - /* ltimeout is in ms */ - if (MHD_YES == MHD_get_timeout (daemon, <imeout)) - { - timeout.tv_usec = (ltimeout % 1000) * 1000; - timeout.tv_sec = ltimeout / 1000; - may_block = MHD_NO; - } - } - num_ready = SELECT (max + 1, - &rs, &ws, &es, - may_block == MHD_NO ? &timeout : NULL); - if (daemon->shutdown == MHD_YES) - return MHD_NO; - if (num_ready < 0) - { - if (errno == EINTR) - return MHD_YES; - MHD_DLOG (daemon, "Select failed: %s\n", STRERROR (errno)); - return MHD_NO; - } - ds = daemon->socket_fd; - if (ds == -1) - return MHD_YES; - if (FD_ISSET (ds, &rs)) - { - MHD_accept_connection (daemon); - go_again = MHD_YES; - } - if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) - { - /* do not have a thread per connection, process all connections now */ - now = time (NULL); - pos = daemon->connections; - while (pos != NULL) - { - ds = pos->socket_fd; - if (ds != -1) - { - if (FD_ISSET (ds, &rs)) - { - pos->last_activity = now; - MHD_connection_handle_read (pos); - } - if (FD_ISSET (ds, &ws)) - { - pos->last_activity = now; - MHD_connection_handle_write (pos); - } - } - pos = pos->next; - } - } + /* single-threaded, go over everything */ + if (MHD_NO == MHD_get_fdset (daemon, &rs, &ws, &es, &max)) + return MHD_NO; + } + else + { + /* accept only, have one thread per connection */ + max = daemon->socket_fd; + if (max == -1) + return MHD_NO; + FD_SET (max, &rs); + } + if (may_block == MHD_NO) + { + timeout.tv_usec = 0; + timeout.tv_sec = 0; + } + else + { + /* ltimeout is in ms */ + if (MHD_YES == MHD_get_timeout (daemon, <imeout)) + { + timeout.tv_usec = (ltimeout % 1000) * 1000; + timeout.tv_sec = ltimeout / 1000; + may_block = MHD_NO; + } + } + num_ready = SELECT (max + 1, + &rs, &ws, &es, + may_block == MHD_NO ? &timeout : NULL); + if (daemon->shutdown == MHD_YES) + return MHD_NO; + if (num_ready < 0) + { + if (errno == EINTR) + return MHD_YES; + MHD_DLOG (daemon, "Select failed: %s\n", STRERROR (errno)); + return MHD_NO; + } + ds = daemon->socket_fd; + if (ds == -1) + return MHD_YES; + if (FD_ISSET (ds, &rs)) + MHD_accept_connection (daemon); + if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + { + /* do not have a thread per connection, process all connections now */ + now = time (NULL); + pos = daemon->connections; + while (pos != NULL) + { + ds = pos->socket_fd; + if (ds != -1) + { + if (FD_ISSET (ds, &rs)) + { + pos->last_activity = now; + MHD_connection_handle_read (pos); + } + if (FD_ISSET (ds, &ws)) + { + pos->last_activity = now; + MHD_connection_handle_write (pos); + } + } + pos = pos->next; + } } return MHD_YES; } -- cgit v1.2.3