Esempio n. 1
0
static void connect_done_handler(lcb_connection_t conn, lcb_error_t err)
{
    http_provider *http = (http_provider *)conn->data;
    const lcb_host_t *host = lcb_connection_get_host(conn);

    if (err != LCB_SUCCESS) {
        lcb_log(LOGARGS(http, ERR),
                "Connection to REST API @%s:%s failed with code=0x%x",
                host->host, host->port, err);

        io_error(http, err);
        return;
    }

    lcb_log(LOGARGS(http, DEBUG),
            "Successfuly connected to REST API %s:%s",
            host->host, host->port);

    lcb_connection_reset_buffers(conn);
    ringbuffer_strcat(conn->output, http->request_buf);
    lcb_assert(conn->output->nbytes > 0);

    lcb_sockrw_set_want(conn, LCB_RW_EVENT, 0);
    lcb_sockrw_apply_want(conn);
    lcb_timer_rearm(http->io_timer,
                    PROVIDER_SETTING(&http->base, config_node_timeout));
}
Esempio n. 2
0
static void socket_connected(connmgr_request *req)
{
    cccp_provider *cccp = req->data;
    lcb_connection_t conn = req->conn;
    struct lcb_nibufs_st nistrs;
    struct lcb_io_use_st use;
    struct negotiation_context *ctx;
    lcb_error_t err;

    free(req);
    cccp->cur_connreq = NULL;

    LOG(cccp, DEBUG, "CCCP Socket connected");

    if (!conn) {
        mcio_error(cccp, LCB_CONNECT_ERROR);
        return;
    }

    lcb_connuse_easy(&use, cccp, io_read_handler, io_error_handler);
    lcb_connection_transfer_socket(conn, &cccp->connection, &use);
    conn = NULL;


    if (cccp->connection.protoctx) {
        /** Already have SASL */
        if ((err = lcb_connection_reset_buffers(&cccp->connection)) != LCB_SUCCESS) {
            mcio_error(cccp, err);
            return;
        }

        request_config(cccp);
        return;
    }

    if (!lcb_get_nameinfo(&cccp->connection, &nistrs)) {
        mcio_error(cccp, LCB_EINTERNAL);
        return;
    }

    ctx = lcb_negotiation_create(&cccp->connection,
                                 cccp->base.parent->settings,
                                 PROVIDER_SETTING(&cccp->base,
                                         config_node_timeout),
                                 nistrs.remote,
                                 nistrs.local,
                                 &err);
    if (!ctx) {
        mcio_error(cccp, err);
    }

    ctx->complete = negotiation_done;
    ctx->data = cccp;
    cccp->connection.protoctx = ctx;
    cccp->connection.protoctx_dtor = (protoctx_dtor_t)lcb_negotiation_destroy;
}
Esempio n. 3
0
static void reset_stream_state(lcb_t instance)
{
    struct lcb_http_bootstrap_st *http = &instance->bootstrap.via.http;

    free(http->stream.input.data);
    free(http->stream.chunk.data);
    free(http->stream.header);
    memset(&http->stream, 0, sizeof(http->stream));
    lcb_assert(LCB_SUCCESS == lcb_connection_reset_buffers(&http->connection));
}
Esempio n. 4
0
static void reset_stream_state(http_provider *http)
{
    lcb_string_clear(&http->stream.chunk);
    lcb_string_clear(&http->stream.input);
    lcb_string_clear(&http->stream.header);

    if (http->stream.config) {
        lcb_clconfig_decref(http->stream.config);
    }

    http->stream.config = NULL;

    lcb_assert(LCB_SUCCESS == lcb_connection_reset_buffers(&http->connection));
}
Esempio n. 5
0
/**
 * Schedule a connection to the server
 */
void lcb_server_connect(lcb_server_t *server)
{
    lcb_connection_t conn = &server->connection;
    conn->on_connect_complete = lcb_server_connect_handler;
    conn->on_timeout = server_timeout_handler;
    conn->evinfo.handler = lcb_server_v0_event_handler;
    conn->completion.read = lcb_server_v1_read_handler;
    conn->completion.write = lcb_server_v1_write_handler;
    conn->completion.error = lcb_server_v1_error_handler;
    conn->timeout.usec = server->instance->config.operation_timeout;

    if (lcb_connection_reset_buffers(&server->connection) != LCB_SUCCESS) {
        lcb_error_handler(server->instance, LCB_CLIENT_ENOMEM, NULL);
    }

    lcb_connection_start(conn, LCB_CONNSTART_NOCB | LCB_CONNSTART_ASYNCERR);
}
Esempio n. 6
0
static lcb_error_t cccp_connect(lcb_t instance)
{
    lcb_error_t rc;
    lcb_connection_result_t connrc;
    protocol_binary_request_set_cluster_config req;
    lcb_server_t *server = &instance->bootstrap.via.cccp.server;
    lcb_connection_t conn = &server->connection;

    rc = lcb_init_next_host(instance, 11210);

    if (rc != LCB_SUCCESS) {
        return rc;
    }

    conn->on_connect_complete = lcb_server_connect_handler;
    conn->on_timeout = lcb_bootstrap_timeout_handler;
    conn->evinfo.handler = lcb_server_v0_event_handler;
    conn->completion.read = lcb_server_v1_read_handler;
    conn->completion.write = lcb_server_v1_write_handler;
    conn->completion.error = lcb_server_v1_error_handler;
    conn->timeout.usec = instance->config.bootstrap_timeout;

    if (lcb_connection_reset_buffers(conn) != LCB_SUCCESS) {
        return LCB_CLIENT_ENOMEM;
    }

    memset(&req, 0, sizeof(req));
    req.message.header.request.magic = PROTOCOL_BINARY_REQ;
    req.message.header.request.opcode = CMD_GET_CLUSTER_CONFIG;
    req.message.header.request.opaque = ++instance->seqno;
    lcb_server_complete_packet(server, NULL, req.bytes, sizeof(req.bytes));
    instance->last_error = LCB_SUCCESS;

    connrc = lcb_connection_start(conn,
                                  LCB_CONNSTART_NOCB | LCB_CONNSTART_ASYNCERR);
    if (connrc == LCB_CONN_ERROR) {
        return LCB_NETWORK_ERROR;
    }
    if (instance->config.syncmode == LCB_SYNCHRONOUS) {
        lcb_wait(instance);
    }

    return instance->last_error;
}
Esempio n. 7
0
lcb_error_t lcb_connection_init(lcb_connection_t conn, lcb_t instance)
{
    conn->instance = instance;

    conn->sockfd = INVALID_SOCKET;
    conn->state = LCB_CONNSTATE_UNINIT;
    conn->timeout.timer = instance->io->v.v0.create_timer(instance->io);

    if (LCB_SUCCESS != lcb_connection_reset_buffers(conn)) {
        lcb_connection_cleanup(conn);
        return LCB_CLIENT_ENOMEM;
    }
    if (conn->timeout.timer == NULL) {
        lcb_connection_cleanup(conn);
        return LCB_CLIENT_ENOMEM;
    }

    return LCB_SUCCESS;
}