07 Bufferevent related functions under libevent library

07 Bufferevent related functions under libevent library

以下是关于libevent学习的相关文章:
01 Download and installation of the libevent library and test whether the installation is successful
02 The overall framework idea of ​​the libevent library
03 The main function of
communication
under the
libevent Area features introduction
07libevent library related functions of the bufferevent event
08libevent library communication server and client main functions
09libevent library server and client TCP communication process and code examples

The header file is:

#include <event2/bufferevent.h>

1 Creation of bufferevent event
Note that bufferevent is also an event, you can also understand the following as a socket for creating bufferevent.

struct bufferevent * bufferevent_socket_new(
                     struct event_base *base,
                     evutil_socket_t fd,   //传入参数,绑定套接字在bufferevent内部
                     enum bufferevent_options options
);
// 参数options: 只需要记住BEV_OPT_CLOSE_ON_FREE 即可。代表 释放 bufferevent 时关闭底层传输端口
// 成功时返回bufferevent,失败则返回NULL

2 Destroy the bufferevent event

void bufferevent_free(struct bufferevent *bev);

3 Set the callback function of the bufferevent, which includes the callbacks for read and write and exception events.
As we have made it clear in the previous article, writing a callback function has no real effect.

		void bufferevent_setcb(
				struct bufferevent *bufev,
				bufferevent_data_cb readcb,//使用 bufferevent_read()读取buff中数据信息,自己封装
				bufferevent_data_cb writecb,//写回调只是提示你发生出去数据,没有实质作用,一般用于打印信息或者直接置为NULL				      
				bufferevent_event_cb eventcb, 		//异常回调		
				void *cbarg			//三个回调函数的参数
		);


//读写回调函数 
		typedef void (*bufferevent_data_cb)(
				struct bufferevent *bev, 
				void *ctx		//相当于cbarg	
		);
 
 //异常回调函数
		typedef void (*bufferevent_event_cb)(
				struct bufferevent *bev,
				short events, 
				void *ctx		//相当于cbarg
		);
//events参数:
//			EV_EVENT_READING: 读操作时发生某事件,具体是哪种事件请看其他标志
//			BEV_EVENT_WRITING:写操作时发生某事件,具体是哪种事件请看其他标志
//			BEV_EVENT_ERROR:  操作时发生错误
//			BEV_EVENT_TIMEOUT:发生超时
//			BEV_EVENT_EOF:    遇到文件结束指示。
//			BEV_EVENT_CONNECTED:请求的连接过程已经完成实现客户端的时候可以判断
 
//EVUTIL_SOCKET_ERROR():关于错误的更多信息

4 Set the bufferevent buffer to the activated state or the disabled state or obtain its state.
We know that the bufferevent buffer is divided into read and write, but the read buffer in the newly created bufferevent buffer is disabled by default, and the write buffer is enabled by endable. You can use the get function to get the bufferevent buffer startup disabled status with &.

//开启,可以调用该bufferevent事件回调
		void bufferevent_enable(
			struct bufferevent *bufev, 
			short events
		); 
 
//禁用, 对应的回调就不会被调用
		void bufferevent_disable(
			struct bufferevent *bufev, 
			short events
		); 
 //获取缓冲区的状态
		short bufferevent_get_enabled(
			struct bufferevent *bufev
		);

5 The two functions for reading and writing the bufferevent buffer are generally called in the read callback function

//读数据函数,注意:bufferevent的读写缓冲区是由队列实现的,所以它相当于pop移除数据
	  size_t bufferevent_read(
				struct bufferevent *bufev, 
				void *data, 
				size_t size
       );

//写数据函数
	    int bufferevent_write(
				struct bufferevent *bufev,
				const void *data, 
				size_t size
		);

At this point, we have learned the main related functions of the bufferevent event. In order to adapt to everyone's laziness, we try to shorten every article as much as possible.

Guess you like

Origin blog.csdn.net/weixin_44517656/article/details/108763067