12a18e699SShuo Chen// excerpts from http://code.google.com/p/muduo/ 22a18e699SShuo Chen// 32a18e699SShuo Chen// Use of this source code is governed by a BSD-style license 42a18e699SShuo Chen// that can be found in the License file. 52a18e699SShuo Chen// 62a18e699SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 72a18e699SShuo Chen 82a18e699SShuo Chen#include "Channel.h" 92a18e699SShuo Chen#include "EventLoop.h" 102a18e699SShuo Chen#include "logging/Logging.h" 112a18e699SShuo Chen 122a18e699SShuo Chen#include <sstream> 132a18e699SShuo Chen 142a18e699SShuo Chen#include <poll.h> 152a18e699SShuo Chen 162a18e699SShuo Chenusing namespace muduo; 172a18e699SShuo Chen 182a18e699SShuo Chenconst int Channel::kNoneEvent = 0; 192a18e699SShuo Chenconst int Channel::kReadEvent = POLLIN | POLLPRI; 202a18e699SShuo Chenconst int Channel::kWriteEvent = POLLOUT; 212a18e699SShuo Chen 222a18e699SShuo ChenChannel::Channel(EventLoop* loop, int fdArg) 232a18e699SShuo Chen : loop_(loop), 242a18e699SShuo Chen fd_(fdArg), 252a18e699SShuo Chen events_(0), 262a18e699SShuo Chen revents_(0), 272a18e699SShuo Chen index_(-1), 282a18e699SShuo Chen eventHandling_(false) 292a18e699SShuo Chen{ 302a18e699SShuo Chen} 312a18e699SShuo Chen 322a18e699SShuo ChenChannel::~Channel() 332a18e699SShuo Chen{ 342a18e699SShuo Chen assert(!eventHandling_); 352a18e699SShuo Chen} 362a18e699SShuo Chen 372a18e699SShuo Chenvoid Channel::update() 382a18e699SShuo Chen{ 392a18e699SShuo Chen loop_->updateChannel(this); 402a18e699SShuo Chen} 412a18e699SShuo Chen 422a18e699SShuo Chenvoid Channel::handleEvent(Timestamp receiveTime) 432a18e699SShuo Chen{ 442a18e699SShuo Chen eventHandling_ = true; 452a18e699SShuo Chen if (revents_ & POLLNVAL) { 462a18e699SShuo Chen LOG_WARN << "Channel::handle_event() POLLNVAL"; 472a18e699SShuo Chen } 482a18e699SShuo Chen 492a18e699SShuo Chen if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) { 502a18e699SShuo Chen LOG_WARN << "Channel::handle_event() POLLHUP"; 512a18e699SShuo Chen if (closeCallback_) closeCallback_(); 522a18e699SShuo Chen } 532a18e699SShuo Chen if (revents_ & (POLLERR | POLLNVAL)) { 542a18e699SShuo Chen if (errorCallback_) errorCallback_(); 552a18e699SShuo Chen } 562a18e699SShuo Chen if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) { 572a18e699SShuo Chen if (readCallback_) readCallback_(receiveTime); 582a18e699SShuo Chen } 592a18e699SShuo Chen if (revents_ & POLLOUT) { 602a18e699SShuo Chen if (writeCallback_) writeCallback_(); 612a18e699SShuo Chen } 622a18e699SShuo Chen eventHandling_ = false; 632a18e699SShuo Chen} 64