Channel.cc revision 17c057c3
1bfe73648SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2bfe73648SShuo Chen// 3bfe73648SShuo Chen// Use of this source code is governed by a BSD-style license 4bfe73648SShuo Chen// that can be found in the License file. 5bfe73648SShuo Chen// 6bfe73648SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7bfe73648SShuo Chen 8bfe73648SShuo Chen#include "Channel.h" 9bfe73648SShuo Chen#include "EventLoop.h" 10bfe73648SShuo Chen#include "logging/Logging.h" 11bfe73648SShuo Chen 12bfe73648SShuo Chen#include <sstream> 13bfe73648SShuo Chen 14bfe73648SShuo Chen#include <poll.h> 15bfe73648SShuo Chen 16bfe73648SShuo Chenusing namespace muduo; 17bfe73648SShuo Chen 18bfe73648SShuo Chenconst int Channel::kNoneEvent = 0; 19bfe73648SShuo Chenconst int Channel::kReadEvent = POLLIN | POLLPRI; 20bfe73648SShuo Chenconst int Channel::kWriteEvent = POLLOUT; 21bfe73648SShuo Chen 22bfe73648SShuo ChenChannel::Channel(EventLoop* loop, int fdArg) 23bfe73648SShuo Chen : loop_(loop), 24bfe73648SShuo Chen fd_(fdArg), 25bfe73648SShuo Chen events_(0), 26bfe73648SShuo Chen revents_(0), 2717c057c3SShuo Chen index_(-1), 2817c057c3SShuo Chen eventHandling_(false) 29bfe73648SShuo Chen{ 30bfe73648SShuo Chen} 31bfe73648SShuo Chen 3217c057c3SShuo ChenChannel::~Channel() 3317c057c3SShuo Chen{ 3417c057c3SShuo Chen assert(!eventHandling_); 3517c057c3SShuo Chen} 3617c057c3SShuo Chen 37bfe73648SShuo Chenvoid Channel::update() 38bfe73648SShuo Chen{ 39bfe73648SShuo Chen loop_->updateChannel(this); 40bfe73648SShuo Chen} 41bfe73648SShuo Chen 42bfe73648SShuo Chenvoid Channel::handleEvent() 43bfe73648SShuo Chen{ 4417c057c3SShuo Chen eventHandling_ = true; 450615e80eSShuo Chen if (revents_ & POLLNVAL) { 46bfe73648SShuo Chen LOG_WARN << "Channel::handle_event() POLLNVAL"; 47bfe73648SShuo Chen } 48bfe73648SShuo Chen 4917c057c3SShuo Chen if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) { 5017c057c3SShuo Chen LOG_WARN << "Channel::handle_event() POLLHUP"; 5117c057c3SShuo Chen if (closeCallback_) closeCallback_(); 5217c057c3SShuo Chen } 530615e80eSShuo Chen if (revents_ & (POLLERR | POLLNVAL)) { 54bfe73648SShuo Chen if (errorCallback_) errorCallback_(); 55bfe73648SShuo Chen } 560615e80eSShuo Chen if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) { 57bfe73648SShuo Chen if (readCallback_) readCallback_(); 58bfe73648SShuo Chen } 590615e80eSShuo Chen if (revents_ & POLLOUT) { 60bfe73648SShuo Chen if (writeCallback_) writeCallback_(); 61bfe73648SShuo Chen } 6217c057c3SShuo Chen eventHandling_ = false; 63bfe73648SShuo Chen} 64