網站首頁 編程語言 正文
Poller是抽象類,Eventloop通過抽象類Poller,引用不同的派生類對象(PollPoller或EpollPoller),調用同名覆蓋方法,就可以很方便地去擴展不同的I/O復用
Poller.h
源碼
#include <map> #include <vector> #include "muduo/base/Timestamp.h" #include "muduo/net/EventLoop.h" namespace muduo { namespace net { class Channel; class Poller : noncopyable { public: typedef std::vector<Channel*> ChannelList; Poller(EventLoop* loop); virtual ~Poller(); virtual Timestamp poll(int timeoutMs, ChannelList* activeChannels) = 0; virtual void updateChannel(Channel* channel) = 0; virtual void removeChannel(Channel* channel) = 0; virtual bool hasChannel(Channel* channel) const; static Poller* newDefaultPoller(EventLoop* loop); void assertInLoopThread() const { ownerLoop_->assertInLoopThread(); } protected: typedef std::map<int, Channel*> ChannelMap; ChannelMap channels_; private: EventLoop* ownerLoop_; }; } // namespace net } // namespace muduo
可以看到,Poller里有很多純虛函數,是抽象類
為什么muduo庫要抽象一層Poller呢?
因為在eventloop里面,在使用I/O復用的時候,并沒有直接指定epoll,因為muduo庫對外提供兩種I/O復用方法poll和epoll,在eventloop里面,沒有直接使用poll或者epoll,而是從抽象層面通過抽象類Poller,引用不同的派生類對象,調用同名覆蓋方法,就可以很方便地去擴展不同的I/O復用
Poller抽象基類有兩個成員變量:
protected: typedef std::map<int, Channel*> ChannelMap; ChannelMap channels_; private: EventLoop* ownerLoop_; // 表示Poller所屬的事件循環EventLoop
Poller監聽的就是Eventloop另外一個成員ChannelList保存的那些channel,所以Poller里面會有一個ChannelMap,key是sockfd,value是sockfd所屬的Channel
protected的成員變量就是讓派生類可以訪問到,private的成員變量派生類不能訪問到
Poller.h
#pragma once #include "noncopyable.h" #include "Timestamp.h" #include <vector> #include <unordered_map> class Channel; //只用到指針類型,如果需要用到實例就要包含相應頭文件,類型聲明是沒用的 class EventLoop; // muduo庫中多路事件分發器的核心IO復用模塊 class Poller : noncopyable{ public: using ChannelList = std::vector<Channel*>; Poller(EventLoop *loop); virtual ~Poller(); // 給所有IO復用保留統一的接口 epoll_wait virtual Timestamp poll(int timeoutMs, ChannelList* activeChannels) = 0; // 更新感興趣的事件 epoll_ctl EPOLL_CTL_ADD EPOLL_CTL_MOD virtual void updateChannel(Channel* channel) = 0; // eventloop中刪除channel epoll_ctl EPOLL_CTL_DEL virtual void removeChannel(Channel* channel) = 0; // 判斷Poller里是否包含某個channel bool hasChannel(Channel* channel) const; // EventLoop可以通過newDefaultPoller獲取一個Poller實例 static Poller* newDefaultPoller(EventLoop* loop); protected: // key:sockfd,value:sockfd所屬的Channel using ChannelMap = std::unordered_map<int, Channel*>; ChannelMap channels_; private: EventLoop* ownerLoop_; // Poller所屬的事件循環 };
Poller.cc
#include "Poller.h" #include "Channel.h" Poller::Poller(EventLoop *loop) : ownerLoop_(loop) {} Poller::~Poller() = default; bool Poller::hasChannel(Channel* channel) const{ auto iter = channels_.find(channel->fd()); return iter != channels_.end() && iter->second == channel; // 找到fd且channel相等 }
為什么不把newDefaultPoller的實現放在Poller.cc
里
如果真的把newDefaultPoller寫在Poller.cc里面,從語法上來說,沒有錯誤。但是這個函數是要生成一個具體的I/O復用對象,并返回一個基類的指針
所以基類就得include這兩個包含了派生類聲明的頭文件,才能去生成一個具體的實例對象并返回回去,這樣不合理。
在繼承結構中,Poller是基類,只能派生類引用基類,而Poller.cc基類不能引用派生類,這就是好的OOP設計
muduo用一個單獨的源文件DefaultPoller.cc
實現newDefaultPoller
#include "muduo/net/Poller.h" #include "muduo/net/poller/PollPoller.h" #include "muduo/net/poller/EPollPoller.h" #include <stdlib.h> using namespace muduo::net; Poller* Poller::newDefaultPoller(EventLoop* loop) { if (::getenv("MUDUO_USE_POLL")) { return new PollPoller(loop); // 環境變量中有MUDUO_USE_POLL,則返回PollPoller實例 } else { return new EPollPoller(loop); // 默認返回EPollPoller實例 } }
重寫DefaultPoller.cc
#include <stdlib.h> #include "Poller.h" #include "EPollPoller.h" Poller* Poller::newDefaultPoller(EventLoop* loop){ if(std::getenv("MUDUO_USE_POLL")){ // new poll的實例 return nullptr; }else{ return new EPollPoller(loop); } }
原文鏈接:https://blog.csdn.net/qq_42500831/article/details/124293598
相關推薦
- 2021-10-04 Flutter輸入框TextField屬性及監聽事件介紹_Android
- 2022-05-13 python魔法方法之__setattr__()_python
- 2022-02-07 SSH連服務器提示“Permission denied,please try again”的原因與解
- 2023-01-09 基于Go語言實現插入排序算法及優化_Golang
- 2022-10-26 jQuery?表單事件與遍歷詳情_jquery
- 2024-07-15 GIT同步修改后的遠程分支
- 2022-05-25 Entity?Framework?Core對Web項目生成數據庫表_實用技巧
- 2023-05-22 Redis數據結構類型示例解析_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支