libevent配置上下文、支持的网络模式、特征判别

环境:
windows:vs2017 比较稳定,liunx ubuntu2016.04TLS.备注在程序中标注很详细。

#include 
#include 
#include 
using namespace std;

int main()
{
#ifdef _WIN32
	//初始化socket库
	WSADATA wsa;
	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 << "event_get_supported_methods" << endl;
	for (int i = 0; methods[i] != NULL; i++)
	{
		cout << methods[i] << endl;
	}

	//设置特征(在liunx在epoll不支持文件描述符fds)
	//event_config_require_features(conf, EV_FEATURE_ET | EV_FEATURE_FDS);

	//在liunx设置了EV_FEATURE_FDS 其他特征就无法设置)在windows中EV_FEATURE_FDS 无效
	event_config_require_features(conf,EV_FEATURE_FDS);


	//根据配置项生成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
	{
		//确认特征是否生效
		//(在windows中输出,检测默认的支持类型)==》(只支持win32模式,四个类型特征都不支持)
		//(在liunx中输出,检测默认的支持类型)==》(支持epoll,poll,select模式,四个类型特征只有文件描述符不支持)
		int f = event_base_get_features(base);
		if (f & EV_FEATURE_ET)
		{
			cout << "EV_FEATURE_ET event are supported" << endl;
		}
		else
		{
			cout << "EV_FEATURE_ET event not supported" << endl;
		}
		if (f & EV_FEATURE_O1)
		{
			cout << "EV_FEATURE_O1 event are supported" << endl;
		}
		else
		{
			cout << "EV_FEATURE_O1 event not supported" << endl;
		}
		if (f & EV_FEATURE_FDS)
		{
			cout << "EV_FEATURE_FDS event are supported" << endl;
		}
		else
		{
			cout << "EV_FEATURE_FDS event not supported" << endl;
		}
		if (f & EV_FEATURE_EARLY_CLOSE)
		{
			cout << "EV_FEATURE_EARLY_CLOSE event are supported" << endl;
		}
		else
		{
			cout << "EV_FEATURE_EARLY_CLOSE event not supported" << endl;
		}

		cout << "event_base_new_with_config success !" << endl;
		event_base_free(base);
	}
   
	return 0;
}

你可能感兴趣的:(libevent配置上下文、支持的网络模式、特征判别)