Changeset 474:5da95acd7b11 in verona


Ignore:
Timestamp:
May 3, 2012 1:43:14 PM (14 months ago)
Author:
Vadim Lebedev <vadim@…>
Branch:
default
Message:

fixes for WIN32 and OS/X to avoid blocking recv and SIGPIPE

File:
1 edited

Legend:

Unmodified
Added
Removed
  • phapi/ph_socks_rtp.c

    r472 r474  
    3636#ifndef WIN32 
    3737#define closesocket close 
     38#else 
     39static int 
     40socknonblocking(SOCKET sock, int yes) 
     41{ 
     42   return  sockioctl(sock, FIONBIO, (char *)&yes); 
     43} 
     44 
     45static int 
     46recvAll(SOCKET sock, char* buf, size_t s) 
     47{ 
     48        // the socket is in non blocking mode in WIN32 
     49        int count = 100; 
     50        int total = 0; 
     51 
     52        while(total < (int) s && count--) { 
     53                int res = recv(sock, buf, s, 0); 
     54 
     55                if (res == SOCKET_ERROR) { 
     56                        int err = WSAGetLastError(); 
     57 
     58                        if (err == WSAEWOULDBLOCK) { 
     59                                continue; 
     60                        } 
     61                        return -1; 
     62                } 
     63                toal += res; 
     64                buf += res; 
     65                s -= res; 
     66 
     67        } 
     68 
     69        return total; 
     70} 
     71#endif 
     72 
     73#ifndef MSG_NOSIGNAL 
     74#define MSG_NOSIGNAL 0 
    3875#endif 
    3976 
     
    96133                connreq.port =  as_sin(&sk->addr).sin_port; 
    97134 
    98                 res = send(sk->socket, &connreq, 10, 0); 
     135                res = send(sk->socket, &connreq, 10, MSG_DONTWAIT|MSG_NOSIGNAL); 
    99136                if (res != 10) 
    100137                        return -1; 
     
    106143        plen = htonl(slen); 
    107144 
    108         res = send(sk->socket, &plen, 4, 0); 
     145        res = send(sk->socket, &plen, 4, MSG_DONTWAIT|MSG_NOSIGNAL); 
    109146        if (res != 4) 
    110147                return -1; 
    111         res = send(sk->socket, m->b_rptr, slen, MSG_DONTWAIT); 
     148        res = send(sk->socket, m->b_rptr, slen, MSG_DONTWAIT|MSG_NOSIGNAL); 
    112149        return res; 
    113150} 
     
    141178                return 0; 
    142179 
    143  
     180#ifdef WIN32 
     181        res = recvAll(sk->socket, m->b_wptr, expectlen); 
     182#else 
    144183        res = recv(sk->socket, m->b_wptr, expectlen, 0); 
     184#endif 
    145185        if (res <= 0) 
    146186                return res; 
     
    203243        struct in_addr ina; 
    204244 
    205         ret = send(sk->socket, msg1, 3, 0); 
     245        ret = send(sk->socket, msg1, 3, MSG_NOSIGNAL); 
    206246        if (ret != 3) 
    207247                return -1; 
     
    223263        req.dport  = as_sin(&sk->addr).sin_port; 
    224264 
    225         ret = send(sk->socket, &req, sizeof(req), 0); 
     265        ret = send(sk->socket, &req, sizeof(req), MSG_NOSIGNAL); 
    226266        if (ret != sizeof(req)) 
    227267                return -1; 
     
    240280                return -1; 
    241281 
    242         ret = recv(sk->socket, &resp.destip, 6, 0); 
     282        ret = recv(sk->socket, &resp.destip, 6, MSG_NOSIGNAL); 
    243283        if (ret != 6) 
    244284                return 0; 
     
    246286        sk->publicip = resp.destip; 
    247287        sk->publicport = resp.dport; 
     288 
     289#ifdef WIN32 
     290        /* make socket non-blocking */ 
     291        { 
     292                char yes = 1; 
     293 
     294                 sockioctl(sk->socket, FIONBIO, (char *)&yes); 
     295        } 
     296#endif 
     297#ifdef APPLE 
     298        { 
     299                int set = 1; 
     300                setsockopt(sk->socket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int)); 
     301        } 
     302#endif 
     303 
    248304 
    249305        return 0; 
Note: See TracChangeset for help on using the changeset viewer.