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

Message ID 1415800763-14311-5-git-send-email-yszhou4tech@gmail.com
State Changes Requested
Headers show

Commit Message

Yousong Zhou Nov. 12, 2014, 1:59 p.m. UTC
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@gmail.com>
---
 ustream.c |   24 ++++++++++++++++++++++++
 ustream.h |    5 +++++
 2 files changed, 29 insertions(+)

Patch
diff mbox

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