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