Epoll的使用例子
本篇文章在上一篇的基础上,使用 epoll 实现了一个事件监听和回调处理的模块。如何编写一个使用该模块的例子呢? 监测什么类型的fd,监测什么类型的事件,监测到事件以后需要做什么?后来可以看看如何将该模块与socket , 回调函数, 线程池联系起来。
#include<sys/epoll.h> // epoll_create, epoll_ctl, epoll_wait
#include <mutex> // std::mutex
#include <functional> // std::function
#include <iostream>
#include <memory> // std::unique_ptr
#include <unistd.h> // close class Epoll{
public:
class ActiveEvents {
public:
ActiveEvents( int num, const struct epoll_event* events):
num_( num ),
events_( events )
{
}
int num() const { return num_; }
const struct epoll_event* events() const { return events_; } private:
int num_;
const struct epoll_event* events_;
}; Epoll();
~Epoll(); int AddMonitorReadableEvent( int fd );
int AddMonitorWritableEvent( int fd );
int DeleteMonitoringEvent( int fd );
int ModifyMonitorEvent( int fd , int status ); void StartPolling();
void HandleEvents( int num, const struct epoll_event* events );
void Stop(); using EpollAwakeCallBack = std::function< void(const ActiveEvents*) > ;
void SetAwakeCallBack( EpollAwakeCallBack* cb ); // 设置事件处理回调函数 private :
int Add_Event( int fd, int event ); // 封装 epoll_ctl, 使函数语义看起来更明确
int Delete_Event( int fd, int event );
int Modify_Event( int fd, int event ); int epollfd_;
std::unique_ptr< EpollAwakeCallBack > awake_cb_ ;
static const int fd_size_; // 最多能处理的fd个数
std::mutex awake_cb_mutex_; // 由于使用std::unique_ptr保存回调函数指针,设置回调函数时需要加锁
std::mutex mutex_;
}; const int Epoll::fd_size_ = 100 ; Epoll::Epoll() {
epollfd_ = epoll_create(fd_size_);
std::cout << "epollfd = " << epollfd_ << std::endl;
} Epoll::~Epoll() {
std::cout << "deleting epoll" << std::endl;
close(epollfd_);
} void Epoll::StartPolling() {
const int EPOLLEVENTS = 100;
struct epoll_event events[EPOLLEVENTS];
while (1) {
auto ret = epoll_wait(epollfd_, events, EPOLLEVENTS, -1);
std::cout << "awakening " << ret << std::endl;
HandleEvents(ret, events);
}
} void Epoll::HandleEvents(int num, const struct epoll_event* events) {
ActiveEvents active_events(num, events);
std::unique_lock<std::mutex> lock(awake_cb_mutex_);
if (awake_cb_) {
(*awake_cb_)(&active_events);
}
} void Epoll::SetAwakeCallBack(EpollAwakeCallBack* cb) {
std::unique_lock<std::mutex> lock(awake_cb_mutex_);
awake_cb_.reset(cb);
} int Epoll::AddMonitorReadableEvent(int fd) {
// TODO: Are epoll_wait and epoll_ctl thread-safe?
std::unique_lock<std::mutex> lock(mutex_);
return Add_Event(fd, EPOLLIN | EPOLLONESHOT);
} int Epoll::AddMonitorWritableEvent(int fd) {
std::unique_lock<std::mutex> lock(mutex_);
return Add_Event(fd, EPOLLOUT | EPOLLONESHOT);
} int Epoll::DeleteMonitoringEvent(int fd) {
std::unique_lock<std::mutex> lock(mutex_);
return Delete_Event(fd, EPOLLIN | EPOLLONESHOT);
} int Epoll::ModifyMonitorEvent(int fd, int status) {
std::unique_lock<std::mutex> lock(mutex_);
return Modify_Event(fd, status);
} int Epoll::Add_Event(int fd, int event) {
struct epoll_event ev;
ev.events = event;
ev.data.fd = fd;
int ret = epoll_ctl(epollfd_, EPOLL_CTL_ADD, fd, &ev);
return ret;
} int Epoll::Delete_Event(int fd, int event) {
struct epoll_event ev;
ev.events = event;
ev.data.fd = fd;
int ret = epoll_ctl(epollfd_, EPOLL_CTL_DEL, fd, &ev);
return ret;
} int Epoll::Modify_Event(int fd, int event) {
struct epoll_event ev;
ev.events = event;
ev.data.fd = fd;
int ret = epoll_ctl(epollfd_, EPOLL_CTL_MOD, fd, &ev);
return ret;
} int main(){
Epoll a;
return 0;
}
Epoll的使用例子的更多相关文章
- 一个epoll的简单例子
epoll事件机制的触发方式有两种:LT(电平触发)和ET(边沿触发) EPOLLIN事件: 内核中的socket接收缓冲区 为空(低电平) 内核中的socket接受缓冲区 不为空(高电平) EPOL ...
- Epoll简介以及例子
第一部分:Epoll简介 问题 : Select,Poll和Epoll的区别 答案 : Epoll和Select的区别 1. 遍历方式的区别.select判断是否有事件发生是遍历的,而epoll是事 ...
- epoll 系列函数简介、与select、poll 的区别
一.epoll 系列函数简介 #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags) ...
- epoll学习
一.epoll_create #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags) ...
- Redis事件管理(一)
Redis统一的时间管理器,同时管理文件事件和定时器, 这个管理器的定义: #if defined(__APPLE__) #define HAVE_TASKINFO 1 #endif /* Test ...
- Redis事件管理(三)
Redis的事件管理和定时器的管理都是自己来实现的,Redis的事件管理分为两部分,一部分是封装了系统的异步事件API,还有一部分是在这基础上封装了一个通用的事件管理器,根据具体的系统来决定具体使用哪 ...
- epoll 简单介绍及例子
第一部分:Epoll简介 . 当select()返回时,timeout参数的状态在不同的系统中是未定义的,因此每次调用select()之前必须重新初始化timeout和文件描述符set.实际上,秒,然 ...
- (转)浅析epoll – epoll例子以及分析
原文地址:http://www.cppfans.org/1419.html 浅析epoll – epoll例子以及分析 上篇我们讲到epoll的函数和性能.这一篇用用这些个函数,给出一个最简单的epo ...
- epoll完整例子
#include <deque> #include <map> #include <vector> #include <pthread.h> #incl ...
- epoll+socket的简单测试例子
server: #include <sys/socket.h> #include <sys/epoll.h> #include <netinet/in.h> #in ...
随机推荐
- 软件开发流程-路飞项目需求- pip永久换源-虚拟环境-路飞项目前后端创建-包导入-后端项目调整目录
目录 软件开发流程-路飞项目需求- pip永久换源-虚拟环境-路飞项目前后端创建-包导入-后端项目调整目录 今日内容概要 今日内容详细 1 软件开发流程 2 路飞项目需求 3 pip永久换源 4 虚拟 ...
- 《__cplusplus修饰符的作用:C和CPP接口互相调用时候,编译没问题,链接提示未定义问题》
关于__cplusplus修饰符说明如下: __cplusplus是cpp中的自定义宏,那么定义了这个宏的话表示这是一段cpp的代码,也就是说,上面的代码的含义是:如果这是一段cpp的代码,那么加入e ...
- 6.mysql优化案例
1.单表优化: 进行优化: 删除原来的三个字段的索引,创建二个字段的索引: 2.两表关联: 左连接,在右表创建索引 右连接,在左表创建索引: 3.三表关联:左关联全都在右表创建索引:
- sass和less以及stylus的区别
一.区别: 需要配置不同:Sass需要Ruby环境:Less需要引入less.js:Stylus需要安装node 使用语法不用:Sass变量使用$ ;Less变量使用@:stylus变量使用 属性名 ...
- spark闭包检查
spark在执行算子时,如果算子内部用到了外部(Driver)端的对象或变量,就一定会出现闭包:spark在执行算子之前会进行闭包检查,也就是对外部对象或变量进行序列化检查:
- Webrtc audio
整体理解 在 WebRTC 中,Call 是peer connection 的. 为 WebRTC Call 注入的 AudioState 来自于全局的 MediaEngine 的 VoiceEngi ...
- dbeaver把表数据导出csv时字符串自动加双引号问题解决
背景: mysql 5.7 dbeaver 21.1.4 解决:如下图,括起字符这里设置一个 空格(space)即可: 参考1
- (转载)Overlay网络
1. Overlay 网络1.1 Overlay 技术概述 Overlay 在网络技术领域,指的是一种网络架构上叠加的虚拟化技术模式,其大体框架是对基础网络不进行大规模修改的条件下,实现应用在网 ...
- C CODE STYLE 每天瞅一两眼
阅读进度:只读到了swich 转载于:https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html C Coding Standard C Co ...
- WPF窗体图标设置
WPF程序在编写结束后,界面没有图标,影响美观性.今天在添加图标的时候,一直报错,然后通过查找资料,将这个问题解决,现在将主要步骤记录如下; 准备好图标文件,注意文件格式应该为ico格式文件.有一个在 ...