libspopc SO_SNDTIMEO/SO_RCVTIMEO Win32 patch

In case you’re already using or would like to use libspopc in your project and you’re making it for Win32, make sure that socket_prepare() code resembles the one below:

/* prepares the socket options
 * mainly set read an write timeouts
 * this way may be either ignored
 * or refused on some platform
 */
DLLIMPORT int socket_prepare(int sock){
    int ret=0;

#ifdef WIN32
    int timeout = SOCKET_TIMEOUT * 1000;
    if (ret += setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, (void*)&timeout, sizeof timeout))
        perror("socket_prepare.setsockopt");
    if (ret += setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO, (void*)&timeout, sizeof timeout))
        perror("socket_prepare.setsockopt");
#else
    struct timeval tv;
    tv.tv_sec = SOCKET_TIMEOUT;
    tv.tv_usec = 0 ;

    if (ret += setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, (void*)&tv, sizeof tv))
        perror("socket_prepare.setsockopt");
    if (ret += setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO, (void*)&tv, sizeof tv))
        perror("socket_prepare.setsockopt");
#endif
    return ret;
}

I had to patch it adding the WIN32 #ifdef because of SO_SNDTIMEO/SO_RCVTIMEO socket options incompatibilities between M$ Sockets and Berkeley Sockets. The latter take a struct timeval as parameter, the former – just a single integer which indicates the number of milliseconds! I’ve informed Benoit Rouits about this finding and hope he will patch it in his repo.

Leave a Reply

Your email address will not be published. Required fields are marked *