--- parser3/src/main/pa_socks.C 2026/04/25 13:38:46 1.35 +++ parser3/src/main/pa_socks.C 2026/10/29 19:51:53 1.41 @@ -5,14 +5,14 @@ Authors: Konstantin Morshnev , Alexandr Petrosian */ -volatile const char * IDENT_PA_SOCKS_C="$Id: pa_socks.C,v 1.35 2026/04/25 13:38:46 moko Exp $"; +volatile const char * IDENT_PA_SOCKS_C="$Id: pa_socks.C,v 1.41 2026/10/29 19:51:53 moko Exp $"; #include "pa_config_includes.h" +#include "pa_socks.h" #ifdef _MSC_VER #include "pa_exception.h" -#include "pa_socks.h" #include "pa_string.h" #include @@ -63,8 +63,39 @@ int pa_socks_errno() { return WSAGetLastError(); } +bool pa_socks_is_reset(int no) { + return no==WSAECONNRESET || no==WSAECONNABORTED; +} + +bool pa_socks_is_interrupted(int no) { + return no==WSAEINTR; +} + +// there are no signals, calls are not restarted for the time left +typedef int sock_time; + +static sock_time sock_now() { + return 0; +} + +static bool sock_ready_within_remaining_timeout(SOCKET, bool, sock_time) { + return false; +} + +int pa_sock_ready(SOCKET sock, bool for_write, int timeout_ms) { + WSAPOLLFD fd={sock, (SHORT)(for_write ? POLLWRNORM : POLLRDNORM), 0}; + return WSAPoll(&fd, 1, timeout_ms); +} + +int pa_sock_connect(SOCKET sock, const struct sockaddr *addr, socklen_t addrlen) { + return connect(sock, addr, addrlen); +} + #else +#include +#include "pa_request.h" + void pa_socks_init() {} void pa_socks_done() {} @@ -75,4 +106,95 @@ int pa_socks_errno() { return errno; } +bool pa_socks_is_reset(int no) { + return no==ECONNRESET; +} + +bool pa_socks_is_interrupted(int no) { + // the request is interrupted by the response timeout: the error does not matter + if(thread_request && thread_request->get_skip()==Request::SKIP_INTERRUPTED) + thread_request->throw_interrupted(); + return no==EINTR; +} + +typedef struct timeval sock_time; + +static sock_time sock_now() { + sock_time now; + gettimeofday(&now, 0); + return now; +} + +static long long timeval_ms(const struct timeval &tv) { + return (long long)tv.tv_sec*1000+tv.tv_usec/1000; +} + +int pa_sock_ready(SOCKET sock, bool for_write, int timeout_ms) { + struct pollfd fd={sock, (short)(for_write ? POLLOUT : POLLIN), 0}; + sock_time start=sock_now(); + + int left=timeout_ms; + int result; + // GC stops the threads with signals: continue poll for the time left + while((result=poll(&fd, 1, left))<0 && pa_socks_is_interrupted(errno)) { + if(timeout_ms>0){ + long long passed=timeval_ms(sock_now()) - timeval_ms(start); + if(passed>=timeout_ms) + return 0; + left=passed<0 ? timeout_ms : timeout_ms-(int)passed; + } + } + return result; +} + +// the system restarts recv/send with the whole socket timeout again, under load it would never expire: wait for the remaining time +static bool sock_ready_within_remaining_timeout(SOCKET sock, bool for_write, sock_time start) { + struct timeval socket_timeout; + socklen_t timeout_size=sizeof(socket_timeout); + if(getsockopt(sock, SOL_SOCKET, for_write ? SO_SNDTIMEO : SO_RCVTIMEO, &socket_timeout, &timeout_size)<0 || !(socket_timeout.tv_sec || socket_timeout.tv_usec)) + return pa_sock_ready(sock, for_write, -1)>0; // no timeout + + long long passed=timeval_ms(sock_now())-timeval_ms(start); + long long left=timeval_ms(socket_timeout)-passed; + int ready=left>0 ? pa_sock_ready(sock, for_write, left0; +} + +int pa_sock_connect(SOCKET sock, const struct sockaddr *addr, socklen_t addrlen) { + sock_time start=sock_now(); + if(connect(sock, addr, addrlen)==0) + return 0; + if(!pa_socks_is_interrupted(errno)) + return -1; + + // the connection goes on in the background + if(!sock_ready_within_remaining_timeout(sock, true, start)) + return -1; + + int error=0; + socklen_t error_size=sizeof(error); + if(getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &error_size)<0) + return -1; + if(!error) + return 0; + errno=error; + return -1; +} + #endif + +ssize_t pa_sock_recv(SOCKET sock, char *buf, size_t len) { + sock_time start=sock_now(); + ssize_t result; + while((result=recv(sock, buf, len, 0)) < 0 && pa_socks_is_interrupted(pa_socks_errno()) && sock_ready_within_remaining_timeout(sock, false, start)); + return result; +} + +ssize_t pa_sock_send(SOCKET sock, const char *buf, size_t len) { + sock_time start=sock_now(); + ssize_t result; + while((result=send(sock, buf, len, 0)) < 0 && pa_socks_is_interrupted(pa_socks_errno()) && sock_ready_within_remaining_timeout(sock, true, start)); + return result; +}