类似于EventLoop的分析,忽略一些次要成员。
1.类定义
class Channel
{
typedef boost::function<void()> EventCallback;
typedef boost::function<void(Timestamp)> ReadEventCallback;
/***************Function Member******************/
//bind with loop and fd
Channel(EventLoop* loop, int fd);
~Channel();
//bind with callback
void setReadCallBack(const ReadEventCallback& cb);
void setWriteCallBack(const EventCallback& cb);
void setCloseCallBack(cosnt EventCallback& cb);
void setErrorCallBack(const EventCallback& cb);
//change channel in Poller
void enableReading();
void enableWriting();
void disableReading();
void disableWriting();
void update();
void remove();
//key function
void handleEvent(Timerstamp receiveTime);
/*************Data Member***********************/
//bind with loop and fd
EventLoop* loop;
const int fd;
//bind with callback
ReadEventCallback readCallback;
EventCallback writeCallback;
EventCallback closeCallback;
EventCallback errorCallback;
//change channel in Poller
const int fd;
int events;
int revents;
}
2.channel与libev中的watcher对比
①init <------------> Channel::setXXXCallback()
②set <------------> Channel::enablingXXX()
③start <------------> Channel::update()
④stop <------------> Channel::remove()
当然这只是功能上大体对应,并不是严格对应。
比如enablingXXX本身就包含了设定监听的具体内容(read/write)以及调用update
3.三种channel
在loop这几个文件的学习之中,目前共遇到3种channel(从处理fd上来说)
IO channel---socketfd
Timer channel---timerfd
wakeup channel---eventfd
(1)IO channel
其操作由用户来指定,各种功能操作被封装到更高层的类里面(2)Timer channel
①New
TimerQueue::TimerQueue()
②SetCallback
TimerQueue::TimerQueue()
③Enablingxxx
TimerQueue::TimerQueue()
TimerQueue::addTimer()
④HandleEvent
TimerQueue::handleRead()
TimerQueue::cancel()
TimerQueue::~TimerQueue()
(3)Wakeup channel
①New
EventLoop::EventLoop()
②SetCallback
EventLoop::EventLoop()
③Enablingxxx
EventLoop::EventLoop()
EventLoop::runInLoop()
④HandleEvent
EventLoop::doPendingFunctors()
EventLoop::RemoveChannel()
EventLoop::~EventLoop()