创建event_base
1、创建默认的event_base, 从全局变量eventops中选出操作系统支持的最快方法 。它直接调用event_base_new_with_config。
struct event_base *event_init(void);
struct event_base *event_base_new(void);
void event_base_free(struct event_base *);
2、创建复杂event_base
2.1 分配一个 event_config。
struct event_config * pConfig = event_config_new();
void event_config_free(struct event_config *cfg);
2.2 创建base时,选择支持指定的I/O复用机制。
int event_config_require_features(struct event_config *cfg, int feature);
enum event_base_config_flag {
EVENT_BASE_FLAG_NOLOCK = 0x01,
EVENT_BASE_FLAG_IGNORE_ENV = 0x02,
EVENT_BASE_FLAG_STARTUP_IOCP = 0x04,
EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08,
EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10,
EVENT_BASE_FLAG_PRECISE_TIMER = 0x20
};
2.3 使用 event_config_set_flag 函数设置支持指定特性的I/O复用机制。
int event_config_set_flag(struct event_config *cfg, int flag);
enum event_base_config_flag {
EVENT_BASE_FLAG_NOLOCK = 0x01,
EVENT_BASE_FLAG_IGNORE_ENV = 0x02,
EVENT_BASE_FLAG_STARTUP_IOCP = 0x04,
EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08,
EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10,
EVENT_BASE_FLAG_PRECISE_TIMER = 0x20
};
2.4 通过 event_config_avoid_method 函数使用字符串的方式指定base不使用指定的I/O复用机制
int event_config_avoid_method(struct event_config *cfg, const char *method);
event_config_avoid_method(cfg, "select");
2.5 通过 event_get_supported_methods 函数获取所有的支持的I/O复用机制
const char **event_get_supported_methods(void);
const char *event_base_get_method(const struct event_base *);
2.6 通过 event_config 初始化一个 event_base
struct event_base *event_base_new_with_config(const struct event_config *);
2.7 使用 event_base_get_features 获取event_base的 event_config的配置,用于检验配置是否生效
int event_base_get_features(const struct event_base *base);
2.8 使用 event_base_priority_init 设置 event_base 优先级别
int event_base_priority_init(struct event_base *base, int n_priorities);
#define EVENT_MAX_PRIORITIES 256
2.9 使用 event_reinit 重新初始化 event_base
int event_reinit(struct event_base *base);
3.0 其他函数使用
evthread_use_windows_threads();
SYSTEM_INFO info;
GetSystemInfo(&info);
event_config_set_num_cpus_hint(pConfig, info.dwNumberOfProcessors/*进程数量*/);
一、遍历系统支持那些网络模型
#include <iostream>
#include <event2/event.h>
#include <event2/listener.h>
#ifndef _WIN32
#include <signal.h>
#include <string.h>
#endif
using namespace std;
#define SPORT 5001
int main()
{
#ifdef _WIN32
WSADATA was;
WSAStartup(MAKEWORD(2, 2), &was);
#else
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
return 1;
#endif
event_config * pConfig = event_config_new();
const char ** methods = event_get_supported_methods();
for (int i = 0; methods[i] != NULL; i++)
{
std::cout << i << " " << methods[i] << std::endl;
}
event_config_require_features(pConfig, EV_FEATURE_ET);
event_base *pBase = event_base_new_with_config(pConfig);
event_config_free(pConfig);
if (!pBase)
{
pBase = event_base_new();
if (!pBase)
return 0;
}
else
{
int f = event_base_get_features(pBase);
if (f&EV_FEATURE_ET)
cout << "EV_FEATURE_ET events are supported." << endl;
else
cout << "EV_FEATURE_ET events are not supported." << endl;
if (f&EV_FEATURE_O1)
cout << "EV_FEATURE_O1 events are supported." << endl;
else
cout << "EV_FEATURE_O1 events are not supported." << endl;
if (f&EV_FEATURE_FDS)
cout << "EV_FEATURE_FDS events are supported." << endl;
else
cout << "EV_FEATURE_FDS events are not supported." << endl;
if (f&EV_FEATURE_EARLY_CLOSE)
cout << "EV_FEATURE_EARLY_CLOSE events are supported." << endl;
else
cout << "EV_FEATURE_EARLY_CLOSE events are not supported." << endl;
cout << "event_base_new_with_config success!" << endl;
event_base_free(pBase);
}
event_base_free(pBase);
#ifdef _WIN32
WSACleanup();
#endif
return 0;
}
二、支持windows的iocp模型示例
void listen_cb(struct evconnlistener * e, evutil_socket_t s,
struct sockaddr * a, int socklen, void *arg)
{
std::cout << "listen_cb" << std::endl;
}
int main()
{
#ifdef _WIN32
WSADATA was;
WSAStartup(MAKEWORD(2, 2), &was);
#else
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
return 1;
#endif
event_config * pConfig = event_config_new();
#ifdef _WIN32
event_config_set_flag(pConfig, EVENT_BASE_FLAG_STARTUP_IOCP);
evthread_use_windows_threads();
SYSTEM_INFO info;
GetSystemInfo(&info);
event_config_set_num_cpus_hint(pConfig, info.dwNumberOfProcessors);
#endif // _WIN32
event_base *base = event_base_new_with_config(pConfig);
event_config_free(pConfig);
if (!base)
{
base = event_base_new();
if (!base)
return 0;
}
else
{
sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SPORT);
evconnlistener * ev = evconnlistener_new_bind(base, listen_cb, base, 10,
LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE,
(sockaddr*)&sin, sizeof(sin));
event_base_dispatch(base);
evconnlistener_free(ev);
event_base_free(base);
}
#ifdef _WIN32
WSACleanup();
#endif //
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步