handy源码阅读(五):PollerBase类
使用poll内核函数等待事件发生:
struct PollerBase: private noncopyable {
int64_t id_;
int lastActive_;
PollerBase(): lastActive_(-) {
static std::atomic<int64_t> id();
id_ = ++id;
}
virtual void addChannel(Channel* ch) = ;
virtual void removeChannel(Channel* ch) = ;
virtual void updateChannel(Channel* ch) = ;
virtual ~PollerBase() {};
};
struct PollerEpoll : public PollerBase {
int fd_;
std::set<Channel*> liveChannels_;
struct epoll_event activeEvs_[kMaxEvents];
PollerEpoll();
~PollerEpoll();
void addChannel(Channel* ch) override;
void removeChannel(Channel* ch) override;
void updateChannel(Channel* ch) override;
void loop_once(int waitMs) override;
};
pollerEpoll的实现主要是使用了epoll类函数。
PollerBase* createPoller() {
return new PollerEpoll();
}
PollerEpoll::PollerEpoll() {
fd_ = epoll_create1(EPOLL_CLOEXEC); //创建一个epoll文件描述符
}
PollerEpoll::~PollerEpoll() {
while (liveChannels_.size()) {
(*liveChannels_.begin())->close(); //从poller中移除该通道,关闭通道上的文件描述符并处理该通道上的读事件。
}
::close(fd_); //关闭epoll文件描述符
}
void PollerEpoll::addChannel(Channel* ch) {
struct epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.events = ch->events();
ev.data.ptr = ch;
int r = epoll_ctl(fd_, EPOLL_CTL_ADD, ch->fd(), &ev); //将通道内的文件描述符和其关心的事件添加到epoll监听中。
liveChannels_.insert(ch);
}
void PollerEpoll::updateChannel(Channel* ch) {
struct epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.events = ch->events();
ev.data.ptr = ch;
int r = epoll_ctl(fd_, EPOLL_CTL_MOD, ch->fd(), &ev);
}
void PollerEpoll::removeChannel(Channel* ch) {
liveChannels_.erase(ch);
for (int i = lastActive_; i >= 0; i--) {
if (ch == activeEvs_[i].data.ptr) {
activeEvs_[i].data.ptr = NULL;
break;
}
}
}
//核心函数
void PollerEpoll::loop_once(int waitMs) {
lastActive_ = epoll_wait(fd_, activeEvs_, kMaxEvents, waitMs);
while (--lastActive_ >= 0) {
int i = lastActive_;
Channel* ch = (Channel*)activeEvs_[i].data.ptr;
int events = activeEvs_[i].events;
if (ch) {
if (events & kWriteEvent) {
ch->handleWrite();
}
if (events & (kReadEvent | POLLERR)) {
ch->handleRead();
}
if (!(events & (kReadEvent | kWriteEvent | POLLERR))) {
fatal("unexpected poller events");
}
}
}
}
handy源码阅读(五):PollerBase类的更多相关文章
- handy源码阅读(四):Channel类
通道,封装了可以进行epoll的一个fd. struct Channel: private noncopyable { Channel(EventBase* base, int fd, int eve ...
- handy源码阅读(二):EventsImp类
EventsImp用于完成事件的处理. class EventsImp { EventBase* base_; PollerBase* poller_; std::atomic<bool> ...
- handy源码阅读(六):tcp类
首先是tcpconn和tcpserver类: struct TcpConn : public std::enable_shared_from_this<TcpConn>, private ...
- handy源码阅读(六):udp类
分为UdpServer类和UdpConn类. struct UdpServer : public std::enable_shared_from_this<UdpServer>, priv ...
- handy源码阅读(三):SafeQueue类
SafeQueue类继承与信号量mutex(用于加锁),nonocopyable 定义如下: template <typename T> struct SafeQueue : privat ...
- handy源码阅读(一):EventBase类
类EventBase继承于类EventBases,继承于noncopyable. 其中noncopyable是一个去除了拷贝构造和赋值构造的类. noncopyable: class noncopy ...
- JDK源码阅读:Object类阅读笔记
Object 1. @HotSpotIntrinsicCandidate @HotSpotIntrinsicCandidate public final native Class<?> g ...
- Spark数据传输及ShuffleClient(源码阅读五)
我们都知道Spark的每个task运行在不同的服务器节点上,map输出的结果直接存储到map任务所在服务器的存储体系中,reduce任务有可能不在同一台机器上运行,所以需要远程将多个map任务的中间结 ...
- JDK源码阅读(五)java.io.Serializable接口
package java.io; public interface Serializable { } (1)实现Serializable接口的类,将会被提示提供一个 serialVersionUID ...
随机推荐
- 《Using Databases with Python》Week3 Data Models and Relational SQL 课堂笔记
Coursera课程<Using Databases with Python> 密歇根大学 Week3 Data Models and Relational SQL 15.4 Design ...
- 2018.03.27 pandas concat 和 combin_first使用
# 连接和修补concat.combine_first 沿轴的堆叠连接 # 连接concatimport pandas as pdimport numpy as np s1 = pd.Series([ ...
- 【HANA系列】SAP HANA SQL获取当前日期最后一天
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL获取当前 ...
- 【MM系列】SAP MM模块-关于批次特性的查看和获取
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-关于批次特性的查看 ...
- Java与C#不同
1.C#方法定义可以有默认参数,而Java则不支持该方式. C#方法定义 public void ShowMessage(string text,string orderId="" ...
- java日常统计
姓名:Danny 日期:2017/11/27 任务 日期 听课 编程程序 阅读课本 准备考试 日统计 周一 30 120 150 周二 50 140 190 周三 周四 周五 周六 周 ...
- Intersection of Two Arrays(交集)
来源:https://leetcode.com/problems/intersection-of-two-arrays Given two arrays, write a function to co ...
- JAVA基础--JAVA 集合框架(泛型、file类)
一.集合总结 集合:Collection体系.Map体系. Collection体系:单列集合的共性操作规则. List:列表,可以重复,有下标,拥有特有的迭代器ListIterator. Array ...
- C++基础-多态
本文为 C++ 学习笔记,参考<Sams Teach Yourself C++ in One Hour a Day>第 8 版.<C++ Primer>第 5 版.<代码 ...
- idea配置less自动编译
参考: idea配置less自动编译 1. 电脑安装node.js环境: window下直接上官网下载node.msi文件下载安装即可 安装完成后在命令行执行如下命令表明安装成功 npm -v nod ...