void conn_add_data(struct connection *conn, uint8_t *data, int n, struct buf_ptr *start, struct buf_ptr *end) { struct mhdr *queue = &conn->info->local_data; struct context *ctx = conn->ctx; struct mbuf *buf = mbuf_queue_get(ctx, queue); int remain = n, wlen, size, len = 0; if (remain > 0 && start != NULL) { start->pos = buf->last; start->buf = buf; } while (remain > 0) { wlen = mbuf_write_size(buf); size = remain < wlen ? remain : wlen; memcpy(buf->last, data + len, size); buf->last += size; len += size; remain -= size; if (remain <= 0 && end != NULL) { end->pos = buf->last; end->buf = buf; } if (wlen - size <= 0) { buf = mbuf_queue_get(ctx, queue); } } }
void conn_add_data(struct connection *conn, uint8_t *data, int n, struct buf_ptr *start, struct buf_ptr *end) { // get buffer from local_data. struct mbuf *buf = conn_get_buf(conn, false, true); int remain = n, wlen, size, len = 0; if (remain > 0 && start != NULL) { start->pos = buf->last; start->buf = buf; } while (remain > 0) { wlen = mbuf_write_size(buf); size = remain < wlen ? remain : wlen; memcpy(buf->last, data + len, size); buf->last += size; len += size; remain -= size; if (remain <= 0 && end != NULL) { end->pos = buf->last; end->buf = buf; } if (wlen - size <= 0) { buf = conn_get_buf(conn, false, true); } } }
void mbuf_queue_copy(struct context *ctx, struct mhdr *q, uint8_t *data, int n) { struct mbuf *buf = mbuf_queue_get(ctx, q); int remain = n, wlen, size, len = 0; while (remain > 0) { wlen = mbuf_write_size(buf); size = remain < wlen ? remain : wlen; memcpy(buf->last, data + len, size); buf->last += size; len += size; remain -= size; if (wlen - size <= 0) { buf = mbuf_queue_get(ctx, q); } } }
int socket_read(int fd, struct mbuf *buf) { ssize_t n; while (1) { n = read(fd, buf->last, mbuf_write_size(buf)); if (n == -1) { switch (errno) { case EINTR: continue; case EAGAIN: return CORVUS_AGAIN; default: LOG(WARN, "socket read: %s", strerror(errno)); return CORVUS_ERR; } } buf->last += n; return n; } return CORVUS_ERR; }