Channel.cc revision aed8b24f
12745a763SShuo Chen// excerpts from http://code.google.com/p/muduo/ 22745a763SShuo Chen// 32745a763SShuo Chen// Use of this source code is governed by a BSD-style license 42745a763SShuo Chen// that can be found in the License file. 52745a763SShuo Chen// 62745a763SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 72745a763SShuo Chen 82745a763SShuo Chen#include "Channel.h" 92745a763SShuo Chen#include "EventLoop.h" 102745a763SShuo Chen#include "logging/Logging.h" 112745a763SShuo Chen 122745a763SShuo Chen#include <sstream> 132745a763SShuo Chen 142745a763SShuo Chen#include <poll.h> 152745a763SShuo Chen 162745a763SShuo Chenusing namespace muduo; 172745a763SShuo Chen 182745a763SShuo Chenconst int Channel::kNoneEvent = 0; 192745a763SShuo Chenconst int Channel::kReadEvent = POLLIN | POLLPRI; 202745a763SShuo Chenconst int Channel::kWriteEvent = POLLOUT; 212745a763SShuo Chen 22aed8b24fSShuo ChenChannel::Channel(EventLoop* loop, int fdArg) 232745a763SShuo Chen : loop_(loop), 24aed8b24fSShuo Chen fd_(fdArg), 252745a763SShuo Chen events_(0), 262745a763SShuo Chen revents_(0), 272745a763SShuo Chen index_(-1) 282745a763SShuo Chen{ 292745a763SShuo Chen} 302745a763SShuo Chen 312745a763SShuo Chenvoid Channel::update() 322745a763SShuo Chen{ 332745a763SShuo Chen loop_->updateChannel(this); 342745a763SShuo Chen} 352745a763SShuo Chen 362745a763SShuo Chenvoid Channel::handleEvent() 372745a763SShuo Chen{ 382745a763SShuo Chen if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) 392745a763SShuo Chen { 402745a763SShuo Chen // FIXME handleClose() 412745a763SShuo Chen } 422745a763SShuo Chen 432745a763SShuo Chen if (revents_ & POLLNVAL) 442745a763SShuo Chen { 452745a763SShuo Chen LOG_WARN << "Channel::handle_event() POLLNVAL"; 462745a763SShuo Chen } 472745a763SShuo Chen 482745a763SShuo Chen if (revents_ & (POLLERR | POLLNVAL)) 492745a763SShuo Chen { 502745a763SShuo Chen if (errorCallback_) errorCallback_(); 512745a763SShuo Chen } 522745a763SShuo Chen if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) 532745a763SShuo Chen { 542745a763SShuo Chen if (readCallback_) readCallback_(); 552745a763SShuo Chen } 562745a763SShuo Chen if (revents_ & POLLOUT) 572745a763SShuo Chen { 582745a763SShuo Chen if (writeCallback_) writeCallback_(); 592745a763SShuo Chen } 602745a763SShuo Chen} 61