libubox-ustream
参考:libubox [4] - uloop runqueue ustream
libubox提供了流缓冲管理,定义在文件ustream.h,ustream.c和ustream-fd.c。
1. 数据结构
struct ustream_buf {
struct ustream_buf *next;
char *data; /** 指向上次操作buff开始地址 */
char *tail; /** 指向未使用buff开始地址 */
char *end; /** 指向buf结束地址 */
char head[]; /** 指向buf开始地址 */
};
struct ustream_buf_list {
struct ustream_buf *head; /** 指向第1块ustream_buf */
struct ustream_buf *data_tail; /** 指向未使用的ustream_buf */
struct ustream_buf *tail; /** 指向最后的ustream_buf */
int (*alloc)(struct ustream *s, struct ustream_buf_list *l);
int data_bytes; /** 已用存储空间大小 */
int min_buffers; /** 可存储最小的ustream_buf块个数 */
int max_buffers; /** 可存储最大的ustream_buf块个数 */
int buffer_len; /** 每块ustream_buf块存储空间大小 */
int buffers; /** ustream_buf块个数 */
};
struct ustream {
struct ustream_buf_list r, w;
struct uloop_timeout state_change;
struct ustream *next;
/*
* notify_read: (optional)
* called by the ustream core to notify that new data is available
* for reading.
* must not free the ustream from this callback
*/
void (*notify_read)(struct ustream *s, int bytes_new);
/*
* notify_write: (optional)
* called by the ustream core to notify that some buffered data has
* been written to the stream.
* must not free the ustream from this callback
*/
void (*notify_write)(struct ustream *s, int bytes);
/*
* notify_state: (optional)
* called by the ustream implementation to notify that the read
* side of the stream is closed (eof is set) or there was a write
* error (write_error is set).
* will be called again after the write buffer has been emptied when
* the read side has hit EOF.
*/
void (*notify_state)(struct ustream *s);
/*
* write:
* must be defined by ustream implementation, accepts new write data.
* 'more' is used to indicate that a subsequent call will provide more
* data (useful for aggregating writes)
* returns the number of bytes accepted, or -1 if no more writes can
* be accepted (link error)
*/
int (*write)(struct ustream *s, const char *buf, int len, bool more);
/*
* free: (optional)
* defined by ustream implementation, tears down the ustream and frees data
*/
void (*free)(struct ustream *s);
/*
* set_read_blocked: (optional)
* defined by ustream implementation, called when the read_blocked flag
* changes
*/
void (*set_read_blocked)(struct ustream *s);
/*
* poll: (optional)
* defined by the upstream implementation, called to request polling for
* available data.
* returns true if data was fetched.
*/
bool (*poll)(struct ustream *s);
/*
* ustream user should set this if the input stream is expected
* to contain string data. the core will keep all data 0-terminated.
*/
bool string_data; /** 此ustream是否为字符串,true-是;false-否 */
bool write_error; /** 写出错,true-是;false-否 */
bool eof, eof_write_done;
enum read_blocked_reason read_blocked;
};
struct ustream_fd {
struct ustream stream;
struct uloop_fd fd;
};
2. 存储结构

