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{
380615e80eSShuo Chen  if (revents_ & POLLNVAL) {
394b7a333eSShuo Chen    LOG_WARN << "Channel::handle_event() POLLNVAL";
404b7a333eSShuo Chen  }
414b7a333eSShuo Chen
420615e80eSShuo Chen  if (revents_ & (POLLERR | POLLNVAL)) {
434b7a333eSShuo Chen    if (errorCallback_) errorCallback_();
444b7a333eSShuo Chen  }
450615e80eSShuo Chen  if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) {
464b7a333eSShuo Chen    if (readCallback_) readCallback_();
474b7a333eSShuo Chen  }
480615e80eSShuo Chen  if (revents_ & POLLOUT) {
494b7a333eSShuo Chen    if (writeCallback_) writeCallback_();
504b7a333eSShuo Chen  }
514b7a333eSShuo Chen}
52