muduo源码分析——Channel

本文简单分析muduo的Channel类,我在学习muduo的时候,因为一开始看的很晕,后来找了很多文章来学习,这个Channel有很多种叫法“事件分发器”,“I/O选择器”等,但我还是喜欢叫它通道,为什么呢,因为,当I/O事件发生的时候,最终会回调到Channel的回调函数中,所以我更喜欢把它当作通道,当然这也是它的中文翻译。

每一个Channel都对应唯一的EventLoop,也就是一个I/O线程,所以一般不用担心Channel的线程安全问题,因为别的线程不会操作到当前的这个Channel。

我们看看Channel类的成员:

		private:
		bool update();
		void handleEventWithGuard(Timestamp receiveTime);//真正被调用的方法,用来执行判断事件类型,并执行绑定的函数

		static const int            kNoneEvent;//0
		static const int            kReadEvent;//=XPOLLIN | XPOLLPRI  其中XPOLLIN=0x0100|0x0200,XPOLLPRI=0x0400
		static const int            kWriteEvent;//0x0010

		EventLoop*                  loop_;//属于的EventLoop
		const int                   fd_;//对应的文件描述符,不过并不拥有
		int                         events_;
		int                         revents_; // it's the received event types of epoll or poll
		int                         index_; // used by Poller.
		bool                        logHup_;

		std::weak_ptr<void>         tie_;           //std::shared_ptr<void>/std::shared_ptr<void>可以指向不同的数据类型
		bool                        tied_;//应该是表明,上面的tie_是否指向了对象
		//bool                        eventHandling_;
		//bool                        addedToLoop_;
		ReadEventCallback           readCallback_;
		EventCallback               writeCallback_;
		EventCallback               closeCallback_;
		EventCallback               errorCallback_;
	};

关于这个events_我将他当作一个标志,标记通道的状态。

在TcpConnection中哪这个例子来看看

   if (!channel_->isWriting() && outputBuffer_.readableBytes() == 0)

这是在TcpConnection::sendInLoop函数中的一个判断,当channel没有在写,且输出缓冲区没有数据的时候,直接调用socket的write去写数据,我们看看这个iswriting函数

bool isWriting() const {
    return events_ & kWriteEvent
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值