Message ID | 1418764537-36787-2-git-send-email-yszhou4tech@gmail.com |
---|---|
State | Changes Requested |
Headers | show |
On 2014-12-16 22:15, Yousong Zhou wrote: > It can be used to fill caller specified buffer area with data already in > ustream read buffer. Useful in the following use pattern. > > int available = ustream_pending_data(s, false); > if (available >= sizeof(struct msghdr)) { > struct msghdr h; > ustream_read(s, &h, sizeof(h)); > } > > Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com> > --- > ustream.c | 24 ++++++++++++++++++++++++ > ustream.h | 5 +++++ > 2 files changed, 29 insertions(+) > > diff --git a/ustream.c b/ustream.c > index 828a025..f1db809 100644 > --- a/ustream.c > +++ b/ustream.c > @@ -333,6 +333,30 @@ char *ustream_get_read_buf(struct ustream *s, int *buflen) > return data; > } > > +int ustream_read(struct ustream *s, char *buf, int buflen) > +{ > + int len = 0; > + int chunk_len; > + struct ustream_buf *sbuf; > + > + if (!s->r.head) { > + return 0; > + } > + > + sbuf = s->r.head; > + do { > + chunk_len = sbuf->tail - sbuf->data; > + if (chunk_len > buflen) > + chunk_len = buflen; > + memcpy(&buf[len], sbuf->data, chunk_len); > + buflen -= chunk_len; > + len += chunk_len; > + sbuf = sbuf->next; > + } while (buflen && sbuf); > + > + return len; > +} > + I'd expect ustream_read() to consume the data it has read. It should also probably just use ustream_get_read_buf() and ustream_consume() instead of open-coding its functionality. - Felix
On 22 December 2014 at 22:46, Felix Fietkau <nbd@openwrt.org> wrote: > On 2014-12-16 22:15, Yousong Zhou wrote: >> It can be used to fill caller specified buffer area with data already in >> ustream read buffer. Useful in the following use pattern. >> >> int available = ustream_pending_data(s, false); >> if (available >= sizeof(struct msghdr)) { >> struct msghdr h; >> ustream_read(s, &h, sizeof(h)); >> } >> <snip> >> + > I'd expect ustream_read() to consume the data it has read. > It should also probably just use ustream_get_read_buf() and > ustream_consume() instead of open-coding its functionality. I tend to peek inside the msghdr to get the total length then consume them as a whole if there is enough available in the read buffer. But I am fine with either way. Regards. yousong
diff --git a/ustream.c b/ustream.c index 828a025..f1db809 100644 --- a/ustream.c +++ b/ustream.c @@ -333,6 +333,30 @@ char *ustream_get_read_buf(struct ustream *s, int *buflen) return data; } +int ustream_read(struct ustream *s, char *buf, int buflen) +{ + int len = 0; + int chunk_len; + struct ustream_buf *sbuf; + + if (!s->r.head) { + return 0; + } + + sbuf = s->r.head; + do { + chunk_len = sbuf->tail - sbuf->data; + if (chunk_len > buflen) + chunk_len = buflen; + memcpy(&buf[len], sbuf->data, chunk_len); + buflen -= chunk_len; + len += chunk_len; + sbuf = sbuf->next; + } while (buflen && sbuf); + + return len; +} + static void ustream_write_error(struct ustream *s) { if (!s->write_error) diff --git a/ustream.h b/ustream.h index 6431744..fadff3d 100644 --- a/ustream.h +++ b/ustream.h @@ -143,6 +143,11 @@ void ustream_free(struct ustream *s); /* ustream_consume: remove data from the head of the read buffer */ void ustream_consume(struct ustream *s, int len); +/* + * ustream_read: fill buf with data in internal read buffer. + * return size of filled data. + :*/ +int ustream_read(struct ustream *s, char *buf, int buflen); /* ustream_write: add data to the write buffer */ int ustream_write(struct ustream *s, const char *buf, int len, bool more); int ustream_printf(struct ustream *s, const char *format, ...);
It can be used to fill caller specified buffer area with data already in ustream read buffer. Useful in the following use pattern. int available = ustream_pending_data(s, false); if (available >= sizeof(struct msghdr)) { struct msghdr h; ustream_read(s, &h, sizeof(h)); } Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com> --- ustream.c | 24 ++++++++++++++++++++++++ ustream.h | 5 +++++ 2 files changed, 29 insertions(+)