1// excerpts from http://code.google.com/p/muduo/
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the License file.
5//
6// Author: Shuo Chen (chenshuo at chenshuo dot com)
7
8#include "Channel.h"
9#include "EventLoop.h"
10#include "logging/Logging.h"
11
12#include <sstream>
13
14#include <poll.h>
15
16using namespace muduo;
17
18const int Channel::kNoneEvent = 0;
19const int Channel::kReadEvent = POLLIN | POLLPRI;
20const int Channel::kWriteEvent = POLLOUT;
21
22Channel::Channel(EventLoop* loop, int fdArg)
23  : loop_(loop),
24    fd_(fdArg),
25    events_(0),
26    revents_(0),
27    index_(-1)
28{
29}
30
31void Channel::update()
32{
33  loop_->updateChannel(this);
34}
35
36void Channel::handleEvent()
37{
38  if (revents_ & POLLNVAL) {
39    LOG_WARN << "Channel::handle_event() POLLNVAL";
40  }
41
42  if (revents_ & (POLLERR | POLLNVAL)) {
43    if (errorCallback_) errorCallback_();
44  }
45  if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) {
46    if (readCallback_) readCallback_();
47  }
48  if (revents_ & POLLOUT) {
49    if (writeCallback_) writeCallback_();
50  }
51}
52