[OpenWrt-Devel] [PATCH 1/3] ustream: add function ustream_read().
Yousong Zhou
yszhou4tech at gmail.com
Tue Dec 16 16:15:35 EST 2014
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 at 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;
+}
+
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, ...);
--
1.7.10.4
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
More information about the openwrt-devel
mailing list