libevent在windows中设置iocp和线程池


#include <event2/event.h>
#include <event2/thread.h>
#include<event2/listener.h>
#ifndef _WIN32
#include <signal.h>
#else
#endif
#include <iostream>
using namespace std;
#define SPORT 5001

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


	//创建配置上下文
	event_config* conf = event_config_new();

	//显示支持的网络模式
	const char** methods = event_get_supported_methods();
	cout << "supported_methods:" << endl;
	for (int i = 0; methods[i] != NULL; i++)
	{
		cout << methods[i] << endl;
	}

	//设置特征
	//设置了EV_FEATURE_FDS 其他特征就无法设置,在windows中EV_FEATURE_FDS无效
	//event_config_require_features(conf, EV_FEATURE_ET| EV_FEATURE_FDS);
	//event_config_require_features(conf,EV_FEATURE_FDS); //不支持epoll

	//设置网络模型,使用select
	//event_config_avoid_method(conf, "epoll");
	//event_config_avoid_method(conf, "poll");

	//windows中支持IOCP(线程池)
#ifdef _WIN32
	event_config_set_flag(conf, EVENT_BASE_FLAG_STARTUP_IOCP);
	//初始化iocp的线程
	evthread_use_windows_threads();
	//设置cpu数量
	SYSTEM_INFO si;
	GetSystemInfo(&si);
	event_config_set_num_cpus_hint(conf, si.dwNumberOfProcessors);
#endif


	//初始化配置libevent上下文
	event_base* base = event_base_new_with_config(conf);
	event_config_free(conf);


	if (!base)
	{
		cerr << "event_base_new_with_config failed!" << endl;
		base = event_base_new();
		if (!base)
		{
			cerr << "event_base_new failed!" << endl;
			return 0;
		}
	}
	else
	{
		//获取当前网络模型
		cout << "current method is " << event_base_get_method(base) << endl;


		//确认特征是否生效
		int f = event_base_get_features(base);
		if (f & EV_FEATURE_ET)
			cout << "EV_FEATURE_ET events are supported." << endl;
		else
			cout << "EV_FEATURE_ET events are not supported." << endl;
		if (f & EV_FEATURE_O1)
			cout << "EV_FEATURE_O1 events are supported." << endl;
		else
			cout << "EV_FEATURE_O1 events are not supported." << endl;
		if (f & EV_FEATURE_FDS)
			cout << "EV_FEATURE_FDS events are supported." << endl;
		else
			cout << "EV_FEATURE_FDS events are not supported." << endl;

		if (f & EV_FEATURE_EARLY_CLOSE)
			cout << "EV_FEATURE_EARLY_CLOSE events are supported." << endl;
		else
			cout << "EV_FEATURE_EARLY_CLOSE events are not supported." << endl;
		cout << "event_base_new_with_config success!" << endl;


		sockaddr_in sin;
		memset(&sin, 0, sizeof(sin));
		sin.sin_family = AF_INET;
		sin.sin_port = htons(SPORT);

		evconnlistener* ev = evconnlistener_new_bind(base, listen_cb, base, 10,
			LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE,
			(sockaddr*)&sin, sizeof(sin));
		event_base_dispatch(base);
		evconnlistener_free(ev);
		event_base_free(base);
	}

	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值