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 ...
随机推荐
- Docker 容器和镜像使用
Docker 容器使用: docker run -d -P training/webapp python app.py -d:让容器在后台运行. -P:将容器内部使用的网络端口映射到我们使用的主机上. ...
- 通过BeanPostProcessor理解Spring中Bean的生命周期
通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...
- python dpkt解析ssl流
用法:python extract_tls_flow.py -vr white_pcap/11/2018-01-10_13-05-09_2.pcap -o pcap_ssl_flow.txt & ...
- npm run dev 报错 版本太低
解决方案是: 先用命令: npm -v 查看下你的版本(我原来是 V3.1 不行) 然后用 cnpm install -g npm 更新版本 npm - v 变成最新的4.0.4 npm run d ...
- 【Query】使用java对mysql数据库进行查询操作
操作步骤: 1.加载数据库驱动(先在工程里加载数据库对应的驱动包) 2.获取连接 3.根据连接建立一个可执行sql的对象 4.执行sql语句 5.关闭连接 代码: package database; ...
- bzoj1294
题解: 首先发现假如一个豆豆被多边形围住了,那么从这个豆豆引出一条射线 会有奇数个焦点 然后我们从每个豆豆引出一条射线 然后状压dfs 代码: #include<bits/stdc++.h> ...
- day06 小数据池,再谈编码
今日所学 一. 小数据池 二. is 和==的区别 三. 编码的问题 一.小数据池的作用 用来缓存数据 可以作用的数据类型: 整数(int), 字符串(str), 布尔值(bool). 什么是块 ...
- JS时间戳和时间之间转换
一.时间转换时间戳 var date = new Date(); //时间对象 var str = date.getTime(); //转换成时间戳 二.时间戳转换为时间 1.转换成形如 2018 ...
- java中的方法method
java中的方法必须存在于类class里,不能独立存在.类是描述具有某种特征的事物,方法则是这类 事物具有的某种功能,通过调用方法可以实现某种特定的功能.方法名一般以小写的动词开头. 例: publi ...
- [Leetcode 100]判断二叉树相同 Same Tree
[题目] 判断二叉树是否相同. [思路] check函数. p==null并且q==null,返回true;(两边完全匹配) p==null或q==null,返回false;(p.q其中一方更短) p ...