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.

93 lines
1.9 KiB

3 years ago
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. #ifndef _WIN32
  5. #include <signal.h>
  6. #endif // !_WIN32
  7. using namespace std;
  8. int main()
  9. {
  10. #ifdef _WIN32
  11. //初始化socket库
  12. WSADATA wsa;
  13. WSAStartup(MAKEWORD(2, 2), &wsa);
  14. #else
  15. if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { //忽略管道信号,发送数据给已关闭的socket,会飞掉!
  16. return 1;
  17. }
  18. #endif
  19. //初始化libevent上下文
  20. event_config *conf = event_config_new();
  21. //显示支持的网络模式
  22. const char** methods = event_get_supported_methods();
  23. cout << "supported_methods" << endl;
  24. for (int i = 0; methods[i] != NULL; i++) {
  25. cout << methods[i] << endl;
  26. }
  27. //设置特征
  28. //event_config_require_features(conf, EV_FEATURE_FDS);
  29. //初始化配置上下文
  30. event_base* base = event_base_new_with_config(conf);
  31. if (!base) {
  32. cout << "event_base_new_with_config failed!" << endl;
  33. base = event_base_new();
  34. if (!base) {
  35. cerr << "event_base_new failed" << endl;
  36. return 0;
  37. }
  38. }
  39. else {
  40. //获取当前网络模型
  41. cout << "current mothed is " << event_base_get_method(base) << endl;
  42. //确认特征是否生效
  43. int f = event_base_get_features(base);
  44. if (f& EV_FEATURE_ET) {
  45. cout << "EV_FEATURE_ET events are supported" << endl;
  46. }
  47. else {
  48. cout << "EV_FEATURE_ET events are not supported" << endl;
  49. }
  50. if (f& EV_FEATURE_O1) {
  51. cout << "EV_FEATURE_O1 events are supported" << endl;
  52. }
  53. else {
  54. cout << "EV_FEATURE_O1 events are not supported" << endl;
  55. }
  56. if (f& EV_FEATURE_FDS) {
  57. cout << "EV_FEATURE_FDS events are supported" << endl;
  58. }
  59. else {
  60. cout << "EV_FEATURE_FDS events are not supported" << endl;
  61. }
  62. if (f& EV_FEATURE_EARLY_CLOSE) {
  63. cout << "EV_FEATURE_EARLY_CLOSE events are supported" << endl;
  64. }
  65. else {
  66. cout << "EV_FEATURE_EARLY_CLOSE events are not supported" << endl;
  67. }
  68. cout << "event_base_new_with_config success!" << endl;
  69. event_config_free(conf);
  70. }
  71. return 0;
  72. }