栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

libevent (二)初始化配置libevent上下文

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

libevent (二)初始化配置libevent上下文

文章目录
      • libevent 初始化配置(网络模式与默认特征)
      • 在windows下使用IOCP(默认开启线程)

libevent 初始化配置:获取系统所支持的网络模型

libevent 初始化配置(网络模式与默认特征)
#include 
#include 
#ifndef _WIN32
#include 
#endif
using namespace std;



int main(int argc, char** argv) {
#if _WIN32
	//windowns 初始化socket库
	WSADATA wsa;
	WSAStartup(MAKEWORD(2, 2), &wsa);
#else
	//linux 忽略管道信号,发送数据给已关闭的socket
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
		return 1;
#endif

	//初始化配置libevent上下文

	//创建配置上下文
	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:" << methods[i] << endl;
	}
	
	event_base* base = event_base_new_with_config(conf);
	if (!base) {
		std::cerr << "event_base_new_with_config failed!" << std::endl;
		return -1;
	}
	std::cout << "event_base_new_with_config successfully!" << std::endl;

	cout << "=====================Default Feature=====================" << endl;
	//确认特征是否生效
	int feature = event_base_get_features(base);
	if (feature&EV_FEATURE_ET){
		cout << "EV_FEATURE_ET supported" << endl;
	}
	else
	{
		cout << "EV_FEATURE_ET not supported" << endl;
	}
	if (feature & EV_FEATURE_O1) {
		cout << "EV_FEATURE_O1 supported" << endl;
	}
	else
	{
		cout << "EV_FEATURE_O1 not supported" << endl;
	}
	if (feature & EV_FEATURE_FDS) {
		cout << "EV_FEATURE_FDS supported" << endl;
	}
	else
	{
		cout << "EV_FEATURE_FDS not supported" << endl;
	}
	if (feature & EV_FEATURE_EARLY_CLOSE) {
		cout << "EV_FEATURE_EARLY_CLOSE supported" << endl;
	}
	else
	{
		cout << "EV_FEATURE_EARLY_CLOSE not supported" << endl;
	}

	event_base_free(base);
	event_config_free(conf);

	return 0;
}

win10平台

linux平台

在windows下使用IOCP(默认开启线程)
#include 
#include 
#include 
#include 
#ifndef _WIN32
#include 
#endif
#define SPORT 5001

using namespace std;



void listen_cb(evconnlistener* ev, evutil_socket_t t, struct sockaddr* addr, int socklen, void *arg){
	cout << "listen_cb" << endl;
}



int main(int argc, char** argv) {
#if _WIN32
	//windowns 初始化socket库
	WSADATA wsa;
	WSAStartup(MAKEWORD(2, 2), &wsa);
#else
	//linux 忽略管道信号,发送数据给已关闭的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:" << methods[i] << endl;
	}
	
	//设置特征(设置了EV_FEATURE_FDS,其它特征就无法设置,windows中无效)
	// event_config_require_features(conf, EV_FEATURE_FDS); //不支持epoll

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


	//windows支持IOCP(默认开始线程池)
#if _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);
	if (!base) {
		std::cerr << "event_base_new_with_config failed!" << std::endl;
		base = event_base_new();
		if (!base){
			cout << "event_base_new failed!" << endl;
			return -1;
		}
	}
	std::cout << "event_base_new_with_config successfully!" << std::endl;

	//获取当前网络模型
	cout << "current method is: " << event_base_get_method(base) << endl;


	cout << "=====================Default Feature=====================" << endl;
	//确认特征是否生效
	int feature = event_base_get_features(base);
	if (feature&EV_FEATURE_ET){
		cout << "EV_FEATURE_ET supported" << endl;
	}
	else
	{
		cout << "EV_FEATURE_ET not supported" << endl;
	}
	if (feature & EV_FEATURE_O1) {
		cout << "EV_FEATURE_O1 supported" << endl;
	}
	else
	{
		cout << "EV_FEATURE_O1 not supported" << endl;
	}
	if (feature & EV_FEATURE_FDS) {
		cout << "EV_FEATURE_FDS supported" << endl;
	}
	else
	{
		cout << "EV_FEATURE_FDS not supported" << endl;
	}
	if (feature & EV_FEATURE_EARLY_CLOSE) {
		cout << "EV_FEATURE_EARLY_CLOSE supported" << endl;
	}
	else
	{
		cout << "EV_FEATURE_EARLY_CLOSE not supported" << 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);
	event_config_free(conf);

	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/338436.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号