Beispiel #1
0
int32_t ringbuf_reads(ringbuf_t *buf, uint8_t *data,  uint16_t maxSize)
{
    if (buf == NULL) {
        return -1;
    }
    uint16_t i = 0;
    while (i < maxSize && buf->count > 0) {
        ringbuf_read(buf, &data[i++]);
    }
    return i;
}
Beispiel #2
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;
}
Beispiel #3
0
static bool hf_can_read_data(struct io *io, void *user_data)
{
	struct hfp_hf *hfp = user_data;
	ssize_t bytes_read;

	bytes_read = ringbuf_read(hfp->read_buf, hfp->fd);
	if (bytes_read < 0)
		return false;

	hf_process_input(hfp);

	return true;
}
Beispiel #4
0
static void dump_ringbuffer() {
  byte_t buf_data[1025];
  uint_t buf_length;
  uint_t buf_dropped;

  do {
    buf_length = sizeof(buf_data) - 1;
    ringbuf_read(buf_data, &buf_length, &buf_dropped, ringbuf_data, &ringbuf_head, &ringbuf_length, &ringbuf_dropped, ringbuf_size);

    if(buf_length) {
      buf_data[buf_length] = 0;
      fprintf(stderr, "dropped = %lu\n[%s]\n", buf_dropped, buf_data);
    }
  }
  while(buf_length > 0);
}
Beispiel #5
0
static bool can_read_data(struct io *io, void *user_data)
{
	struct hfp_gw *hfp = user_data;
	ssize_t bytes_read;

	bytes_read = ringbuf_read(hfp->read_buf, hfp->fd);
	if (bytes_read < 0)
		return false;

	if (hfp->result_pending)
		return true;

	process_input(hfp);

	return true;
}
Beispiel #6
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);
        }
    }
}