Channel类时muduo网络库中将fd与其需要对应的操作进行绑定的包装类,但channel实际上并不持有文件描述符,实际上的事件分发是由Channel类实现的,同时,Channel是EventLoop与IO多路复用之间进行通信的数据对象,体现在EventLoop调用poller的poll填充活跃的事件并且暂存起来,EventLoop再调用活跃链表中Channel提供的事件分发功能完成实际想要的回调
Channel的数据成员
int m_events;///关心的事件
int m_revents;///真实发生的事件(Poller类会将这部分填充好)
int m_idxs;///Poller类使用的下标
ErrFuncType m_errCb;///
ReadFuncType m_readCb;///
WriteFuncType m_writeCb;///三个回调
ErrFuncType m_closeCb;
const int m_fd;///回调对应的fd
EventLoop *m_ownerLoop;///Channel类归属的loop
static const int KNoneEvent;///常量声明(用于发生事件的侦测)
static const int KReadEvent;
static const int KWriteEvent;
bool m_addedToLoop{true};(是否添加到Loop中了)
bool m_tied{false};
std::weak_ptr<void> m_tier;(弱回调的Tricks,确保tcpconnection还活着)
对外暴露的接口
void handleEvent(TimeStamp recvtime);
///设置关注读事件等
void enableRead() {
m_events |= KReadEvent;
///灵魂,最终会通知相应的Poller来更新关注事件
update();
}
void enableWrite() {
m_events |= KWriteEvent;
update();
}
void disableRead() {
m_events &= ~KReadEvent;
update();
}
void setReadCb(ReadFuncType readF) {
m_readCb = std::move(readF);
}
void setWriteCb(WriteFuncType writeF) {
m_writeCb = std::move(writeF);
}
void setCloseCb(ErrFuncType cb) {
m_closeCb = std::move(cb);
}
其中在enable或者disable读或者写事件后,update会最终使得相应的poller做出关注事件改变,代码如下
void CzyNetFrame::Channel::update() {
m_ownerLoop->updateChannel(this);
}
而m_ownerLoop会最终调用自己的poller相应的update来更新关注事件
事件处理HanleEvent则是提供给EventLoop的接口,根据真实发生的事件调用相应的回调:
void CzyNetFrame::Channel::handleEvent(TimeStamp recvtime) {
if (m_tied) {
std::shared_ptr<void> locked = m_tier.lock();
if (locked) {
///确保调用TcpConnection事件时,其还活着
handleEventWithGraund(recvtime);
}
} else {
handleEventWithGraund(recvtime);
}
}
///根据真实事件调用相应回调
void Channel::handleEventWithGraund(TimeStamp recvtime) {
///POLLNVAL代表一个文件打不开的情况
if (m_revents & POLLNVAL) {
LOG_WARN << "POLL NVAL ERROR in fd: " << m_fd;
} else if (m_revents & (POLLERR | POLLERR)) {
if (m_errCb) {
m_errCb();
}
} else if (m_revents & (POLLIN)) {
m_readCb(TimeStamp::now());
} else if (m_revents & POLLOUT) {
if (m_writeCb) {
m_writeCb();
}
}
}