You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.3 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. 
  2. #include <iostream>
  3. #include<event2/event.h>
  4. #include<event2/thread.h>
  5. #include<event2/listener.h>
  6. #ifdef _WIN32
  7. #else
  8. #include <signal.h>
  9. #endif // !_WIN32
  10. using namespace std;
  11. #define SPORT 5001
  12. void listen_cb(struct evconnlistener *, evutil_socket_t, struct sockaddr *, int socklen, void *arg) {
  13. std::cout << "listen to be" << std::endl;
  14. }
  15. int main()
  16. {
  17. #ifdef _WIN32
  18. //初始化socket库
  19. WSADATA wsa;
  20. WSAStartup(MAKEWORD(2, 2), &wsa);
  21. #else
  22. if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { //忽略管道信号,发送数据给已关闭的socket,会飞掉!
  23. return 1;
  24. }
  25. #endif
  26. //初始化libevent上下文
  27. event_config *conf = event_config_new();
  28. //打印支持的网络模式
  29. const char** methods = event_get_supported_methods();
  30. cout << "supported_methods" << endl;
  31. for (int i = 0; methods[i] != NULL; i++) {
  32. cout << methods[i] << endl;
  33. }
  34. //设置特征
  35. //设置了EV_FEATURE_FDS 其它特征就无法设置,在windows中EV_FEATURE_FDS无效
  36. //event_config_require_features(conf, EV_FEATURE_FDS); //不支持epoll
  37. //设置网络模型使用select
  38. //event_config_avoid_method(conf, "epoll"); //linux 默认顺序为 epoll -> poll -> select
  39. //event_config_avoid_method(conf, "poll");
  40. //windows中支持IOCP(线程池) 要查看该项是否开启成功,可以在运行程序后打开任务管理器查看线程数量
  41. #ifdef _WIN32
  42. event_config_set_flag(conf, EVENT_BASE_FLAG_STARTUP_IOCP);
  43. evthread_use_windows_threads();
  44. //设置cpu数量
  45. SYSTEM_INFO si;
  46. GetSystemInfo(&si);
  47. event_config_set_num_cpus_hint(conf, si.dwNumberOfProcessors);
  48. #endif
  49. //初始化配置上下文
  50. event_base* base = event_base_new_with_config(conf);
  51. if (!base) {
  52. cout << "event_base_new_with_config failed!" << endl;
  53. base = event_base_new();
  54. if (!base) {
  55. cerr << "event_base_new failed" << endl;
  56. return 0;
  57. }
  58. }
  59. else {
  60. //获取当前网络模型
  61. cout << "current mothed is " << event_base_get_method(base) << endl;
  62. //确认特征是否生效
  63. int f = event_base_get_features(base);
  64. if (f& EV_FEATURE_ET) {
  65. cout << "EV_FEATURE_ET events are supported" << endl;
  66. }
  67. else {
  68. cout << "EV_FEATURE_ET events are not supported" << endl;
  69. }
  70. if (f& EV_FEATURE_O1) {
  71. cout << "EV_FEATURE_O1 events are supported" << endl;
  72. }
  73. else {
  74. cout << "EV_FEATURE_O1 events are not supported" << endl;
  75. }
  76. if (f& EV_FEATURE_FDS) {
  77. cout << "EV_FEATURE_FDS events are supported" << endl;
  78. }
  79. else {
  80. cout << "EV_FEATURE_FDS events are not supported" << endl;
  81. }
  82. if (f& EV_FEATURE_EARLY_CLOSE) {
  83. cout << "EV_FEATURE_EARLY_CLOSE events are supported" << endl;
  84. }
  85. else {
  86. cout << "EV_FEATURE_EARLY_CLOSE events are not supported" << endl;
  87. }
  88. cout << "event_base_new_with_config success!" << endl;
  89. sockaddr_in sin;
  90. memset(&sin, 0, sizeof(sin));
  91. sin.sin_family = AF_INET;
  92. sin.sin_port = htons(SPORT);
  93. evconnlistener * ev = evconnlistener_new_bind(base, //libevent上下文
  94. listen_cb, //接收到连接的回调函数
  95. base, //回调函数获取的参数(根据业务来)
  96. LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, //地址重用,evconnlistener关闭同时关闭socket
  97. 10, //连接队列大小,对应listen函数参数
  98. (sockaddr*)&sin,//绑定的地址和端口
  99. sizeof(sin)
  100. );
  101. event_base_dispatch(base);
  102. event_base_free(base);
  103. evconnlistener_free(ev);
  104. event_config_free(conf);
  105. }
  106. return 0;
  107. }