12745a763SShuo Chen// excerpts from http://code.google.com/p/muduo/
22745a763SShuo Chen//
32745a763SShuo Chen// Use of this source code is governed by a BSD-style license
42745a763SShuo Chen// that can be found in the License file.
52745a763SShuo Chen//
62745a763SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
72745a763SShuo Chen
82745a763SShuo Chen#include "Channel.h"
92745a763SShuo Chen#include "EventLoop.h"
102745a763SShuo Chen#include "logging/Logging.h"
112745a763SShuo Chen
122745a763SShuo Chen#include <sstream>
132745a763SShuo Chen
142745a763SShuo Chen#include <poll.h>
152745a763SShuo Chen
162745a763SShuo Chenusing namespace muduo;
172745a763SShuo Chen
182745a763SShuo Chenconst int Channel::kNoneEvent = 0;
192745a763SShuo Chenconst int Channel::kReadEvent = POLLIN | POLLPRI;
202745a763SShuo Chenconst int Channel::kWriteEvent = POLLOUT;
212745a763SShuo Chen
22aed8b24fSShuo ChenChannel::Channel(EventLoop* loop, int fdArg)
232745a763SShuo Chen  : loop_(loop),
24aed8b24fSShuo Chen    fd_(fdArg),
252745a763SShuo Chen    events_(0),
262745a763SShuo Chen    revents_(0),
272745a763SShuo Chen    index_(-1)
282745a763SShuo Chen{
292745a763SShuo Chen}
302745a763SShuo Chen
312745a763SShuo Chenvoid Channel::update()
322745a763SShuo Chen{
332745a763SShuo Chen  loop_->updateChannel(this);
342745a763SShuo Chen}
352745a763SShuo Chen
362745a763SShuo Chenvoid Channel::handleEvent()
372745a763SShuo Chen{
380615e80eSShuo Chen  if (revents_ & POLLNVAL) {
392745a763SShuo Chen    LOG_WARN << "Channel::handle_event() POLLNVAL";
402745a763SShuo Chen  }
412745a763SShuo Chen
420615e80eSShuo Chen  if (revents_ & (POLLERR | POLLNVAL)) {
432745a763SShuo Chen    if (errorCallback_) errorCallback_();
442745a763SShuo Chen  }
450615e80eSShuo Chen  if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) {
462745a763SShuo Chen    if (readCallback_) readCallback_();
472745a763SShuo Chen  }
480615e80eSShuo Chen  if (revents_ & POLLOUT) {
492745a763SShuo Chen    if (writeCallback_) writeCallback_();
502745a763SShuo Chen  }
512745a763SShuo Chen}
52