Channel.cc revision aed8b24f
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
22aed8b24fSShuo ChenChannel::Channel(EventLoop* loop, int fdArg)
234b7a333eSShuo Chen  : loop_(loop),
24aed8b24fSShuo Chen    fd_(fdArg),
254b7a333eSShuo Chen    events_(0),
264b7a333eSShuo Chen    revents_(0),
274b7a333eSShuo Chen    index_(-1)
284b7a333eSShuo Chen{
294b7a333eSShuo Chen}
304b7a333eSShuo Chen
314b7a333eSShuo Chenvoid Channel::update()
324b7a333eSShuo Chen{
334b7a333eSShuo Chen  loop_->updateChannel(this);
344b7a333eSShuo Chen}
354b7a333eSShuo Chen
364b7a333eSShuo Chenvoid Channel::handleEvent()
374b7a333eSShuo Chen{
384b7a333eSShuo Chen  if ((revents_ & POLLHUP) && !(revents_ & POLLIN))
394b7a333eSShuo Chen  {
404b7a333eSShuo Chen    // FIXME handleClose()
414b7a333eSShuo Chen  }
424b7a333eSShuo Chen
434b7a333eSShuo Chen  if (revents_ & POLLNVAL)
444b7a333eSShuo Chen  {
454b7a333eSShuo Chen    LOG_WARN << "Channel::handle_event() POLLNVAL";
464b7a333eSShuo Chen  }
474b7a333eSShuo Chen
484b7a333eSShuo Chen  if (revents_ & (POLLERR | POLLNVAL))
494b7a333eSShuo Chen  {
504b7a333eSShuo Chen    if (errorCallback_) errorCallback_();
514b7a333eSShuo Chen  }
524b7a333eSShuo Chen  if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
534b7a333eSShuo Chen  {
544b7a333eSShuo Chen    if (readCallback_) readCallback_();
554b7a333eSShuo Chen  }
564b7a333eSShuo Chen  if (revents_ & POLLOUT)
574b7a333eSShuo Chen  {
584b7a333eSShuo Chen    if (writeCallback_) writeCallback_();
594b7a333eSShuo Chen  }
604b7a333eSShuo Chen}
61