Ejemplo n.º 1
0
void
lcb_luv_read_nudge(lcb_luv_socket_t sock)
{
    int status;
    if (sock->read.readhead_active) {
        log_read_trace("Read-ahead already active");
        return; /* nothing to do here */
    }

    status = uv_read_start((uv_stream_t*)&sock->tcp, alloc_cb, read_cb);

    if (status) {
        sock->evstate[LCB_LUV_EV_READ].err =
                lcb_luv_errno_map(
                        (uv_last_error(sock->parent->loop)).code);
        log_read_error("Couldn't start read: %d",
                  sock->evstate[LCB_LUV_EV_READ].err);
    } else {
        log_read_debug("read-ahead initialized");
        sock->read.buf.len = LCB_LUV_READAHEAD;
        sock->read.buf.base = sock->read.data;
        lcb_luv_socket_ref(sock);
        sock->read.readhead_active = 1;
    }
}
Ejemplo n.º 2
0
static void write_cb(uv_write_t *req, int status)
{
    lcb_luv_socket_t sock = (lcb_luv_socket_t)req->data;
    struct lcb_luv_evstate_st *evstate;

    if (!sock) {
        fprintf(stderr, "Got write callback (req=%p) without socket\n",
                (void *)req);
        return;
    }

    evstate = EVSTATE_FIND(sock, WRITE);

    if (status) {
        evstate->err =
            lcb_luv_errno_map((uv_last_error(sock->parent->loop)).code);
    }
    log_write_debug("Flush done. Flushed %d bytes", sock->write.buf.len);
    sock->write.pos = 0;
    sock->write.nb = 0;
    evstate->flags |= LCB_LUV_EVf_PENDING;
    evstate->flags &= ~(LCB_LUV_EVf_FLUSHING);

    if (SOCK_EV_ENABLED(sock, WRITE)) {
        sock->event->lcb_cb(sock->idx, LCB_WRITE_EVENT, sock->event->lcb_arg);

        if (sock->write.nb) {
            evstate->flags &= ~(LCB_LUV_EVf_PENDING);
            lcb_luv_flush(sock);
        }
    }

    lcb_luv_socket_unref(sock);
}
Ejemplo n.º 3
0
/**
 * Flush the write buffers
 */
void lcb_luv_flush(lcb_luv_socket_t sock)
{
    int status;
    struct lcb_luv_evstate_st *evstate;
    if (sock->write.nb == 0) {
        return;
    }

    evstate = EVSTATE_FIND(sock, WRITE);
    if (EVSTATE_IS(evstate, FLUSHING)) {
        log_write_info("Not flushing because we are in the middle of a flush");
        return;
    }

    sock->write.buf.base = sock->write.data;
    sock->write.buf.len = sock->write.nb;
    log_write_debug("Will flush");
    status = uv_write(&sock->u_req.write,
                      (uv_stream_t *)&sock->tcp,
                      &sock->write.buf, 1, write_cb);
    lcb_luv_socket_ref(sock);

    if (status) {
        evstate->err =
            lcb_luv_errno_map((uv_last_error(sock->parent->loop)).code);
    }
    evstate->flags |= LCB_LUV_EVf_FLUSHING;
}
Ejemplo n.º 4
0
void
read_cb(uv_stream_t *stream, ssize_t nread, uv_buf_t buf)
{
    /* This is the same buffer structure we had before */
    lcb_luv_socket_t sock = (lcb_luv_socket_t)stream;
    int is_stopped = 0;
    (void)buf;

    lcb_luv_socket_ref(sock);
    log_read_debug("%d: nr=%d, len=%d", sock->idx, nread, buf.len);

    if (nread == -1) {
        uv_err_t last_err = uv_last_error(sock->parent->loop);
        if (last_err.code == UV_EOF) {
            sock->eof = 1;
        } else {
            sock->evstate[LCB_LUV_EV_READ].err =
                    lcb_luv_errno_map(last_err.code);
        }
        lcb_luv_read_stop(sock);
    } else if (nread) {
        sock->read.buf.len -= nread;
        sock->read.buf.base += (size_t)nread;
        sock->read.nb += nread;

        /* We don't have any more space */
        if (!sock->read.buf.len) {
            lcb_luv_read_stop(sock);
        }
    } else {
        /* nread == 0 */
        goto GT_RET;
    }

    sock->evstate[LCB_LUV_EV_READ].flags |= LCB_LUV_EVf_PENDING;
    if (sock->event && (sock->event->lcb_events & LCB_READ_EVENT)) {
        sock->event->lcb_cb(sock->idx, LCB_READ_EVENT,
                sock->event->lcb_arg);
    }

    GT_RET:
    if (is_stopped) {
        lcb_luv_socket_unref(sock);
    }
    lcb_luv_socket_unref(sock);
}