|
|
#include <iostream>
|
|
#include <event2/event.h>
|
|
#include <event2/thread.h>
|
|
#include <event2/listener.h>
|
|
#include <event2/bufferevent.h>
|
|
#include <event2/buffer.h>
|
|
#include <string.h>
|
|
#ifdef _WIN32
|
|
|
|
#else
|
|
#include <signal.h>
|
|
#endif // !_WIN32
|
|
|
|
using namespace std;
|
|
|
|
|
|
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();
|
|
|
|
//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
|
|
{
|
|
void Server(event_base* base);
|
|
Server(base);
|
|
void Client(event_base* base);
|
|
Client(base);
|
|
|
|
|
|
event_base_dispatch(base);
|
|
event_base_free(base);
|
|
event_config_free(conf);
|
|
}
|
|
return 0;
|
|
}
|