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