コード例 #1
0
size_t ringbuf_findchr(const struct ringbuf_t *rb, int c, size_t offset)
{
	const uint8_t *bufend = ringbuf_end(rb);
	size_t bytes_used = ringbuf_bytes_used(rb);
	if (offset >= bytes_used)
		return bytes_used;

	const uint8_t *start = rb ->buf + (((rb->tail - rb->buf) + offset) % ringbuf_buffer_size(rb));
	lwIP_ASSERT0(bufend > start);
	size_t n = LWIP_MIN(bufend - start, bytes_used - offset);
	const uint8_t *found = (const uint8_t *)memchr(start, c, n);
	if (found)
		return offset + (found - start);
	else
		return ringbuf_findchr(rb, c, offset + n);
}
コード例 #2
0
ファイル: echoev.c プロジェクト: dhess/echoev
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);
        }
    }
}