diff mbox

[OpenWrt-Devel,4/5] ustream: add function ustream_fill_with_read_buf().

Message ID 1415703092-46458-4-git-send-email-yszhou4tech@gmail.com
State Superseded
Headers show

Commit Message

Yousong Zhou Nov. 11, 2014, 10:51 a.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(+)

Comments

Felix Fietkau Dec. 11, 2014, 4:33 p.m. UTC | #1
On 2014-11-11 11:51, Yousong Zhou wrote:
> 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>
I'd prefer to have this named ustream_read() instead - similar to
ustream_write().

- Felix
Yousong Zhou Dec. 12, 2014, 4:01 a.m. UTC | #2
On 12 December 2014 at 00:33, Felix Fietkau <nbd@openwrt.org> wrote:
> On 2014-11-11 11:51, Yousong Zhou wrote:
>> 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>
> I'd prefer to have this named ustream_read() instead - similar to
> ustream_write().

Makes sense.  Will adapt the patch.

                yousong

>
> - Felix
diff mbox

Patch

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