epoll c++封装
#ifndef _BFC_EPOLL_FLOW_HPP_
#define _BFC_EPOLL_FLOW_HPP_
#include <string.h>
#include <errno.h>
#include <sys/epoll.h>
#include <assert.h>
#include <string>
#include <stdexcept>
#include <iostream>
/*
EPOLLIN :表示对应的文件描述符可以读
EPOLLOUT:表示对应的文件描述符可以写
EPOLLPRI:表示对应的文件描述符有紧急的数据可读
EPOLLERR:表示对应的文件描述符发生错误
EPOLLHUP:表示对应的文件描述符被挂断
EPOLLET: 表示对应的文件描述符有事件发生
*/
class CEPollFlow
{
public:
CEPollFlow();
~CEPollFlow();
int Create(int iMaxFD);
//int Wait(int iTimeMs);
int Wait(int iTimeMs);
int GetEvents(long long &llKey, unsigned int &iEvent);
int Add(int iFd,long long llKey,int iFlag);
int Modify(int iFd,long long llKey,int iFlag);
int Del(int iFd);
private:
int Ctl(int iFd,long long llKey,int iEpollAction, int iFlag);
int m_iEpollFD;
epoll_event* m_pEpollEvents;
int m_iMaxFD;
int m_iEventNum;
int m_iCurrEvtIdx;
};
#endif // _BFC_EPOLL_FLOW_HPP_
#include "bfc_epoll_flow.h"
CEPollFlow::CEPollFlow()
{
m_iEpollFD = -1;
m_pEpollEvents = NULL;
}
CEPollFlow::~CEPollFlow()
{
if(m_pEpollEvents)
{
delete [] m_pEpollEvents;
}
if(m_iEpollFD >= 0)
{
close(m_iEpollFD);
}
}
int CEPollFlow::Create(int iMaxFD)
{
m_iMaxFD = iMaxFD;
m_iEpollFD = epoll_create(iMaxFD);
if(m_iEpollFD < 0)
{
return -1;
}
m_pEpollEvents = new epoll_event[iMaxFD];
return 0;
}
int CEPollFlow::Wait(int iTimeMs)
{
m_iEventNum = epoll_wait(m_iEpollFD, m_pEpollEvents, m_iMaxFD, iTimeMs);
m_iCurrEvtIdx = 0;
return m_iEventNum;
}
int CEPollFlow::Add(int iFd, long long llKey, int iFlag)
{
return Ctl(iFd,llKey,EPOLL_CTL_ADD,iFlag);
}
int CEPollFlow::Del(int iFd)
{
return Ctl(iFd, 0, EPOLL_CTL_DEL, 0);
}
int CEPollFlow::Modify(int iFd, long long llKey, int iFlag)
{
return Ctl(iFd, llKey, EPOLL_CTL_MOD, iFlag);
}
int CEPollFlow::GetEvents(long long &llKey, unsigned int &iEvent)
{
if(m_iCurrEvtIdx >= m_iEventNum)
{
return 0;
}
epoll_event* curr_event = &m_pEpollEvents[m_iCurrEvtIdx++];
llKey = curr_event->data.u64;
iEvent = curr_event->events;
return 1;
}
int CEPollFlow::Ctl(int iFd,long long llKey,int iEpollAction, int iFlag)
{
epoll_event evt;
evt.events = iFlag;
evt.data.u64 = llKey;
int ret = epoll_ctl(m_iEpollFD, iEpollAction, iFd, &evt);
if(ret < 0)
{
return -1;
}
return 0;
}
epoll c++封装的更多相关文章
- libevent源码阅读笔记(一):libevent对epoll的封装
title: libevent源码阅读笔记(一):libevent对epoll的封装 最近开始阅读网络库libevent的源码,阅读源码之前,大致看了张亮写的几篇博文(libevent源码深度剖析 h ...
- muduo定时器、多线程模型及epoll的封装
timerfd是Linux为用户程序提供的一个定时器接口,这个接口基于文件描述符. clock_gettime函数可以获取系统时钟,精确到纳秒.需要在编译时指定库:-lrt.可以获取两种类型时间: C ...
- 基于epoll的聊天室程序
epoll相对于poll和select这两个多路复用的I/O模型更加的高效.epoll的函数很简单,麻烦的地方在于水平触发和边沿触发. 用张图来说明下 ET(边沿)只是在状态反转时触发,比如从不可读到 ...
- 04: 事件驱动、五种I/O操作、I/O多路复用select和epoll
网络编程其他篇 目录: 1.1 事件驱动 1.2 五种I/O操作 1.3 I/O 多路复用之select.poll.epoll详解 1.1 事件驱动返回顶部 1.什么是事件驱动 定义:就是根据不同事 ...
- c++ 网络编程(八) LINUX-epoll/windows-IOCP下 socket opoll函数用法 优于select方法的epoll 以及windows下IOCP 解决多进程服务端创建进程资源浪费问题
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9622548.html 锲子:关于并发服务器中的I/O复用实现方式,前面在网络编程系列四还是五来 ...
- 第二章epoll
epoll_create:函数实现分析 /* * Open an eventpoll file descriptor. */ SYSCALL_DEFINE1(epoll_create1, int, f ...
- epoll源码解析翻译------说使用了mmap的都是骗子
本文地址 //https://www.cnblogs.com/l2017/p/10830391.html //https://blog.csdn.net/li_haoren select poll e ...
- Tornado框架简析
Tornado是一款轻量级的Web服务器,同时又是一个开发框架.采用单线程非阻塞I/O模型(epoll),主要是为了应对高并发 访问量而被开发出来,尤其适用于comet应用. Tornado服务器3大 ...
- 【转】libevent源码分析
libevent源码分析 转自:http://www.cnblogs.com/hustcat/archive/2010/08/31/1814022.html 这两天没事,看了一下Memcached和l ...
随机推荐
- vue 父组件通过props向子组件传递数据/方法的方式
参考网址:https://segmentfault.com/a/1190000010507616 下面栗子中, callback是传递父组件的方法, mutationName是传递父组件的数据, Ap ...
- hive top n
hive 中窗口函数row_number,rank,dense_ran,ntile分析函数的用法 hive中一般取top n时,row_number(),rank,dense_ran()这三个函数就派 ...
- textarea输入框限制字数
<textarea onkeyup="checkLen(this)"></textarea> <div>您还可以输入 <span id=& ...
- 【基础】selenium中元素定位的常用方法(三)
一.Selenium中元素定位共有八种 id name className tagName linkText partialLinkText xpath cssSelector 其中前六种都比较简单, ...
- javaweb环境搭建
首先,在开始搭建MyEclipse的开发环境之前,还有三步工具的安装需要完成,只要在安装配置成功之后才可以进入下面的java Web项目开发环境的搭建. 1.安装工具 第一步,下载并安装JDK,到官网 ...
- day30 操作系统介绍 进程的创建
今日内容 一.操作系统的简单介绍 二,并发与并行 三.同步异步阻塞非阻塞 四.multiprocess模块 1. 操作系统的简单介绍 多道技术(重点) 空间复用: 时间复用: 进程之间是空间隔离的 分 ...
- Linux系统分区方案(CentOs 6)
装Linux如何分区: 方案1:(监控服务器,负载均衡器) 1./boot 引导分区,存放引导文件和Linux内核. 启动文件:用于判断你需要启动哪个操作系统或启动哪个内核. ...
- winserver 搭建 Citrix License 许可服务器
1. 申请许可证 Citrix XenApp_XenDesktop7.6和XenServer 6.5申请许可证的步骤是一致的,由于之前我已经申请过XenApp_XenDesktop的许可证,本次以X ...
- 玩转X-CTR100 | STM32F4 l X-Assistant串口助手控制功能
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] X-CTR100控制器配套的X-Assis ...
- DevExpress WinForms使用教程:Data Grid - Find Panel模式
[DevExpress WinForms v18.2下载] DevExpress WinForms用户都熟知,Data Grid是整个产品线的主要产品.在v18.2中添加了一些新的功能,例如之前教程中 ...