1048f6023SShuo Chen // excerpts from http://code.google.com/p/muduo/ 2048f6023SShuo Chen // 3048f6023SShuo Chen // Use of this source code is governed by a BSD-style license 4048f6023SShuo Chen // that can be found in the License file. 5048f6023SShuo Chen // 6048f6023SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com) 7048f6023SShuo Chen 8048f6023SShuo Chen #include "Channel.h" 9048f6023SShuo Chen #include "EventLoop.h" 10048f6023SShuo Chen #include "logging/Logging.h" 11048f6023SShuo Chen 12048f6023SShuo Chen #include <sstream> 13048f6023SShuo Chen 14048f6023SShuo Chen #include <poll.h> 15048f6023SShuo Chen 16048f6023SShuo Chen using namespace muduo; 17048f6023SShuo Chen 18048f6023SShuo Chen const int Channel::kNoneEvent = 0; 19048f6023SShuo Chen const int Channel::kReadEvent = POLLIN | POLLPRI; 20048f6023SShuo Chen const int Channel::kWriteEvent = POLLOUT; 21048f6023SShuo Chen 22048f6023SShuo Chen Channel::Channel(EventLoop* loop, int fdArg) 23048f6023SShuo Chen : loop_(loop), 24048f6023SShuo Chen fd_(fdArg), 25048f6023SShuo Chen events_(0), 26048f6023SShuo Chen revents_(0), 27048f6023SShuo Chen index_(-1), 28048f6023SShuo Chen+ eventHandling_(false) 29048f6023SShuo Chen { 30048f6023SShuo Chen } 31048f6023SShuo Chen 32048f6023SShuo Chen+Channel::~Channel() 33048f6023SShuo Chen+{ 34048f6023SShuo Chen+ assert(!eventHandling_); 35048f6023SShuo Chen+} 36048f6023SShuo Chen+ 37048f6023SShuo Chen void Channel::update() 38048f6023SShuo Chen { 39048f6023SShuo Chen loop_->updateChannel(this); 40048f6023SShuo Chen } 41048f6023SShuo Chen 42048f6023SShuo Chen void Channel::handleEvent() 43048f6023SShuo Chen { 44048f6023SShuo Chen+ eventHandling_ = true; 45048f6023SShuo Chen if (revents_ & POLLNVAL) { 46048f6023SShuo Chen LOG_WARN << "Channel::handle_event() POLLNVAL"; 47048f6023SShuo Chen } 48048f6023SShuo Chen 49048f6023SShuo Chen+ if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) { 50048f6023SShuo Chen+ LOG_WARN << "Channel::handle_event() POLLHUP"; 51048f6023SShuo Chen+ if (closeCallback_) closeCallback_(); 52048f6023SShuo Chen+ } 53048f6023SShuo Chen if (revents_ & (POLLERR | POLLNVAL)) { 54048f6023SShuo Chen if (errorCallback_) errorCallback_(); 55048f6023SShuo Chen } 56048f6023SShuo Chen if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) { 57048f6023SShuo Chen if (readCallback_) readCallback_(); 58048f6023SShuo Chen } 59048f6023SShuo Chen if (revents_ & POLLOUT) { 60048f6023SShuo Chen if (writeCallback_) writeCallback_(); 61048f6023SShuo Chen } 62048f6023SShuo Chen+ eventHandling_ = false; 63048f6023SShuo Chen } 64