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 ...
随机推荐
- Unity ZTest 深度测试 & ZWrite 深度写入
初学Shader,一开始对于渲染队列,ZTest 和 ZWrite一头雾水,经过多方查阅和实验,有了一些自己的理解.发此文与初学Shader的朋友分享,也算是为自己做个笔记.不对或不足之处欢迎指正. ...
- lnmp 安装 访问 配置 laravel
环境要求 Lnmp 一键安装包安装 php7.2+ Mysql 5.7 Innodb 开启 第一步 上传项目到 /home/wwwroot/default/ 或者composer命令行安装larave ...
- 编写Python脚本把sqlAlchemy对象转换成dict的教程
编写Python脚本把sqlAlchemy对象转换成dict的教程 在用sqlAlchemy写web应用的时候,经常会用json进行通信,跟json最接近的对象就是dict,有时候操作dict也会比操 ...
- Failure to find com.oracle:ojdbc6:jar:11.2.0.1.0
报错原因:oracle的ojdbc.jar是收费的,maven的中央仓库是没有的,需要下载到本地,然后打包进maven仓库 1.下载ojdbc6-11.2.0.1.0.jar包 http://cent ...
- 获取当前页面的title
#-*-coding:utf-8-*-from selenium import webdriverdriver = webdriver.Firefox()driver.get("https: ...
- 【HANA系列】SAP HANA SQL获取当前月的第一天
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL获取当前 ...
- python下对文件的操作(非目录)
总文件夹 子文件夹01 文档01.txt-------------------------------------------------------------------------------- ...
- xmake新增对Qt编译环境支持
在最新的xmake v2.2.1版本中,新增了对Qt SDK环境的支持,我们完全可以脱离Qt Creater进行Qt应用程序的开发,甚至配合vscode/idea等编辑器+xmake插件(xmake- ...
- java8--- Predicate 意义 代码
//为了去除 DiyInterface 这个函数式接口,可以用通用函数式接口 Predicate 替代如下: https://blog.csdn.net/u011848397/article/deta ...
- kafka生产者java客户端
producer 包含一个用于保存待发送消息的缓冲池,缓冲池中消息是还没来得及传输到kafka集群的消息. 位于底层的kafka I/O线程负责将缓冲池中的消息转换成请求发送到集群.如果在结束prod ...