Example #1
0
int32_t ringbuf_write_string(ringbuf_t *buf, char *str)
{
    int8_t n = 1;
    int32_t i = 0;
    while (str[i] != '\0' && n > 0) {
        n = ringbuf_write(buf, (uint8_t)str[i++]);
    }
    return i + n - 1;
}
Example #2
0
int32_t ringbuf_writes(ringbuf_t *buf, uint8_t *data, uint16_t size)
{
    if (buf == NULL) {
        return -1;
    }
    uint16_t i = 0;
    int8_t n = 1;
    while (n > 0 && i < size) {
        n = ringbuf_write(buf, data[i]);
    }
    return (i == 0 && n < 0) ? n : i;
}
Example #3
0
int32_t ringbuf_copy(ringbuf_t *src, ringbuf_t *dst, uint16_t size)
{
    uint8_t data;
    int32_t ret = 1;
    int32_t sum = 0;
    while (size > 0 && ret) {
        ringbuf_read(src, &data);
        ret = ringbuf_write(dst, data);
        sum += ret;
        --size;
    }
    return sum;
}
Example #4
0
static bool hf_can_write_data(struct io *io, void *user_data)
{
	struct hfp_hf *hfp = user_data;
	ssize_t bytes_written;

	bytes_written = ringbuf_write(hfp->write_buf, hfp->fd);
	if (bytes_written < 0)
		return false;

	if (ringbuf_len(hfp->write_buf) > 0)
		return true;

	return false;
}
Example #5
0
void
echo_cb(EV_P_ ev_io *w_, int revents)
{
    log(LOG_DEBUG, "echo_cb called");

    echo_io *w = (echo_io *) w_;
    msg_buf *buf = &w->buf;

    if (revents & EV_WRITE) {
        log(LOG_DEBUG, "echo_cb write event");

        bool buf_is_full = ringbuf_is_full(buf->rb);
        while (buf->msg_len) {
            ssize_t n = ringbuf_write(w->io.fd,
                                      buf->rb,
                                      buf->msg_len);
            if (n == -1) {
                if ((errno == EAGAIN) ||
                    (errno == EWOULDBLOCK) ||
                    (errno == EINTR))
                    break;
                else {
                    log(LOG_ERR, "Write on descriptor %d failed: %m", w->io.fd);
                    stop_echo_watcher(EV_A_ w);
                    return;
                }
            } else {
                buf->msg_len -= n;
                w->timeout.last_activity = ev_now(EV_A);
                log(LOG_DEBUG, "echo_cb %zd bytes written", n);

                /*
                 * Re-enable reads if they're paused due to buffer
                 * pressure.
                 */
                if (buf_is_full && !w->half_closed) {
                    log(LOG_DEBUG, "echo_cb re-starting reads.");
                    reset_echo_watcher(EV_A_ &w->io, EV_READ | EV_WRITE);
                    buf_is_full = false;
                }
            }
        }
        if (buf->msg_len == 0) {
            size_t eol = ringbuf_findchr(buf->rb,
                                         MSG_DELIMITER,
                                         buf->search_offset);
            if (eol < ringbuf_bytes_used(buf->rb)) {
                buf->search_offset = 0;
                buf->msg_len = eol + 1;
            } else {
                if (w->half_closed)
                    stop_echo_watcher(EV_A_ w);
                else {
                    buf->search_offset = eol;
                    reset_echo_watcher(EV_A_ &w->io, EV_READ);
                }
            }
        }
    }
    
    if (revents & EV_READ) {
        log(LOG_DEBUG, "echo_cb read event");
        size_t nread = 0;
        while (ringbuf_bytes_free(buf->rb)) {
            ssize_t n = ringbuf_read(w->io.fd,
                                     buf->rb,
                                     ringbuf_bytes_free(buf->rb));
            if (n == 0) {

                /* EOF: drain remaining writes or close connection */
                log(LOG_DEBUG, "echo_cb EOF received");
                w->timeout.last_activity = ev_now(EV_A);
                if (buf->msg_len) {
                    w->half_closed = true;
                    reset_echo_watcher(EV_A_ &w->io, EV_WRITE);
                } else
                    stop_echo_watcher(EV_A_ w);
                return;
            }
            else if (n == -1) {
                if ((errno == EAGAIN) ||
                    (errno == EWOULDBLOCK) ||
                    (errno == EINTR)) {

                    /* Nothing more to read for now. */
                    return;
                } else {
                    log(LOG_ERR, "Read on descriptor %d failed: %m", w->io.fd);
                    stop_echo_watcher(EV_A_ w);
                    return;
                }
            } else {
                nread += n;
                w->timeout.last_activity = ev_now(EV_A);
                log(LOG_DEBUG, "echo_cb %zd bytes read", n);

                /*
                 * If there's no pending message to send, look for a
                 * new one. If found, enable writes.
                 */
                if (buf->msg_len == 0) {
                    size_t eol = ringbuf_findchr(buf->rb,
                                                 MSG_DELIMITER,
                                                 buf->search_offset);
                    if (eol < ringbuf_bytes_used(buf->rb)) {
                        buf->search_offset = 0;
                        buf->msg_len = eol + 1;
                        reset_echo_watcher(EV_A_ &w->io, EV_WRITE | EV_READ);
                    } else
                        buf->search_offset = eol;
                }
            }
        }

        /*
         * If we get here, the buffer is full. If there's a pending
         * message waiting to be written, disable reads until the
         * writes free up space. If there's no pending message, we've
         * overflowed.
         */
        if (buf->msg_len) {
            log(LOG_DEBUG,
                "echo_cb buffer full, disabling reads on fd %d.",
                w->io.fd);
            reset_echo_watcher(EV_A_ &w->io, EV_WRITE);
        } else {
            log(LOG_WARNING, "Read overflow on descriptor %d.", w->io.fd);
            stop_echo_watcher(EV_A_ w);
        }
    }
}