diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h index 09cf8c7c2a..e757bb7bbd 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h @@ -18,6 +18,7 @@ # include #else # include +# include #endif #include @@ -443,13 +444,19 @@ class HttpOperation * @param sockfd * @param for_recv * @param timeout_ms - * @return + * @return true if expected events occur, false if timeout or error happen */ - static int WaitOnSocket(curl_socket_t sockfd, int for_recv, long timeout_ms) + static bool WaitOnSocket(curl_socket_t sockfd, int for_recv, long timeout_ms) { + bool res = false; + +#if defined(_WIN32) + + if (sockfd > FD_SETSIZE) + return false; + struct timeval tv; fd_set infd, outfd, errfd; - int res; tv.tv_sec = timeout_ms / 1000; tv.tv_usec = (timeout_ms % 1000) * 1000; @@ -470,7 +477,47 @@ class HttpOperation } /* select() returns the number of signalled sockets or -1 */ - res = select((int)sockfd + 1, &infd, &outfd, &errfd, &tv); + if (select((int)sockfd + 1, &infd, &outfd, &errfd, &tv) > 0) + { + if (for_recv) + { + res = FD_ISSET(sockfd, &infd); + } + else + { + res = FD_ISSET(sockfd, &outfd); + } + } + +#else + + struct pollfd fds[1]; + ::memset(fds, 0 , sizeof(fds)); + + fds[0].fd = sockfd; + if (for_recv) + { + fds[0].events = POLLIN; + } + else + { + fds[0].events = POLLOUT; + } + + if (poll(fds, 1, timeout_ms) > 0) + { + if (for_recv) + { + res = fds[0].revents & POLLIN; + } + else + { + res = fds[0].revents & POLLOUT; + } + } + +#endif + return res; }