Net——EventLoop类(二)、Channel类、Poller类、PollPoller类

本文详细介绍了Net框架中的EventLoop、Channel和Poller类,以及它们之间的关系。每个EventLoop包含多个Channel,用于注册和响应I/O事件。Poller作为事件监听器,有两种实现:POLLPOLLER和EPOLLPOLLER。在事件循环中,Poller的poll()函数检测I/O事件,触发Channel的handleEvent()处理事件。
摘要由CSDN通过智能技术生成

在这里插入图片描述

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值