Linux libevent设置网络模型 配置特征

8 篇文章 0 订阅
2 篇文章 0 订阅

#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);
    
}

相关内容精华帖

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值