140161064SShuo Chen// excerpts from http://code.google.com/p/muduo/
240161064SShuo Chen//
340161064SShuo Chen// Use of this source code is governed by a BSD-style license
440161064SShuo Chen// that can be found in the License file.
540161064SShuo Chen//
640161064SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
740161064SShuo Chen
840161064SShuo Chen#include "Channel.h"
940161064SShuo Chen#include "EventLoop.h"
1040161064SShuo Chen#include "logging/Logging.h"
1140161064SShuo Chen
1240161064SShuo Chen#include <sstream>
1340161064SShuo Chen
1440161064SShuo Chen#include <poll.h>
1540161064SShuo Chen
1640161064SShuo Chenusing namespace muduo;
1740161064SShuo Chen
1840161064SShuo Chenconst int Channel::kNoneEvent = 0;
1940161064SShuo Chenconst int Channel::kReadEvent = POLLIN | POLLPRI;
2040161064SShuo Chenconst int Channel::kWriteEvent = POLLOUT;
2140161064SShuo Chen
2240161064SShuo ChenChannel::Channel(EventLoop* loop, int fdArg)
2340161064SShuo Chen  : loop_(loop),
2440161064SShuo Chen    fd_(fdArg),
2540161064SShuo Chen    events_(0),
2640161064SShuo Chen    revents_(0),
2740161064SShuo Chen    index_(-1),
2840161064SShuo Chen    eventHandling_(false)
2940161064SShuo Chen{
3040161064SShuo Chen}
3140161064SShuo Chen
3240161064SShuo ChenChannel::~Channel()
3340161064SShuo Chen{
3440161064SShuo Chen  assert(!eventHandling_);
3540161064SShuo Chen}
3640161064SShuo Chen
3740161064SShuo Chenvoid Channel::update()
3840161064SShuo Chen{
3940161064SShuo Chen  loop_->updateChannel(this);
4040161064SShuo Chen}
4140161064SShuo Chen
4240161064SShuo Chenvoid Channel::handleEvent(Timestamp receiveTime)
4340161064SShuo Chen{
4440161064SShuo Chen  eventHandling_ = true;
4540161064SShuo Chen  if (revents_ & POLLNVAL) {
4640161064SShuo Chen    LOG_WARN << "Channel::handle_event() POLLNVAL";
4740161064SShuo Chen  }
4840161064SShuo Chen
4940161064SShuo Chen  if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) {
5040161064SShuo Chen    LOG_WARN << "Channel::handle_event() POLLHUP";
5140161064SShuo Chen    if (closeCallback_) closeCallback_();
5240161064SShuo Chen  }
5340161064SShuo Chen  if (revents_ & (POLLERR | POLLNVAL)) {
5440161064SShuo Chen    if (errorCallback_) errorCallback_();
5540161064SShuo Chen  }
5640161064SShuo Chen  if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) {
5740161064SShuo Chen    if (readCallback_) readCallback_(receiveTime);
5840161064SShuo Chen  }
5940161064SShuo Chen  if (revents_ & POLLOUT) {
6040161064SShuo Chen    if (writeCallback_) writeCallback_();
6140161064SShuo Chen  }
6240161064SShuo Chen  eventHandling_ = false;
6340161064SShuo Chen}
64