static void start_connect()

in wb/wb.c [2361:2501]


static void start_connect(struct connection * c)
{
    apr_status_t rv;

    if (!(started < requests))
        return;

#ifdef _WAF_BENCH_ // make sure not exceeding RPS limit
    static int connection_rps_banned = 0;
    if  (g_RPS_NUMBER > 0 && done > (g_RPS_NUMBER * (apr_time_now() - start) / APR_USEC_PER_SEC )) {
        connection_rps_banned ++;

        // ban the connect if it exceeds RPS now
        if (connection_rps_banned < concurrency) 
            return;

        // if all connections are banned, wait for timeout
        while  (g_RPS_NUMBER > 0 && done > (g_RPS_NUMBER * (apr_time_now() - start) / APR_USEC_PER_SEC ))
                apr_sleep(1);

        // restart all connections
        connection_rps_banned = 0;
        int i;
        for (i = 0; i < concurrency; i++) 
            start_connect(&con[i]);
    }
#endif //_WAF_BENCH_ // // make sure not exceeding RPS limit

    c->read = 0;
    c->bread = 0;
    c->keepalive = 0;
    c->cbx = 0;
    c->gotheader = 0;
    c->rwrite = 0;
    if (c->ctx)
        apr_pool_clear(c->ctx);
    else
        apr_pool_create(&c->ctx, cntxt);

    if ((rv = apr_socket_create(&c->aprsock, destsa->family,
                SOCK_STREAM, 0, c->ctx)) != APR_SUCCESS) {
    apr_err("socket", rv);
    }

    if (myhost) {
        if ((rv = apr_socket_bind(c->aprsock, mysa)) != APR_SUCCESS) {
            apr_err("bind", rv);
        }
    }

    c->pollfd.desc_type = APR_POLL_SOCKET;
    c->pollfd.desc.s = c->aprsock;
    c->pollfd.reqevents = 0;
    c->pollfd.client_data = c;

    if ((rv = apr_socket_opt_set(c->aprsock, APR_SO_NONBLOCK, 1))
         != APR_SUCCESS) {
        apr_err("socket nonblock", rv);
    }

    if (windowsize != 0) {
        rv = apr_socket_opt_set(c->aprsock, APR_SO_SNDBUF,
                                windowsize);
        if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
            apr_err("socket send buffer", rv);
        }
        rv = apr_socket_opt_set(c->aprsock, APR_SO_RCVBUF,
                                windowsize);
        if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
            apr_err("socket receive buffer", rv);
        }
    }

    c->start = lasttime = apr_time_now();
#ifdef USE_SSL
    if (is_ssl) {
        BIO *bio;
        apr_os_sock_t fd;

        if ((c->ssl = SSL_new(ssl_ctx)) == NULL) {
            BIO_printf(bio_err, "SSL_new failed.\n");
            ERR_print_errors(bio_err);
            exit(1);
        }
        ssl_rand_seed();
        apr_os_sock_get(&fd, c->aprsock);
        bio = BIO_new_socket(fd, BIO_NOCLOSE);
        BIO_set_nbio(bio, 1);
        SSL_set_bio(c->ssl, bio, bio);
        SSL_set_connect_state(c->ssl);
        if (verbosity >= 4) {
            BIO_set_callback(bio, ssl_print_cb);
            BIO_set_callback_arg(bio, (void *)bio_err);
        }
#ifdef HAVE_TLSEXT
        if (tls_sni) {
            SSL_set_tlsext_host_name(c->ssl, tls_sni);
        }
#endif
    } else {
        c->ssl = NULL;
    }
#endif
    if ((rv = apr_socket_connect(c->aprsock, destsa)) != APR_SUCCESS) {
        if (APR_STATUS_IS_EINPROGRESS(rv)) {
            set_conn_state(c, STATE_CONNECTING);
            c->rwrite = 0;
            return;
        }
        else {
            set_conn_state(c, STATE_UNCONNECTED);
            apr_socket_close(c->aprsock);
            if (good == 0 && destsa->next) {
                destsa = destsa->next;
                err_conn = 0;
            }
            else if (bad++ > 10) {
                fprintf(stderr,
                   "\nTest aborted after 10 failures\n\n");
                apr_err("apr_socket_connect()", rv);
            }
            else {
                err_conn++;
            }

            start_connect(c);
            return;
        }
    }

    /* connected first time */
    set_conn_state(c, STATE_CONNECTED);
#ifdef USE_SSL
    if (c->ssl) {
        ssl_proceed_handshake(c);
    } else
#endif
    {
        write_request(c);
    }
}