X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_api.h;h=052fb1e1c817a9cb92bd5e99ff1930b9e724ba70;hp=9e5c389da97df25377b60c4849acc2f098ab0ed0;hb=5b440d6af3dd3052dde7b137f975692f0aa84603;hpb=6aca56d611a22eb559098f3c02c31634a6f9ff9f diff --git a/php_http_api.h b/php_http_api.h index 9e5c389..052fb1e 100644 --- a/php_http_api.h +++ b/php_http_api.h @@ -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) {