[OpenWrt-Devel] [PATCH v2 04/10] ustream: add function ustream_fill_with_read_buf().

Yousong Zhou yszhou4tech at gmail.com
Wed Nov 12 08:59:17 EST 2014


Useful in the following use pattern.

	int available = ustream_pending_data(s, false);
	if (available >= sizeof(struct msghdr)) {
		struct msghdr h;
		ustream_fill_with_read_buf(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..64cdc6a 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_fill_with_read_buf(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..48dfa67 100644
--- a/ustream.h
+++ b/ustream.h
@@ -150,6 +150,11 @@ int ustream_vprintf(struct ustream *s, const char *format, va_list arg);
 
 /* ustream_get_read_buf: get a pointer to the next read buffer data */
 char *ustream_get_read_buf(struct ustream *s, int *buflen);
+/*
+ * ustream_fill_with_read_buf: fill buf with read buffer data and return
+ * size of actual data written.
+ :*/
+int ustream_fill_with_read_buf(struct ustream *s, char *buf, int buflen);
 
 /*
  * ustream_set_read_blocked: set read blocked state
-- 
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