From 3bbaaa10be75ae1568c31040fa87cb4f8fe7a493 Mon Sep 17 00:00:00 2001 From: ztao Date: Thu, 19 May 2022 21:47:29 +0800 Subject: [PATCH 1/4] fix: select error when sockfd above FD_SETSIZE --- .../http/client/curl/http_operation_curl.h | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) 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; } From 4ca8633df0b9b6bda300a4b92624ff6fc0d71a88 Mon Sep 17 00:00:00 2001 From: ztao Date: Fri, 20 May 2022 13:13:24 +0800 Subject: [PATCH 2/4] fix implicit convert to bool compile warning --- .../ext/http/client/curl/http_operation_curl.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 e757bb7bbd..88f9e2a489 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 @@ -481,11 +481,11 @@ class HttpOperation { if (for_recv) { - res = FD_ISSET(sockfd, &infd); + res = (0 != FD_ISSET(sockfd, &infd)); } else { - res = FD_ISSET(sockfd, &outfd); + res = (0 != FD_ISSET(sockfd, &outfd)); } } @@ -508,11 +508,11 @@ class HttpOperation { if (for_recv) { - res = fds[0].revents & POLLIN; + res = (0 != (fds[0].revents & POLLIN)); } else { - res = fds[0].revents & POLLOUT; + res = (0 != (fds[0].revents & POLLOUT)); } } From fa8bdd732eb1a82dda06e03576032efb607a70d7 Mon Sep 17 00:00:00 2001 From: ztao Date: Fri, 20 May 2022 16:51:51 +0800 Subject: [PATCH 3/4] fix clang-format --- .../ext/http/client/curl/http_operation_curl.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 88f9e2a489..369b8d8029 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 @@ -17,8 +17,8 @@ # include # include #else -# include # include +# include #endif #include @@ -492,7 +492,7 @@ class HttpOperation #else struct pollfd fds[1]; - ::memset(fds, 0 , sizeof(fds)); + ::memset(fds, 0, sizeof(fds)); fds[0].fd = sockfd; if (for_recv) @@ -506,17 +506,17 @@ class HttpOperation if (poll(fds, 1, timeout_ms) > 0) { - if (for_recv) + if (for_recv) { res = (0 != (fds[0].revents & POLLIN)); - } + } else { res = (0 != (fds[0].revents & POLLOUT)); } } -#endif +#endif return res; } From e02c2a0939aad4f818fb62f35bdbbad589f0addb Mon Sep 17 00:00:00 2001 From: ztao Date: Sat, 21 May 2022 07:37:34 +0800 Subject: [PATCH 4/4] Update http_operation_curl.h --- .../opentelemetry/ext/http/client/curl/http_operation_curl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 369b8d8029..679251cfeb 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 @@ -489,7 +489,7 @@ class HttpOperation } } -#else +#else struct pollfd fds[1]; ::memset(fds, 0, sizeof(fds));