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