libevent常用函数实例

#include <iostream>
#include <string.h>
#include <event2/event.h>
#include <event2/listener.h>
#include <event2/thread.h>

#ifndef __WIN32

#include <signal.h>

#endif
using namespace std;

#define EVENT_PORT  5001

//回调函数
void listen_cb(struct evconnlistener *, evutil_socket_t, struct sockaddr *addr, int socklen, void *arg)
{
    cout<<"listen_cb"<<endl;
}
int main()
{
#ifdef _WIN32
    //初始化socket库
    WSADATA wsa;
    WSAStartup(MAKEWORD(2,2),&wsa);
#else
    //忽略管道信号,发送数据给已关闭的socket
    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
        return 1;
#endif


    int i = 0;
    //初始化event配置上下文
    event_config* pevconf =  event_config_new();
    
    //windos中支持iocp
#ifdef __WIN32
    event_config_set_flag(pevconf, EVENT_BASE_FLAG_STARTUP_IOCP);
    //初始化iocp线程
    event_use_windows_threads();
    //设置CPU数量
    SYSTEMP_INFO si;
    GetSystemInfo(&si);
    event_config_set_num_cpus_hint(pevconf, si.dwNumberOfProcessors);
#endif
    //设置网络模型为select
    event_config_avoid_method(pevconf, "epoll"); //避免使用epoll、poll模型
    event_config_avoid_method(pevconf, "poll");
        
    //查询提供的网络模型
    const char** methods = event_get_supported_methods();
    cout<<"supported methods:"<<endl;

    for (i = 0; methods[i] != NULL; i++)
    {
        cout<<methods[i]<<endl; //优先级epoll>poll>select
    }
    
    //初始化event_base上下文
    event_base *pevbase = event_base_new_with_config(pevconf);
    event_config_free(pevconf);
    if (!pevbase)
    {
        cerr<<"event_base_new_with_config error"<<endl;
        //event_base_new_with_config初始化失败则使用event_base_new进行初始化
        pevbase = event_base_new();
        if (!pevbase)
        {
            cerr<<"event_base_new error"<<endl;
            return -1;
        }
    }
    if (pevbase)
    {
          //socket、bind、listen
        sockaddr_in sn;
        memset(&sn, 0, sizeof(sn));
        sn.sin_family = AF_INET;
        sn.sin_port = htons(EVENT_PORT);
        
        evconnlistener_new_bind(pevbase, listen_cb, pevbase, 10, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, (struct sockaddr*)&sn, sizeof(sn));
        
        //分发处理
        event_base_dispatch(pevbase);
        //获取当前网络模型,默认位epoll
        cout<<"current method:"<<event_base_get_method(pevbase)<<endl;
        //获取所支持的特征
        int feature = event_base_get_features(pevbase);
        if (feature&EV_FEATURE_ET)
        {
            cout<<"EV_FEATURE_ET are supported"<<endl;
        }
        else
        {
            cout<<"EV_FEATURE_ET are not supported"<<endl;

        }
        if (feature&EV_FEATURE_O1)
        {
            cout<<"EV_FEATURE_O1 are supported"<<endl;
        }
        else
        {
            cout<<"EV_FEATURE_O1 are not supported"<<endl;

        }
        if (feature&EV_FEATURE_FDS)
        {
            cout<<"EV_FEATURE_FDS are supported"<<endl;
        }
        else
        {
            cout<<"EV_FEATURE_FDS are not supported"<<endl;

        }
        if (feature&EV_FEATURE_EARLY_CLOSE)
        {
            cout<<"EV_FEATURE_EARLY_CLOSE are supported"<<endl;
        }
        else
        {
            cout<<"EV_FEATURE_EARLY_CLOSE are not supported"<<endl;

        }
        
        
    }
    event_base_free(pevbase);
#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值