muduo中的抽象类Poller

muduo中的抽象类Poller,第1张

Poller是抽象类,Eventloop通过抽象类Poller,引用不同的派生类对象(PollPoller或EpollPoller),调用同名覆盖方法,就可以很方便地去扩展不同的I/O复用

Poller.h源码

#include 
#include 

#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 
#include 

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 

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 

#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://outofmemory.cn/langs/717193.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存