Channel.cc revision 4b7a333e
14b7a333eSShuo Chen// excerpts from http://code.google.com/p/muduo/
24b7a333eSShuo Chen//
34b7a333eSShuo Chen// Use of this source code is governed by a BSD-style license
44b7a333eSShuo Chen// that can be found in the License file.
54b7a333eSShuo Chen//
64b7a333eSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
74b7a333eSShuo Chen
84b7a333eSShuo Chen#include "Channel.h"
94b7a333eSShuo Chen#include "EventLoop.h"
104b7a333eSShuo Chen#include "logging/Logging.h"
114b7a333eSShuo Chen
124b7a333eSShuo Chen#include <sstream>
134b7a333eSShuo Chen
144b7a333eSShuo Chen#include <poll.h>
154b7a333eSShuo Chen
164b7a333eSShuo Chenusing namespace muduo;
174b7a333eSShuo Chen
184b7a333eSShuo Chenconst int Channel::kNoneEvent = 0;
194b7a333eSShuo Chenconst int Channel::kReadEvent = POLLIN | POLLPRI;
204b7a333eSShuo Chenconst int Channel::kWriteEvent = POLLOUT;
214b7a333eSShuo Chen
224b7a333eSShuo ChenChannel::Channel(EventLoop* loop, int fd__)
234b7a333eSShuo Chen  : loop_(loop),
244b7a333eSShuo Chen    fd_(fd__),
254b7a333eSShuo Chen    events_(0),
264b7a333eSShuo Chen    revents_(0),
274b7a333eSShuo Chen    index_(-1)
284b7a333eSShuo Chen{
294b7a333eSShuo Chen}
304b7a333eSShuo Chen
314b7a333eSShuo ChenChannel::~Channel()
324b7a333eSShuo Chen{
334b7a333eSShuo Chen}
344b7a333eSShuo Chen
354b7a333eSShuo Chenvoid Channel::update()
364b7a333eSShuo Chen{
374b7a333eSShuo Chen  loop_->updateChannel(this);
384b7a333eSShuo Chen}
394b7a333eSShuo Chen
404b7a333eSShuo Chenvoid Channel::handleEvent()
414b7a333eSShuo Chen{
424b7a333eSShuo Chen  if ((revents_ & POLLHUP) && !(revents_ & POLLIN))
434b7a333eSShuo Chen  {
444b7a333eSShuo Chen    // FIXME handleClose()
454b7a333eSShuo Chen  }
464b7a333eSShuo Chen
474b7a333eSShuo Chen  if (revents_ & POLLNVAL)
484b7a333eSShuo Chen  {
494b7a333eSShuo Chen    LOG_WARN << "Channel::handle_event() POLLNVAL";
504b7a333eSShuo Chen  }
514b7a333eSShuo Chen
524b7a333eSShuo Chen  if (revents_ & (POLLERR | POLLNVAL))
534b7a333eSShuo Chen  {
544b7a333eSShuo Chen    if (errorCallback_) errorCallback_();
554b7a333eSShuo Chen  }
564b7a333eSShuo Chen  if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
574b7a333eSShuo Chen  {
584b7a333eSShuo Chen    if (readCallback_) readCallback_();
594b7a333eSShuo Chen  }
604b7a333eSShuo Chen  if (revents_ & POLLOUT)
614b7a333eSShuo Chen  {
624b7a333eSShuo Chen    if (writeCallback_) writeCallback_();
634b7a333eSShuo Chen  }
644b7a333eSShuo Chen}
65