commit c6eae238be6357a7e0b77298a0cdada4c63e80bc
parent aea4dcaefeb0b5baef0b7742356403f31a462e62
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Thu, 28 Apr 2022 16:59:19 +0300
Added new function MHD_get_timeout_i()
This function is useful for direct in in poll() or epoll_wait().
Diffstat:
2 files changed, 107 insertions(+), 7 deletions(-)
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
@@ -96,7 +96,7 @@ extern "C"
* they are parsed as decimal numbers.
* Example: 0x01093001 = 1.9.30-1.
*/
-#define MHD_VERSION 0x00097509
+#define MHD_VERSION 0x00097510
/* If generic headers don't work on your platform, include headers
which define 'va_list', 'size_t', 'ssize_t', 'intptr_t',
@@ -2903,7 +2903,8 @@ MHD_get_fdset2 (struct MHD_Daemon *daemon,
* connections with data pending in network buffers and other problems.
*
* It is important to always use this function (or #MHD_get_timeout64(),
- * #MHD_get_timeout64s() functions) when "external" polling is used.
+ * #MHD_get_timeout64s(), #MHD_get_timeout_i() functions) when "external"
+ * polling is used.
* If this function returns #MHD_YES then #MHD_run() (or #MHD_run_from_select())
* must be called right after return from polling function, regardless of
* the states of MHD FDs.
@@ -2939,7 +2940,8 @@ MHD_get_timeout (struct MHD_Daemon *daemon,
* connections with data pending in network buffers and other problems.
*
* It is important to always use this function (or #MHD_get_timeout(),
- * #MHD_get_timeout64s() functions) when "external" polling is used.
+ * #MHD_get_timeout64s(), #MHD_get_timeout_i() functions) when "external"
+ * polling is used.
* If this function returns #MHD_YES then #MHD_run() (or #MHD_run_from_select())
* must be called right after return from polling function, regardless of
* the states of MHD FDs.
@@ -2978,7 +2980,8 @@ MHD_get_timeout64 (struct MHD_Daemon *daemon,
* network buffers and other problems.
*
* It is important to always use this function (or #MHD_get_timeout(),
- * #MHD_get_timeout64() functions) when "external" polling is used.
+ * #MHD_get_timeout64(), #MHD_get_timeout_i() functions) when "external"
+ * polling is used.
* If this function returns non-negative value then #MHD_run() (or
* #MHD_run_from_select()) must be called right after return from polling
* function, regardless of the states of MHD FDs.
@@ -3002,6 +3005,47 @@ MHD_get_timeout64s (struct MHD_Daemon *daemon);
/**
+ * Obtain timeout value for external polling function for this daemon.
+ *
+ * This function set value to the amount of milliseconds for which polling
+ * function (`select()`, `poll()` or epoll) should at most block, not the
+ * timeout value set for connections.
+ *
+ * Any "external" sockets polling function must be called with the timeout
+ * value provided by this function (if returned value is non-negative).
+ * Smaller timeout values can be used for polling function if it is required
+ * for any reason, but using larger timeout value or no timeout (indefinite
+ * timeout) when this function returns non-negative value will break MHD
+ * processing logic and result in "hung" connections with data pending in
+ * network buffers and other problems.
+ *
+ * It is important to always use this function (or #MHD_get_timeout(),
+ * #MHD_get_timeout64(), #MHD_get_timeout64s() functions) when "external"
+ * polling is used.
+ * If this function returns non-negative value then #MHD_run() (or
+ * #MHD_run_from_select()) must be called right after return from polling
+ * function, regardless of the states of MHD FDs.
+ *
+ * In practice, if zero or positive value is returned then #MHD_run() (or
+ * #MHD_run_from_select()) must be called not later than returned amount of
+ * millisecond even if no activity is detected on sockets by sockets
+ * polling function.
+ *
+ * @param daemon the daemon to query for timeout
+ * @return -1 if connections' timeouts are not set and no data processing
+ * is pending, so external polling function may wait for sockets
+ * activity for indefinite amount of time,
+ * otherwise returned value is the the maximum amount of millisecond
+ * (capped at INT_MAX) that external polling function must wait
+ * for the activity of FDs.
+ * @note Available since #MHD_VERSION 0x00097510
+ * @ingroup event
+ */
+_MHD_EXTERN int
+MHD_get_timeout_i (struct MHD_Daemon *daemon);
+
+
+/**
* Run webserver operations (without blocking unless in client callbacks).
*
* This method should be called by clients in combination with
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
@@ -3896,7 +3896,8 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
* connections with data pending in network buffers and other problems.
*
* It is important to always use this function (or #MHD_get_timeout64(),
- * #MHD_get_timeout64s() functions) when "external" polling is used.
+ * #MHD_get_timeout64s(), #MHD_get_timeout_i() functions) when "external"
+ * polling is used.
* If this function returns #MHD_YES then #MHD_run() (or #MHD_run_from_select())
* must be called right after return from polling function, regardless of
* the states of MHD FDs.
@@ -3947,7 +3948,8 @@ MHD_get_timeout (struct MHD_Daemon *daemon,
* connections with data pending in network buffers and other problems.
*
* It is important to always use this function (or #MHD_get_timeout(),
- * #MHD_get_timeout64s() functions) when "external" polling is used.
+ * #MHD_get_timeout64s(), #MHD_get_timeout_i() functions) when "external"
+ * polling is used.
* If this function returns #MHD_YES then #MHD_run() (or #MHD_run_from_select())
* must be called right after return from polling function, regardless of
* the states of MHD FDs.
@@ -4058,7 +4060,8 @@ MHD_get_timeout64 (struct MHD_Daemon *daemon,
* network buffers and other problems.
*
* It is important to always use this function (or #MHD_get_timeout(),
- * #MHD_get_timeout64() functions) when "external" polling is used.
+ * #MHD_get_timeout64(), #MHD_get_timeout_i() functions) when "external"
+ * polling is used.
* If this function returns non-negative value then #MHD_run() (or
* #MHD_run_from_select()) must be called right after return from polling
* function, regardless of the states of MHD FDs.
@@ -4093,6 +4096,59 @@ MHD_get_timeout64s (struct MHD_Daemon *daemon)
/**
+ * Obtain timeout value for external polling function for this daemon.
+ *
+ * This function set value to the amount of milliseconds for which polling
+ * function (`select()`, `poll()` or epoll) should at most block, not the
+ * timeout value set for connections.
+ *
+ * Any "external" sockets polling function must be called with the timeout
+ * value provided by this function (if returned value is non-negative).
+ * Smaller timeout values can be used for polling function if it is required
+ * for any reason, but using larger timeout value or no timeout (indefinite
+ * timeout) when this function returns non-negative value will break MHD
+ * processing logic and result in "hung" connections with data pending in
+ * network buffers and other problems.
+ *
+ * It is important to always use this function (or #MHD_get_timeout(),
+ * #MHD_get_timeout64(), #MHD_get_timeout64s() functions) when "external"
+ * polling is used.
+ * If this function returns non-negative value then #MHD_run() (or
+ * #MHD_run_from_select()) must be called right after return from polling
+ * function, regardless of the states of MHD FDs.
+ *
+ * In practice, if zero or positive value is returned then #MHD_run() (or
+ * #MHD_run_from_select()) must be called not later than returned amount of
+ * millisecond even if no activity is detected on sockets by sockets
+ * polling function.
+ * @remark To be called only from thread that process
+ * daemon's select()/poll()/etc.
+ *
+ * @param daemon the daemon to query for timeout
+ * @return -1 if connections' timeouts are not set and no data processing
+ * is pending, so external polling function may wait for sockets
+ * activity for indefinite amount of time,
+ * otherwise returned value is the the maximum amount of millisecond
+ * (capped at INT_MAX) that external polling function must wait
+ * for the activity of FDs.
+ * @note Available since #MHD_VERSION 0x00097510
+ * @ingroup event
+ */
+_MHD_EXTERN int
+MHD_get_timeout_i (struct MHD_Daemon *daemon)
+{
+#if SIZEOF_INT >= SIZEOF_INT64_T
+ return MHD_get_timeout64s (daemon);
+#else /* SIZEOF_INT < SIZEOF_INT64_T */
+ const int64_t to64 = MHD_get_timeout64s (daemon);
+ if (INT_MAX >= to64)
+ return to64;
+ return INT_MAX;
+#endif /* SIZEOF_INT < SIZEOF_INT64_T */
+}
+
+
+/**
* Obtain timeout value for polling function for this daemon.
* @remark To be called only from the thread that processes
* daemon's select()/poll()/etc.