Channel的update()会调用EventLoop的updateChannel(),会调用Poller的updateChannel()
一个EventLoop包含多个Channel
Acceptor是被动连接的抽象
Connector是主动连接的抽象
1.总览
上图是涉及到的一些类,及其之间的关系:
- 一个线程拥有一个EventLoop,代表事件循环
- 一个EventLoop包含多个Channel,Channel负责注册和响应I/O事件
- 一个EventLoop包含一个Poller,Poller的实现分为两种:
- POLLPOLLER:对 poll 的封装
- EPOLLPOLLER:对 epoll 的封装
2. 时序图
我们根据上述流程看看各个方法的实现。
简化版 EventLoop
EventLoop.h
#ifndef MUDUO_NET_EVENTLOOP_H
#define MUDUO_NET_EVENTLOOP_H
#include <vector>
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
#include <muduo/base/CurrentThread.h>
#include <muduo/base/Thread.h>
#include <muduo/base/Timestamp.h>
namespace muduo
{
namespace net
{
class Channel;
class Poller;
///
/// Reactor, at most one per thread.
///
/// This is an interface class, so don't expose too much details.
class EventLoop : boost::noncopyable
{
public:
EventLoop();
~EventLoop(); // force out-line dtor, for scoped_ptr members.
///
/// Loops forever.
///
/// Must be called in the same thread as creation of the object.
///
void loop();
void quit();
///
/// Time when poll returns, usually means data arrivial.
///
Timestamp pollReturnTime() const {
return pollReturnTime_; }
// internal usage
void updateChannel(Channel* channel); // 在Poller中添加或者更新通道
void removeChannel(Channel* channel); // 从Poller中移除通道
void assertInLoopThread()
{
if (!isInLoopThread())
{
abortNotInLoo