个人做网站需要学什么只是,中国电子商务官网首页,桂平网站设计,西安建设工程信息网是谁开发的自带buffer的事件-bufferevent
bufferevent实际上也是一个event#xff0c;只不过比普通的event高级#xff0c;他的内部有两个缓冲区#xff0c;以及一个文件描述符#xff08;网络套接字#xff09;。一个网络套接字有读写两个缓冲区#xff0c;bufferevent同样也带有…自带buffer的事件-bufferevent
bufferevent实际上也是一个event只不过比普通的event高级他的内部有两个缓冲区以及一个文件描述符网络套接字。一个网络套接字有读写两个缓冲区bufferevent同样也带有两个缓冲区。所以有四个缓冲区。 bufferevent的读事件回调触发时机
当数据由内核的读缓冲区到bufferevent的读缓存区时会触发bufferevent的读事件回调需要注意的是数据由内核到bufferevent的过程不是用户程序执行的而是bufferevent内部操作的。
bufferevent的写事件回调函数触发时机
当用户程序将数据写到bufferevent的写缓冲区之后bufferevent会自动触发写事件回调函数。需要注意的是数据由bufferevent到内核的过程不是用户程序执行的而是bufferevent内部操作的。
事件回调
当bufferevent绑定的socket连接断开或者异常的时候触发事件回调。
bufferevent常用的API函数
struct bufferevent *bufferevent_socket_new(struct event_base *base, evutil_socket_t fd, int options); 函数说明*bufferevent_socket_new对已经存在的socket创建bufferevent事件
options:
BEV_OPT_CLOSE_ON_FREE --释放bufferevent自动关闭底层接口当bufferevent被释放之后文件描述符也被close;
BEV_OPT_THREADSAFE--使bufferevent能够在多线程下是安全的 /** Create a new socket bufferevent over an existing socket. param base the event base to associate with the new bufferevent. param fd the file descriptor from which data is read and written to. This file descriptor is not allowed to be a pipe(2). It is safe to set the fd to -1, so long as you later set it with bufferevent_setfd or bufferevent_socket_connect(). param options Zero or more BEV_OPT_* flags return a pointer to a newly allocated bufferevent struct, or NULL if an error occurred see bufferevent_free() */ int bufferevent_socket_connect(struct bufferevent *, struct sockaddr *, int);
函数说明调用此函数可以将bufferevent事件于通信的socket进行绑定。 /** Launch a connect() attempt with a socket-based bufferevent. When the connect succeeds, the eventcb will be invoked with BEV_EVENT_CONNECTED set. If the bufferevent does not already have a socket set, we allocate a new socket here and make it nonblocking before we begin. If no address is provided, we assume that the socket is already connecting, and configure the bufferevent so that a BEV_EVENT_CONNECTED event will be yielded when it is done connecting. param bufev an existing bufferevent allocated with bufferevent_socket_new(). param addr the address we should connect to param socklen The length of the address return 0 on success, -1 on failure. */ 调用此函数后通信的socket于bufferevent缓冲区进行绑定后面调用了bufferevent_setcb函数以后会对bufferevent缓冲区的读写操作的事件设置回调函数。
void bufferevent_free(struct bufferevent *bufev); /** Deallocate the storage associated with a bufferevent structure. param bufev the bufferevent structure to be freed. */ void bufferevent_setcb(struct bufferevent *bufev, bufferevent_data_cb readcb, bufferevent_data_cb writecb, bufferevent_event_cb eventcb, void *cbarg); /** Changes the callbacks for a bufferevent. param bufev the bufferevent object for which to change callbacks param readcb callback to invoke when there is data to be read, or NULL if no callback is desired param writecb callback to invoke when the file descriptor is ready for writing, or NULL if no callback is desired param eventcb callback to invoke when there is an event on the file descriptor param cbarg an argument that will be supplied to each of the callbacks (readcb, writecb, and errorcb) see bufferevent_new() */ 回调函数原型
typedef void (*bufferevent_data_cb)(struct bufferevent *bev, void *ctx);
typedef void (*bufferevent_event_cb)(struct bufferevent *bev, short what, void *ctx);
int bufferevent_write(struct bufferevent *bufev, const void *data, size_t size); /** Write data to a bufferevent buffer. The bufferevent_write() function can be used to write data to the file descriptor. The data is appended to the output buffer and written to the descriptor automatically as it becomes available for writing. param bufev the bufferevent to be written to param data a pointer to the data to be written param size the length of the data, in bytes return 0 if successful, or -1 if an error occurred see bufferevent_write_buffer() */ size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size); /** Read data from a bufferevent buffer. The bufferevent_read() function is used to read data from the input buffer. param bufev the bufferevent to be read from param data pointer to a buffer that will store the data param size the size of the data buffer, in bytes return the amount of data read, in bytes. */