本文简单分析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