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 eventHandling_(false) 29{ 30} 31 32Channel::~Channel() 33{ 34 assert(!eventHandling_); 35} 36 37void Channel::update() 38{ 39 loop_->updateChannel(this); 40} 41 42void Channel::handleEvent(Timestamp receiveTime) 43{ 44 eventHandling_ = true; 45 if (revents_ & POLLNVAL) { 46 LOG_WARN << "Channel::handle_event() POLLNVAL"; 47 } 48 49 if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) { 50 LOG_WARN << "Channel::handle_event() POLLHUP"; 51 if (closeCallback_) closeCallback_(); 52 } 53 if (revents_ & (POLLERR | POLLNVAL)) { 54 if (errorCallback_) errorCallback_(); 55 } 56 if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) { 57 if (readCallback_) readCallback_(receiveTime); 58 } 59 if (revents_ & POLLOUT) { 60 if (writeCallback_) writeCallback_(); 61 } 62 eventHandling_ = false; 63} 64