Annotation of parser3/src/main/pa_socks.C, revision 1.39

1.3       paf         1: /**    @file
1.1       paf         2:        Parser: socks initialization/finalization.
                      3: 
1.35      moko        4:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.32      moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.15      paf         6: */
1.1       paf         7: 
1.39    ! moko        8: volatile const char * IDENT_PA_SOCKS_C="$Id: pa_socks.C,v 1.38 2026/10/17 22:58:09 moko Exp $";
1.2       paf         9: 
                     10: #include "pa_config_includes.h"
1.36      moko       11: #include "pa_socks.h"
1.1       paf        12: 
1.28      moko       13: #ifdef _MSC_VER
                     14: 
1.9       paf        15: #include "pa_exception.h"
1.19      paf        16: #include "pa_string.h"
1.11      paf        17: 
1.19      paf        18: #include <windows.h>
                     19: 
1.1       paf        20: WSADATA wsaData;
                     21: 
1.22      paf        22: void pa_socks_init() {
1.1       paf        23:        WORD wVersionRequested;
                     24:        int err; 
                     25:        wVersionRequested = MAKEWORD( 1, 1 ); 
                     26:        err = WSAStartup( wVersionRequested, &wsaData );
                     27:        if ( err != 0 ) {
1.28      moko       28:                /* Tell the user that we could not find a usable */
1.33      moko       29:                throw Exception(0, 0, "cannot WSAStartup, err=%d", err);
1.1       paf        30:        } 
                     31: }
                     32: 
1.22      paf        33: void pa_socks_done() {
1.1       paf        34:        /* Confirm that the WinSock DLL supports 2.2.*/
                     35:        /* Note that if the DLL supports versions greater    */
                     36:        /* than 2.2 in addition to 2.2, it will still return */
                     37:        /* 2.2 in wVersion since that is the version we      */
                     38:        /* requested.                                        */ 
1.28      moko       39:        if ( LOBYTE( wsaData.wVersion ) == 2 || HIBYTE( wsaData.wVersion ) == 2 ) {
                     40:                WSACleanup();
                     41:                return;
1.1       paf        42:        } 
                     43: }
                     44: 
1.23      paf        45: const char* pa_socks_strerr(int no) {
                     46:        char buf[MAX_STRING];
                     47:        buf[0]=0;
                     48:        size_t error_size=FormatMessage( 
                     49:                FORMAT_MESSAGE_FROM_SYSTEM | 
                     50:                FORMAT_MESSAGE_IGNORE_INSERTS,
                     51:                NULL,
                     52:                no,
                     53:                0, // Default language
                     54:                (LPTSTR) &buf,
                     55:                sizeof(buf),
                     56:                NULL 
                     57:        );
                     58:        if(error_size>3) // ".\r\n"
                     59:                buf[error_size-3]=0;
                     60:        return buf[0]? pa_strdup(buf): "unknown error";
                     61: }
                     62: int pa_socks_errno() {
                     63:        return WSAGetLastError();
                     64: }
                     65: 
1.37      moko       66: bool pa_socks_is_reset(int no) {
                     67:        return no==WSAECONNRESET || no==WSAECONNABORTED;
                     68: }
                     69: 
1.38      moko       70: bool pa_socks_is_interrupted(int no) {
                     71:        return no==WSAEINTR;
                     72: }
                     73: 
1.36      moko       74: int pa_sock_ready(SOCKET sock, bool for_write, int timeout_ms) {
                     75:        WSAPOLLFD fd={sock, (SHORT)(for_write ? POLLWRNORM : POLLRDNORM), 0};
                     76:        return WSAPoll(&fd, 1, timeout_ms);
                     77: }
                     78: 
1.1       paf        79: #else
1.10      paf        80: 
1.36      moko       81: #include <poll.h>
                     82: 
1.22      paf        83: void pa_socks_init() {}
                     84: void pa_socks_done() {}
1.23      paf        85: 
                     86: const char* pa_socks_strerr(int no) {
                     87:        return strerror(no);
                     88: }
                     89: int pa_socks_errno() {
                     90:        return errno;
                     91: }
1.1       paf        92: 
1.37      moko       93: bool pa_socks_is_reset(int no) {
                     94:        return no==ECONNRESET;
                     95: }
                     96: 
1.38      moko       97: bool pa_socks_is_interrupted(int no) {
                     98:        return no==EINTR;
                     99: }
                    100: 
1.36      moko      101: int pa_sock_ready(SOCKET sock, bool for_write, int timeout_ms) {
                    102:        struct pollfd fd={sock, (short)(for_write ? POLLOUT : POLLIN), 0};
1.39    ! moko      103:        struct timeval start;
        !           104:        gettimeofday(&start, 0);
        !           105: 
        !           106:        // GC stops the threads with signals, under load they come more often than the timeout expires: poll goes on for the time left
        !           107:        int left=timeout_ms;
        !           108:        int result;
        !           109:        while((result=poll(&fd, 1, left))<0 && pa_socks_is_interrupted(errno)) {
        !           110:                if(timeout_ms<0)
        !           111:                        continue;
        !           112:                struct timeval now;
        !           113:                gettimeofday(&now, 0);
        !           114:                long passed=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
        !           115:                left=passed<0 ? timeout_ms : passed<timeout_ms ? timeout_ms-(int)passed : 0;
        !           116:        }
        !           117:        return result;
1.36      moko      118: }
                    119: 
1.1       paf       120: #endif
1.38      moko      121: 
                    122: ssize_t pa_sock_recv(SOCKET sock, char *buf, size_t len) {
                    123:        ssize_t result;
                    124:        while((result=recv(sock, buf, len, 0)) < 0 && pa_socks_is_interrupted(pa_socks_errno()));
                    125:        return result;
                    126: }
                    127: 
                    128: ssize_t pa_sock_send(SOCKET sock, const char *buf, size_t len) {
                    129:        ssize_t result;
                    130:        while((result=send(sock, buf, len, 0)) < 0 && pa_socks_is_interrupted(pa_socks_errno()));
                    131:        return result;
                    132: }

E-mail: