13b467340SShuo Chen // excerpts from http://code.google.com/p/muduo/
23b467340SShuo Chen //
33b467340SShuo Chen // Use of this source code is governed by a BSD-style license
43b467340SShuo Chen // that can be found in the License file.
53b467340SShuo Chen //
63b467340SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
73b467340SShuo Chen 
83b467340SShuo Chen #include "Channel.h"
93b467340SShuo Chen #include "EventLoop.h"
103b467340SShuo Chen #include "logging/Logging.h"
113b467340SShuo Chen 
123b467340SShuo Chen #include <sstream>
133b467340SShuo Chen 
143b467340SShuo Chen #include <poll.h>
153b467340SShuo Chen 
163b467340SShuo Chen using namespace muduo;
173b467340SShuo Chen 
183b467340SShuo Chen const int Channel::kNoneEvent = 0;
193b467340SShuo Chen const int Channel::kReadEvent = POLLIN | POLLPRI;
203b467340SShuo Chen const int Channel::kWriteEvent = POLLOUT;
213b467340SShuo Chen 
223b467340SShuo Chen Channel::Channel(EventLoop* loop, int fdArg)
233b467340SShuo Chen   : loop_(loop),
243b467340SShuo Chen     fd_(fdArg),
253b467340SShuo Chen     events_(0),
263b467340SShuo Chen     revents_(0),
273b467340SShuo Chen     index_(-1),
283b467340SShuo Chen     eventHandling_(false)
293b467340SShuo Chen {
303b467340SShuo Chen }
313b467340SShuo Chen 
323b467340SShuo Chen Channel::~Channel()
333b467340SShuo Chen {
343b467340SShuo Chen   assert(!eventHandling_);
353b467340SShuo Chen }
363b467340SShuo Chen 
373b467340SShuo Chen void Channel::update()
383b467340SShuo Chen {
393b467340SShuo Chen   loop_->updateChannel(this);
403b467340SShuo Chen }
413b467340SShuo Chen 
423b467340SShuo Chen!void Channel::handleEvent(Timestamp receiveTime)
433b467340SShuo Chen {
443b467340SShuo Chen   eventHandling_ = true;
453b467340SShuo Chen   if (revents_ & POLLNVAL) {
463b467340SShuo Chen     LOG_WARN << "Channel::handle_event() POLLNVAL";
473b467340SShuo Chen   }
483b467340SShuo Chen 
493b467340SShuo Chen   if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) {
503b467340SShuo Chen     LOG_WARN << "Channel::handle_event() POLLHUP";
513b467340SShuo Chen     if (closeCallback_) closeCallback_();
523b467340SShuo Chen   }
533b467340SShuo Chen   if (revents_ & (POLLERR | POLLNVAL)) {
543b467340SShuo Chen     if (errorCallback_) errorCallback_();
553b467340SShuo Chen   }
563b467340SShuo Chen   if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) {
573b467340SShuo Chen!    if (readCallback_) readCallback_(receiveTime);
583b467340SShuo Chen   }
593b467340SShuo Chen   if (revents_ & POLLOUT) {
603b467340SShuo Chen     if (writeCallback_) writeCallback_();
613b467340SShuo Chen   }
623b467340SShuo Chen   eventHandling_ = false;
633b467340SShuo Chen }
64