GNU libmicrohttpd  0.9.72
daemon_get_timeout.c
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
25 #include "internal.h"
26 
27 
47 enum MHD_StatusCode
48 MHD_daemon_get_timeout (struct MHD_Daemon *daemon,
49  MHD_UNSIGNED_LONG_LONG *timeout)
50 {
51  time_t earliest_deadline;
52  time_t now;
53  struct MHD_Connection *pos;
54  bool have_timeout;
55 
56  if (MHD_TM_EXTERNAL_EVENT_LOOP != daemon->threading_mode)
57  {
58 #ifdef HAVE_MESSAGES
59  MHD_DLOG (daemon,
60  MHD_SC_CONFIGURATION_MISMATCH_FOR_GET_TIMEOUT,
61  _ ("Illegal call to MHD_get_timeout.\n"));
62 #endif
63  return MHD_SC_CONFIGURATION_MISSMATCH_FOR_GET_TIMEOUT;
64  }
65 
67  {
68  /* Some data already waiting to be processed. */
69  *timeout = 0;
70  return MHD_SC_OK;
71  }
72 
73 #ifdef EPOLL_SUPPORT
74  if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) &&
75  ((NULL != daemon->eready_head)
76 #if defined(UPGRADE_SUPPORT) && defined(HTTPS_SUPPORT)
77  || (NULL != daemon->eready_urh_head)
78 #endif /* UPGRADE_SUPPORT && HTTPS_SUPPORT */
79  ) )
80  {
81  /* Some connection(s) already have some data pending. */
82  *timeout = 0;
83  return MHD_SC_OK;
84  }
85 #endif /* EPOLL_SUPPORT */
86 
87  have_timeout = false;
88  earliest_deadline = 0; /* avoid compiler warnings */
89  for (pos = daemon->manual_timeout_tail; NULL != pos; pos = pos->prevX)
90  {
91  if (0 != pos->connection_timeout)
92  {
93  if ( (! have_timeout) ||
94  (earliest_deadline - pos->last_activity > pos->connection_timeout) )
95  earliest_deadline = pos->last_activity + pos->connection_timeout;
96  have_timeout = true;
97  }
98  }
99  /* normal timeouts are sorted, so we only need to look at the 'tail' (oldest) */
101  if ( (NULL != pos) &&
102  (0 != pos->connection_timeout) )
103  {
104  if ( (! have_timeout) ||
105  (earliest_deadline - pos->connection_timeout > pos->last_activity) )
106  earliest_deadline = pos->last_activity + pos->connection_timeout;
107  have_timeout = true;
108  }
109 
110  if (! have_timeout)
111  return MHD_SC_NO_TIMEOUT;
112  now = MHD_monotonic_sec_counter ();
113  if (earliest_deadline < now)
114  *timeout = 0;
115  else
116  {
117  const time_t second_left = earliest_deadline - now;
118  if (second_left > ULLONG_MAX / 1000) /* Ignore compiler warning: 'second_left' is always positive. */
119  *timeout = ULLONG_MAX;
120  else
121  *timeout = 1000LL * second_left;
122  }
123  return MHD_SC_OK;
124 }
125 
126 
127 /* end of daemon_get_timeout.c */
enum MHD_StatusCode MHD_daemon_get_timeout(struct MHD_Daemon *daemon, MHD_UNSIGNED_LONG_LONG *timeout)
#define ULLONG_MAX
Definition: mhd_limits.h:58
time_t MHD_monotonic_sec_counter(void)
#define NULL
Definition: reason_phrase.c:30
#define _(String)
Definition: mhd_options.h:42
internal shared structures
#define MHD_UNSIGNED_LONG_LONG
Definition: microhttpd.h:299
struct MHD_Connection * prevX
Definition: internal.h:670
time_t connection_timeout
Definition: internal.h:745
time_t last_activity
Definition: internal.h:739
struct MHD_Daemon * daemon
Definition: internal.h:675
bool data_already_pending
Definition: internal.h:1500
enum MHD_EventLoopSyscall event_loop_syscall
Definition: internal.h:1436
struct MHD_Connection * manual_timeout_tail
Definition: internal.h:1150
struct MHD_Connection * normal_timeout_tail
Definition: internal.h:1135
enum MHD_ThreadingMode threading_mode
Definition: internal.h:1417