3. 函数
初始化/销毁
/**
* ustream_fd_init: create a file descriptor ustream (uses uloop)
*/
void ustream_fd_init(struct ustream_fd *s, int fd) /**
* ustream_init_defaults: fill default callbacks and options
*/
void ustream_init_defaults(struct ustream *s) /**
* ustream_free: free all buffers and data associated with a ustream
*/
void ustream_free(struct ustream *s)
写入read buffer
/*
* ustream_reserve: allocate rx buffer space
* 分配len大小的read buffer可用内存空间,与ustream_fill_read()配合使用
*
* len: int for how much space is needed (not guaranteed to be met)
* maxlen: pointer to where the actual buffer size is going to be stored
*/
char *ustream_reserve(struct ustream *s, int len, int *maxlen) /**
* ustream_fill_read: mark rx buffer space as filled
* 设置被ustream_reseve()分配read buffer后写入的数据大小,
* 回调notify_read()接口,表示有数据可读
*/
void ustream_fill_read(struct ustream *s, int len)
读出read buffer,一般在notify_read()回调接口使用。
/*
* ustream_get_read_buf: get a pointer to the next read buffer data
* 获取新一次写入的内容,与ustream_consume()配置使用
*/
char *ustream_get_read_buf(struct ustream *s, int *buflen) /**
* ustream_consume: remove data from the head of the read buffer
*/
void ustream_consume(struct ustream *s, int len)
操作write buffer,尽最大能力调用write()回调用接口写入,如果超出能力将未写入的数据存储在write buffer中。
/*
* 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, ...)
int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
把在write buffer 中的数据写入实际地方,调用write()回调接口和notify_write()回调接口。
一般在描述符的poll操作中调用,表示当描述符变为可写时立即把上一次写入的内容进行写入操作。
/*
* ustream_write_pending: attempt to write more data from write buffers
* returns true if all write buffers have been emptied.
*/
bool ustream_write_pending(struct ustream *s)
libubox-ustream的更多相关文章
- libubox
lbubox是openwrt的一个核心库,封装了一系列基础实用功能,主要提供事件循环,二进制格式处理,linux链表实现和一些JSON辅助处理. 它的目的是以动态链接库方式来提供可重用的通用功能,给其 ...
- ubus
openwrt提供了一个系统总线ubus,类似linux桌面操作系统的d-bus,目标是提供系统级的进程间通信(IPC)功能. 为了提供各种后台进程和应用程序之间的通信机制,ubus被开发出来,由3部 ...
- openWrt libubox组件之uloop原理分析
1. libubox概述 libubox是openwrt新版本中的一个基础库,有很多应用是基于libubox开发的,如uhttpd,netifd,ubusd等. libubox主要提供以下两种功 ...
- libubox组件(3)——uloop
一:uloop概述 uloop有三个功能: 文件描述符触发事件的监控, timeout定时器处理, 当前进程的子进程的维护 二: uloop的整体框架 1: /** 2: * 初始化事件循环 3: ...
- libubox组件(1)——usock
一:相关API介绍 1.相关源码文件:usocket.h usocket.c 2.类型标志 1: #define USOCK_TCP 0 2: #define USOCK_UDP 1 3: #defi ...
- libubox组件(2)——blob/blobmsg (转载 https://segmentfault.com/a/1190000002391970)
一:blob相关接口 1.数据结构 1: struct blob_attr { 2: uint32_t id_len; /** 高1位为extend标志,高7位存储id, 3: * 低24位存储dat ...
- tr069开源协议EasyCwmp移植
1.平台MT7628 2.交叉编译器及版本信息mipsel-linux + buildroot-gcc463_32bits.tar.bz2 3.创建工作目录lancer@ubuntu:~$ mkdir ...
- libubox-runqueue
参考:libubox [4] - uloop runqueue ustream 任务队列是通过uloop定时器实现,把定时器超时时间设置为1,通过uloop事件循环来处理定时器就会处理任务队列中的ta ...
- CWMP开源代码研究2——easycwmp安装和学习
声明:本文是对开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅号:408797506) 本文所有笔记和代码可以到csdn下载:http://download.csdn.n ...
随机推荐
- Java自定义注解基础知识
注解分为三类:没有任何元素的注解,有一个元素的注解和有多个元素的注解. 1. Marker注解 这类注解没有任何元素,此类注解仅仅是一个标示.如下所示: public @interface Good ...
- 百度编辑器上传大视频报http请求错误怎么办
百度编辑器UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码,所以受到很多开放人员的青睐.但是有时 ...
- iOS 中 JSON 数据交换格式
JSON (JavaScript Object Notation)是一种轻量级的数据交换格式. JSON 的详细教程,能够參见 JSON 中国:http://www.json.org.cn/ ...
- Nginx + FastCGI 程序(C/C++)搭建高性能web service的demo
http://blog.csdn.net/chdhust/article/details/42645313 Nginx + FastCGI 程序(C/C++)搭建高性能web service的Demo ...
- powerdesigner里建物理模型图时choose DBMS为空怎么办?
RT 出现如下对话框,是因为需要“DBMS”的规则文件夹 点击下图文件图标,浏览,找到安装目录里面PowerDesigner 15\Resource Files\DBMS,就可以了. 在此记录一下,希 ...
- Node.js aitaotu图片批量下载Node.js爬虫1.00版
即使是https网页,解析的方式也不是一致的,需要多试试. 代码: //====================================================== // aitaot ...
- Lucene Spatial构建地理空间索引
一.Maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- jQuery实现滚动栏一直处于最底部
相信大家有时候在展示一些实时数据展示并且数据量非常大的时候,由于无法在同一页面看到最有效的数据,所以我们须要将滚动栏至于底部.以便我们看到最须要的数据和信息.这里非常明显的样例那拿windows的pi ...
- how to remove untagged / none images
docker rmi $(docker images -a| grep "^<none>" | awk '{print $"3"}')
- spring声明式事务以及配置
使用spring提供的事务处理机制的好处是程序员可以不用关心事务的切面了,只要配置就好了,可以少写代码. spring声明式事务处理 spring 声明:针对的是程序员,程序员告诉spring容器,哪 ...