1. 程式人生 > 其它 >Linux libevent設定網路模型 配置特徵

Linux libevent設定網路模型 配置特徵

技術標籤:libevent高併發linux


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <strings.h>
#include <sys/wait.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <event2/event_struct.h>
#include <fcntl.h> #include <event.h> #include <event2/listener.h> #include <event2/bufferevent_struct.h> #include <event2/event-config.h> #include <event2/event.h> int main(int argc,char *argv[]) { int i=0; int feature; struct event_config *evconfig=
event_config_new(); //顯示支援的網路模式 const char **methods=event_get_supported_methods(); for(i=0;methods[i]!=NULL;i++) { printf("%s\n",methods[i]); } //設定當前網路模型 // if(event_config_avoid_method(evconfig,"epoll")==0)printf("avoid epoll"); // if(event_config_avoid_method(evconfig,"poll")==0)printf("avoid poll");
struct event_base *base=event_base_new_with_config(evconfig); if(!base) { perror("event base error"); exit(1); } // 獲取當前網路模型 printf("current net methods:%s\n", event_base_get_method(base)); // 設定特徵 EV_FEATURE_ET // event_config_require_features(evconfig,EV_FEATURE_FDS); 不支援epoll // 確認特徵是否生效 feature=event_base_get_features(base); if(feature&EV_FEATURE_ET) // 邊沿觸發,高效但是容易丟訊息,注意與水平觸發區分 { printf("EV_FEATURE_ET support\n"); }else printf("EV_FEATURE_ET not support\n"); // 要求具有很多事件的後臺方法可以以近似O(1)處理事件;select和poll // 無法提供這種特徵,它們只能提供近似O(N)的操作 if(feature&EV_FEATURE_O1) { printf("EV_FEATURE_O1 support\n"); }else printf("EV_FEATURE_O1 not support\n"); // 後臺方法可以處理包括sockets在內的各種檔案描述符 if(feature&EV_FEATURE_FDS) { printf("EV_FEATURE_FDS support\n"); }else printf("EV_FEATURE_FDS not support\n"); // 要求後臺方法可以使用EV_CLOSED檢測連結關閉,而不需要讀完所有未決資料才能判斷 // 支援EV_CLOSED的後臺方法不是所有OS核心都支援的 if(feature&EV_FEATURE_EARLY_CLOSE) { printf("EV_FEATURE_EARLY_CLOSE support\n"); }else printf("EV_FEATURE_EARLY_CLOSE not support\n"); event_config_free(evconfig); event_base_free(base); }

相關內容精華帖