muduo网络库学习之EventLoop(一):事件循环类图简介和muduo 定时器TimeQueue


1、EventLoop、Channel、Poller 等类图如下:

黑色菱形:组合;白色菱形:聚合;白色三角形:继承;实线:关联;

Channel是selectable IO channel,负责注册与响应IO 事件,它不拥有file descriptor。
Channel是Acceptor、Connector、EventLoop、TimerQueue、TcpConnection的成员。


一个EventLoop对象对应一个Poller成员对象,boost::scoped_ptr<Poller> poller_;
 //Poller是个抽象类,具体可以是EPollPoller(默认) 或者PollPoller

Poller类里面有三个纯虚函数,需要子类实现:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
 
/// Polls the I/O events.
/// Must be called in the loop thread.
virtual Timestamp poll( int timeoutMs, ChannelList *activeChannels) =  0;

/// Changes the interested I/O events.
/// Must be called in the loop thread.
virtual  void updateChannel(Channel *channel) =  0;

/// Remove the channel, when it destructs.
/// Must be called in the loop thread.
virtual  void removeChannel(Channel *channel) =  0;

对于PollPoller来说,一个fd对应一个struct pollfd(pollfd.fd),一个fd 对应一个channel*;这个fd 可以是socket, eventfd, timerfd, signalfd; 如下:
 C++ Code 
1
2
3
4
 
typedef std::vector< struct pollfd> PollFdList;
typedef std::map< int, Channel *> ChannelMap;     // key是文件描述符,value是Channel*
PollFdList pollfds_;
ChannelMap channels_;

对于EPollPoller 来说,一个channel* 对应一个fd, 一个channel* 对应一个struct epoll_event(epoll_event.data.ptr)
 C++ Code 
1
2
3
4
 
typedef std::vector< struct epoll_event> EventList;
typedef std::map< int, Channel *> ChannelMap;
EventList events_;
ChannelMap channels_;

一个线程最多只能有一个EventLoop对象,这种线程被称为IO线程。一个EventLoop对象对应多个Channel对象,但只有wakeupChannel_生存期由EventLoop控制 ,  timerfdChannel_生存期由TimeQueue管理。
(boost::scoped_ptr<Channel> wakeupChannel_; // 纳入poller_来管理     int wakeupFd_;   // eventfd函数创建 )

其余以Channel* 方式管理,如下:
 C++ Code 
1
2
 
typedef std::vector<Channel *> ChannelList;
ChannelList activeChannels_;         // Poller返回的活动通道

下面是Channel 类简化:
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值