企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ### API调用过程 ***** **libevent总体API调用过程如下:** (1)初始化一个event_base, event_base_new() ``` struct event_base * event_init(void) /*初始化libevent库, 返回一个struct event_base 指针*/ struct event_base * event_base_new(void) /*同样是初始化libevent库,官方后出的用来替代evnet_init()的*/ //区别: event_base_new是线程安全的、而后者是非线程安全的, //event_init在其官方说明中已经被标志为过时的函数、且建议用event_base_new代替 ``` (2)初始化一个event(事件)结构体 event_set(): ``` /* ev: 执行要初始化的 event 对象; fd: 该 event 绑定的“句柄”,对于信号事件,它就是关注的信号; event:在该 fd 上关注的事件类型,它可以是 EV_READ, EV_WRITE, EV_SIGNAL; cb: 这是一个函数指针,当 fd 上的事件 event 发生时,调用该函数执行处理,它有三个参数, 调用时由 event_base 负责传入,按顺序,实际上就是 event_set 时的 fd, event 和 arg; arg: 传递给 cb 函数指针的参数*/ void event_set(struct event *ev, int fd, short event, void (*cb)(int, short, void *), void *arg) /*初始化事件,并且设置回调函数和关注的事件*/ ``` (3)event_base_set()  //给事件设置相应的event\_base ``` //指明 event 要注册到哪个 event_base 实例上 event_base_set(base, &ev); ``` (4)event_add()  //把事件添加到队列(就绪队列、active队列等),队列由event对应的event_base管理 ``` //添加事件并设置超时时间 event_add(&ev, timeout); ``` (5)event_base_dispatch()  //启动轮询 ``` event_base_dispatch(base); ``` ### 代码示例 ***** server.cpp ``` #include <WinSock2.h> #include <windows.h> #include <memory> #include <stdio.h> #include<event2/event.h> #include <event2/bufferevent.h> #pragma comment(lib,"ws2_32.lib") #define MAX_MSG_LEN 4096 int tcp_init(unsigned short port, int listen_num) { #ifdef WIN32 WSADATA ws_data; if (WSAStartup(MAKEWORD(2, 2), &ws_data) < 0) goto errorn; #endif int errno_save; evutil_socket_t listener; listener = ::socket(AF_INET, SOCK_STREAM, 0); if (-1 == listener) return -1; evutil_make_listen_socket_reuseable(listener);//允许多次绑定同一个地址,要用在socket和bind之间 struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_addr.S_un.S_addr = 0; sin.sin_port = htons(port); if (::bind(listener, (sockaddr*)& sin, sizeof(sin)) < 0) goto errorn; if (::listen(listener, listen_num) < 0) goto errorn; evutil_make_socket_nonblocking(listener);//跨平台统一接口,将套接字设置位非阻塞状态 return listener; errorn: errno_save = errno; evutil_closesocket(listener); errno = errno_save; return -1; } void socket_read_cb(bufferevent *bev, void *arg) { char msg[MAX_MSG_LEN + 1]; memset(msg, 0, sizeof(msg)); size_t len = bufferevent_read(bev, msg, sizeof(msg)); msg[len] = '\0'; printf("Server read the data: %s\n",msg); char reply[] = "I have read your data"; bufferevent_write(bev, reply, strlen(reply)); } void errorcb(bufferevent* bev, short events, void* arg) { if (events & BEV_EVENT_EOF) { printf("connection closed!\n"); } else if (events & BEV_EVENT_ERROR) { printf("some other error!\n"); } bufferevent_free(bev); printf("bufferevent_free bev"); } void accept_cb(evutil_socket_t fd, short events, void* arg) { evutil_socket_t sockfd; struct sockaddr_in client; int len = sizeof(client); sockfd = ::accept(fd, (struct sockaddr*)& client, &len); if (sockfd < 0) { printf("accept error!\n"); return; } evutil_make_socket_nonblocking(sockfd);//平台统一接口,设置非阻塞 printf("accept a client %d\n", sockfd); struct event_base* base = (event_base*)arg; bufferevent* bev = bufferevent_socket_new(base, sockfd, BEV_OPT_CLOSE_ON_FREE); bufferevent_setcb(bev, socket_read_cb, NULL, errorcb, NULL); bufferevent_enable(bev, EV_READ | EV_PERSIST); } int main() { setvbuf(stdout, NULL, _IONBF, 0); int listener = tcp_init(662, 5); struct event_base* base = event_base_new(); struct event* listener_event = event_new(base, listener, EV_READ | EV_PERSIST, accept_cb, (void*)base); event_add(listener_event, NULL); printf("start!\n"); event_base_dispatch(base); event_free(listener_event); event_base_free(base); return 1; } ```