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