statsmc.c
int
sp_memcache_connect(backend_t *bep)
{
    /* Open a non-blocking socket connection to one of the memcache processes.
     * If the connection fails, return a relevant error code to the caller.
     */
    int err = 0;
    int32_t res = 0;
    struct sockaddr_in serv_addr;
    socklen_t reslen = sizeof(res);
    int timeout_ms = bep->settings.connect_ms;
    
    assert(bep->fd == -1);

    memset(&serv_addr, 0, sizeof serv_addr);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = bep->settings.backaddr;
    serv_addr.sin_port   = htons(bep->settings.backport);

    bep->fd = socket(AF_INET, SOCK_STREAM, 0);
    bail_require_msg(bep->fd >= 0, "Cannot open socket for %s:%d, %s", 
                     bep->settings.backhost, bep->settings.backport,
                     strerror(errno));

    if (fcntl(bep->fd, F_SETFL, O_NONBLOCK) < 0) {
        bail_force_msg("Cannot set socket as non-block for %s:%d, %s",
                       bep->settings.backhost, bep->settings.backport,
                       strerror(errno));
    }

    if (connect(bep->fd, (struct sockaddr *) &serv_addr,
                                             sizeof serv_addr) < 0) {
        err = errno;
        if (errno != EINPROGRESS) {
            bail_force_msg("Cannot connect to %s:%d, %s",
                            bep->settings.backhost, bep->settings.backport,
                             strerror(errno));
        }

        struct pollfd fds;
        memset(&fds, 0, sizeof(fds));
        fds.fd = bep->fd;
        fds.events = (POLLOUT | POLLERR | POLLHUP | POLLNVAL);
        if (poll(&fds, 1, timeout_ms) <= 0) {
            errno = ETIMEDOUT;
            bail_force_msg("connect timed out to %s:%d, %s", 
                           bep->settings.backhost, bep->settings.backport,
                           strerror(errno));
        }
    }

    if (getsockopt(bep->fd, SOL_SOCKET, SO_ERROR, (void *)&res, &reslen) < 0) {
        err = errno;
        bail_force_msg("cannot get socket status for %s:%d, %s",
                       bep->settings.backhost, bep->settings.backport,
                       strerror(errno));
    } else {
        err = 0;
    }

    if (res) {
        errno = err = res;
        bail_force_msg("cannot connect to %s:%d, %s", 
                       bep->settings.backhost, bep->settings.backport,
                       strerror(errno));
    }
    return err;

 bail:
    if (bep->fd >= 0) {
        close(bep->fd);
        bep->fd = -1;
    }
    bep->last_error = err;
    return err;
}
// make a socket stream connection to a memcache server
make a socket stream connection to a memcache server
make a socket stream connection to a memcache server
int
sp_memcache_connect(backend_t *bep)
{
    int err = 0;
    int32_t res = 0;
    struct sockaddr_in serv_addr;
    socklen_t reslen = sizeof(res);
    int timeout_ms = bep->settings.connect_ms;
    assert(bep->fd == -1);
    memset(&serv_addr, 0, sizeof serv_addr);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = bep->settings.backaddr;
    serv_addr.sin_port   = htons(bep->settings.backport);
    bep->fd = socket(AF_INET, SOCK_STREAM, 0);
    bail_require_msg(bep->fd >= 0, "Cannot open socket for %s:%d, %s", 
                     bep->settings.backhost, bep->settings.backport,
                     strerror(errno));
    if (fcntl(bep->fd, F_SETFL, O_NONBLOCK) < 0) {
        bail_force_msg("Cannot set socket as non-block for %s:%d, %s",
                       bep->settings.backhost, bep->settings.backport,
                       strerror(errno));
    }
    if (connect(bep->fd, (struct sockaddr *) &serv_addr,
                                             sizeof serv_addr) < 0) {
        err = errno;
        if (errno != EINPROGRESS) {
            bail_force_msg("Cannot connect to %s:%d, %s",
                            bep->settings.backhost, bep->settings.backport,
                             strerror(errno));
        }
        struct pollfd fds;
        memset(&fds, 0, sizeof(fds));
        fds.fd = bep->fd;
        fds.events = (POLLOUT | POLLERR | POLLHUP | POLLNVAL);
        if (poll(&fds, 1, timeout_ms) <= 0) {
            errno = ETIMEDOUT;
            bail_force_msg("connect timed out to %s:%d, %s", 
                           bep->settings.backhost, bep->settings.backport,
                           strerror(errno));
        }
    }
    if (getsockopt(bep->fd, SOL_SOCKET, SO_ERROR, (void *)&res, &reslen) < 0) {
        err = errno;
        bail_force_msg("cannot get socket status for %s:%d, %s",
                       bep->settings.backhost, bep->settings.backport,
                       strerror(errno));
    } else {
        err = 0;
    }
    if (res) {
        errno = err = res;
        bail_force_msg("cannot connect to %s:%d, %s", 
                       bep->settings.backhost, bep->settings.backport,
                       strerror(errno));
    }
    return err;
 bail:
    if (bep->fd >= 0) {
        close(bep->fd);
        bep->fd = -1;
    }
    bep->last_error = err;
    return err;
}
int sp_memcache_connect ( backend_t * bep ) { int err = 0 ; int32_t res = 0 ; struct sockaddr_in serv_addr ; socklen_t reslen = sizeof ( res ) ; int timeout_ms = bep -> settings . connect_ms ; assert ( bep -> fd == -1 ) ; memset ( & serv_addr , 0 , sizeof serv_addr ) ; serv_addr . sin_family = AF_INET ; serv_addr . sin_addr . s_addr = bep -> settings . backaddr ; serv_addr . sin_port = htons ( bep -> settings . backport ) ; bep -> fd = socket ( AF_INET , SOCK_STREAM , 0 ) ; bail_require_msg ( bep -> fd >= 0 , " " , bep -> settings . backhost , bep -> settings . backport , strerror ( errno ) ) ; if ( fcntl ( bep -> fd , F_SETFL , O_NONBLOCK ) < 0 ) { bail_force_msg ( " " , bep -> settings . backhost , bep -> settings . backport , strerror ( errno ) ) ; } if ( connect ( bep -> fd , ( struct sockaddr * ) & serv_addr , sizeof serv_addr ) < 0 ) { err = errno ; if ( errno != EINPROGRESS ) { bail_force_msg ( " " , bep -> settings . backhost , bep -> settings . backport , strerror ( errno ) ) ; } struct pollfd fds ; memset ( & fds , 0 , sizeof ( fds ) ) ; fds . fd = bep -> fd ; fds . events = ( POLLOUT | POLLERR | POLLHUP | POLLNVAL ) ; if ( poll ( & fds , 1 , timeout_ms ) <= 0 ) { errno = ETIMEDOUT ; bail_force_msg ( " " , bep -> settings . backhost , bep -> settings . backport , strerror ( errno ) ) ; } } if ( getsockopt ( bep -> fd , SOL_SOCKET , SO_ERROR , ( void * ) & res , & reslen ) < 0 ) { err = errno ; bail_force_msg ( " " , bep -> settings . backhost , bep -> settings . backport , strerror ( errno ) ) ; } else { err = 0 ; } if ( res ) { errno = err = res ; bail_force_msg ( " " , bep -> settings . backhost , bep -> settings . backport , strerror ( errno ) ) ; } return err ; bail : if ( bep -> fd >= 0 ) { close ( bep -> fd ) ; bep -> fd = -1 ; } bep -> last_error = err ; return err ; }
make a socket stream connection to a memcache server
make a socket stream connection to a memcache server
/* Open a non-blocking socket connection to one of the memcache processes.
     * If the connection fails, return a relevant error code to the caller.
     */