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.

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