[OpenWrt-Devel] [PATCH 1/3] ustream: add function ustream_read().

Felix Fietkau nbd at openwrt.org
Mon Dec 22 09:46:04 EST 2014


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 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;
> +}
> +
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
_______________________________________________
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