- separate sleeping functionality to _http_sleep()
[m6w6/ext-http] / php_http_api.h
index 9e5c389da97df25377b60c4849acc2f098ab0ed0..052fb1e1c817a9cb92bd5e99ff1930b9e724ba70 100644 (file)
@@ -156,6 +156,41 @@ PHP_HTTP_API void _http_parse_params_default_callback(void *ht, const char *key,
 PHP_HTTP_API STATUS _http_parse_params_ex(const char *params, int flags, http_parse_params_callback cb, void *cb_arg TSRMLS_DC);
 
 
+#define http_sleep(s) _http_sleep(s)
+static inline void _http_sleep(double s)
+{
+#define HTTP_DIFFSEC (0.001)
+#define HTTP_MLLISEC (1000)
+#define HTTP_MCROSEC (1000 * 1000)
+#define HTTP_NANOSEC (1000 * 1000 * 1000)
+#define HTTP_MSEC(s) (s * HTTP_MLLISEC)
+#define HTTP_USEC(s) (s * HTTP_MCROSEC)
+#define HTTP_NSEC(s) (s * HTTP_NANOSEC)
+
+#if defined(PHP_WIN32)
+       Sleep((DWORD) HTTP_MSEC(s));
+#elif defined(HAVE_USLEEP)
+       usleep(HTTP_USEC(s));
+#elif defined(HAVE_NANOSLEEP)
+       struct timespec req, rem;
+
+       req.tv_sec = (time_t) s;
+       req.tv_nsec = HTTP_NSEC(s) % HTTP_NANOSEC;
+
+       while (nanosleep(&req, &rem) && (errno == EINTR) && (HTTP_NSEC(rem.tv_sec) + rem.tv_nsec) > HTTP_NSEC(HTTP_DIFFSEC))) {
+               req.tv_sec = rem.tv_sec;
+               req.tv_nsec = rem.tv_nsec;
+       }
+#else
+       struct timeval timeout;
+       timeout.tv.sec = (time_t) s;
+       timeout.tv_usec = HTTP_USEC(s) % HTTP_MCROSEC;
+       
+       select(0, NULL, NULL, NULL, &timeout);
+#endif
+}
+
 #define http_locate_body _http_locate_body
 static inline const char *_http_locate_body(const char *message)
 {