aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/daemon.c')
-rw-r--r--src/daemon/daemon.c134
1 files changed, 72 insertions, 62 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 26b13d35..385e37c3 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -429,6 +429,7 @@ MHD_select (struct MHD_Daemon *daemon, int may_block)
429 unsigned long long ltimeout; 429 unsigned long long ltimeout;
430 int ds; 430 int ds;
431 time_t now; 431 time_t now;
432 int go_again;
432 433
433 timeout.tv_sec = 0; 434 timeout.tv_sec = 0;
434 timeout.tv_usec = 0; 435 timeout.tv_usec = 0;
@@ -436,78 +437,87 @@ MHD_select (struct MHD_Daemon *daemon, int may_block)
436 abort (); 437 abort ();
437 if (daemon->shutdown == MHD_YES) 438 if (daemon->shutdown == MHD_YES)
438 return MHD_NO; 439 return MHD_NO;
439 FD_ZERO (&rs); 440 go_again = MHD_YES;
440 FD_ZERO (&ws); 441 while (go_again == MHD_YES)
441 FD_ZERO (&es);
442 max = 0;
443
444 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
445 {
446 /* single-threaded, go over everything */
447 if (MHD_NO == MHD_get_fdset (daemon, &rs, &ws, &es, &max))
448 return MHD_NO;
449 }
450 else
451 { 442 {
452 /* accept only, have one thread per connection */ 443 go_again = MHD_NO;
453 max = daemon->socket_fd; 444 FD_ZERO (&rs);
454 if (max == -1) 445 FD_ZERO (&ws);
446 FD_ZERO (&es);
447 max = 0;
448
449 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
450 {
451 /* single-threaded, go over everything */
452 if (MHD_NO == MHD_get_fdset (daemon, &rs, &ws, &es, &max))
453 return MHD_NO;
454 }
455 else
456 {
457 /* accept only, have one thread per connection */
458 max = daemon->socket_fd;
459 if (max == -1)
460 return MHD_NO;
461 FD_SET (max, &rs);
462 }
463 if (may_block == MHD_NO)
464 {
465 timeout.tv_usec = 0;
466 timeout.tv_sec = 0;
467 }
468 else
469 {
470 /* ltimeout is in ms */
471 if (MHD_YES == MHD_get_timeout (daemon, &ltimeout))
472 {
473 timeout.tv_usec = (ltimeout % 1000) * 1000 * 1000;
474 timeout.tv_sec = ltimeout / 1000;
475 may_block = MHD_NO;
476 }
477 }
478 num_ready = SELECT (max + 1,
479 &rs, &ws, &es,
480 may_block == MHD_NO ? &timeout : NULL);
481 if (daemon->shutdown == MHD_YES)
455 return MHD_NO; 482 return MHD_NO;
456 FD_SET (max, &rs); 483 if (num_ready < 0)
457 }
458 if (may_block == MHD_NO)
459 {
460 timeout.tv_usec = 0;
461 timeout.tv_sec = 0;
462 }
463 else
464 {
465 /* ltimeout is in ms */
466 if (MHD_YES == MHD_get_timeout (daemon, &ltimeout))
467 { 484 {
468 timeout.tv_usec = (ltimeout % 1000) * 1000 * 1000; 485 if (errno == EINTR)
469 timeout.tv_sec = ltimeout / 1000; 486 return MHD_YES;
470 may_block = MHD_NO; 487 MHD_DLOG (daemon, "Select failed: %s\n", STRERROR (errno));
488 return MHD_NO;
471 } 489 }
472 } 490 ds = daemon->socket_fd;
473 num_ready = SELECT (max + 1, 491 if (ds == -1)
474 &rs, &ws, &es, may_block == MHD_NO ? &timeout : NULL);
475 if (daemon->shutdown == MHD_YES)
476 return MHD_NO;
477 if (num_ready < 0)
478 {
479 if (errno == EINTR)
480 return MHD_YES; 492 return MHD_YES;
481 MHD_DLOG (daemon, "Select failed: %s\n", STRERROR (errno)); 493 if (FD_ISSET (ds, &rs))
482 return MHD_NO; 494 {
483 } 495 MHD_accept_connection (daemon);
484 ds = daemon->socket_fd; 496 go_again = MHD_YES;
485 if (ds == -1) 497 }
486 return MHD_YES; 498 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
487 if (FD_ISSET (ds, &rs))
488 MHD_accept_connection (daemon);
489 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
490 {
491 /* do not have a thread per connection, process all connections now */
492 now = time (NULL);
493 pos = daemon->connections;
494 while (pos != NULL)
495 { 499 {
496 ds = pos->socket_fd; 500 /* do not have a thread per connection, process all connections now */
497 if (ds != -1) 501 now = time (NULL);
502 pos = daemon->connections;
503 while (pos != NULL)
498 { 504 {
499 if (FD_ISSET (ds, &rs)) 505 ds = pos->socket_fd;
500 { 506 if (ds != -1)
501 pos->last_activity = now;
502 MHD_connection_handle_read (pos);
503 }
504 if (FD_ISSET (ds, &ws))
505 { 507 {
506 pos->last_activity = now; 508 if (FD_ISSET (ds, &rs))
507 MHD_connection_handle_write (pos); 509 {
510 pos->last_activity = now;
511 MHD_connection_handle_read (pos);
512 }
513 if (FD_ISSET (ds, &ws))
514 {
515 pos->last_activity = now;
516 MHD_connection_handle_write (pos);
517 }
508 } 518 }
519 pos = pos->next;
509 } 520 }
510 pos = pos->next;
511 } 521 }
512 } 522 }
513 return MHD_YES; 523 return MHD_YES;