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.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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. int f = event_base_get_features(base);
  42. if (f& EV_FEATURE_ET) {
  43. cout << "EV_FEATURE_ET events are supported" << endl;
  44. }
  45. else {
  46. cout << "EV_FEATURE_ET events are not supported" << endl;
  47. }
  48. if (f& EV_FEATURE_O1) {
  49. cout << "EV_FEATURE_O1 events are supported" << endl;
  50. }
  51. else {
  52. cout << "EV_FEATURE_O1 events are not supported" << endl;
  53. }
  54. if (f& EV_FEATURE_FDS) {
  55. cout << "EV_FEATURE_FDS events are supported" << endl;
  56. }
  57. else {
  58. cout << "EV_FEATURE_FDS events are not supported" << endl;
  59. }
  60. if (f& EV_FEATURE_EARLY_CLOSE) {
  61. cout << "EV_FEATURE_EARLY_CLOSE events are supported" << endl;
  62. }
  63. else {
  64. cout << "EV_FEATURE_EARLY_CLOSE events are not supported" << endl;
  65. }
  66. cout << "event_base_new_with_config success!" << endl;
  67. event_config_free(conf);
  68. }
  69. return 0;
  70. }