|
|
#include <iostream>
|
|
#include<event2/event.h>
|
|
#include<event2/thread.h>
|
|
#include<event2/listener.h>
|
|
#include <string.h>
|
|
#ifdef _WIN32
|
|
|
|
#else
|
|
#include <signal.h>
|
|
#endif // !_WIN32
|
|
|
|
using namespace std;
|
|
|
|
#define SPORT 5001
|
|
|
|
void listen_cb(struct evconnlistener *, evutil_socket_t, struct sockaddr *, int socklen, void *arg) {
|
|
std::cout << "listen to be" << std::endl;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
#ifdef _WIN32
|
|
//初始化socket库
|
|
WSADATA wsa;
|
|
WSAStartup(MAKEWORD(2, 2), &wsa);
|
|
#else
|
|
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { //忽略管道信号,发送数据给已关闭的socket,会飞掉!
|
|
return 1;
|
|
}
|
|
#endif
|
|
|
|
//初始化libevent上下文
|
|
event_config *conf = event_config_new();
|
|
|
|
//打印支持的网络模式
|
|
const char** methods = event_get_supported_methods();
|
|
cout << "supported_methods" << endl;
|
|
for (int i = 0; methods[i] != NULL; i++) {
|
|
cout << methods[i] << endl;
|
|
}
|
|
|
|
//设置特征
|
|
//设置了EV_FEATURE_FDS 其它特征就无法设置,在windows中EV_FEATURE_FDS无效
|
|
//event_config_require_features(conf, EV_FEATURE_FDS); //不支持epoll
|
|
|
|
//设置网络模型使用select
|
|
//event_config_avoid_method(conf, "epoll"); //linux 默认顺序为 epoll -> poll -> select
|
|
//event_config_avoid_method(conf, "poll");
|
|
|
|
|
|
//windows中支持IOCP(线程池) 要查看该项是否开启成功,可以在运行程序后打开任务管理器查看线程数量
|
|
#ifdef _WIN32
|
|
event_config_set_flag(conf, EVENT_BASE_FLAG_STARTUP_IOCP);
|
|
evthread_use_windows_threads();
|
|
//设置cpu数量
|
|
SYSTEM_INFO si;
|
|
GetSystemInfo(&si);
|
|
event_config_set_num_cpus_hint(conf, si.dwNumberOfProcessors);
|
|
#endif
|
|
|
|
|
|
//初始化配置上下文
|
|
event_base* base = event_base_new_with_config(conf);
|
|
|
|
|
|
if (!base) {
|
|
cout << "event_base_new_with_config failed!" << endl;
|
|
base = event_base_new();
|
|
if (!base) {
|
|
cerr << "event_base_new failed" << endl;
|
|
return 0;
|
|
}
|
|
}
|
|
else {
|
|
//获取当前网络模型
|
|
cout << "current mothed is " << event_base_get_method(base) << endl;
|
|
|
|
|
|
//确认特征是否生效
|
|
int f = event_base_get_features(base);
|
|
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;
|
|
|
|
|
|
sockaddr_in sin;
|
|
memset(&sin, 0, sizeof(sin));
|
|
sin.sin_family = AF_INET;
|
|
sin.sin_port = htons(SPORT);
|
|
|
|
evconnlistener * ev = evconnlistener_new_bind(base, //libevent上下文
|
|
listen_cb, //接收到连接的回调函数
|
|
base, //回调函数获取的参数(根据业务来)
|
|
LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, //地址重用,evconnlistener关闭同时关闭socket
|
|
10, //连接队列大小,对应listen函数参数
|
|
(sockaddr*)&sin,//绑定的地址和端口
|
|
sizeof(sin)
|
|
);
|
|
event_base_dispatch(base);
|
|
event_base_free(base);
|
|
evconnlistener_free(ev);
|
|
|
|
event_config_free(conf);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|