libevent备忘

本文详细介绍了Libevent库中evbuffer模块的使用方法,包括如何将数据从一个evbuffer移动到另一个evbuffer,如何在evbuffer的前端添加数据,以及如何通过bufferevent_write接口进行数据写入。此外,还提到了evbuffer_add_buffer和evbuffer_remove_buffer函数的具体用法和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

英文部分引用:http://www.wangafu.net/~nickm/libevent-book/Ref7_evbuffer.html

Moving data from one evbuffer to anotherFor efficiency, Libevent has optimized functions for moving data from one evbuffer to another.

Interface
int evbuffer_add_buffer(struct evbuffer *dst, struct evbuffer *src);
int evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
    size_t datlen);

The evbuffer_add_buffer() function moves all data from src to the end of dst. It returns 0 on success, -1 on failure.
The evbuffer_remove_buffer() function moves exactly datlen bytes from src to the end of dst, copying as little as possible. If there are fewer than datlen bytes to move, it moves all the bytes. It returns the number of bytes moved.

We introduced evbuffer_add_buffer() in Libevent 0.8; evbuffer_remove_buffer() was new in Libevent 2.0.1-alpha.

Adding data to the front of an evbufferInterface
int evbuffer_prepend(struct evbuffer *buf, constvoid *data, size_t size);
int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
These functions behave as evbuffer_add() and evbuffer_add_buffer() respectively, except that they move data to the front of the destination buffer.

These functions should be used with caution, and never on an evbuffer shared with a bufferevent. They were new in Libevent 2.0.1-alpha.

(使用这组函数需谨慎,并且永远不要在和bufferevent关联的evbuffer上使用)


bufferevent_write
bufferevent_write_buffer 只是对evbuffer的一个包装
如果上述两个函数不能满足需求,那么取bufferevent_get_output, bufferevent_get_input可以取出关联的evbuffer,然后可以evbuffer调用相关的方法

 

转载于:https://my.oschina.net/dourgulf/blog/54425

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值