19a1e991dSShuo Chen// excerpts from http://code.google.com/p/muduo/
29a1e991dSShuo Chen//
39a1e991dSShuo Chen// Use of this source code is governed by a BSD-style license
49a1e991dSShuo Chen// that can be found in the License file.
59a1e991dSShuo Chen//
69a1e991dSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
79a1e991dSShuo Chen
89a1e991dSShuo Chen#include "Channel.h"
99a1e991dSShuo Chen#include "EventLoop.h"
109a1e991dSShuo Chen#include "logging/Logging.h"
119a1e991dSShuo Chen
129a1e991dSShuo Chen#include <sstream>
139a1e991dSShuo Chen
149a1e991dSShuo Chen#include <poll.h>
159a1e991dSShuo Chen
169a1e991dSShuo Chenusing namespace muduo;
179a1e991dSShuo Chen
189a1e991dSShuo Chenconst int Channel::kNoneEvent = 0;
199a1e991dSShuo Chenconst int Channel::kReadEvent = POLLIN | POLLPRI;
209a1e991dSShuo Chenconst int Channel::kWriteEvent = POLLOUT;
219a1e991dSShuo Chen
22aed8b24fSShuo ChenChannel::Channel(EventLoop* loop, int fdArg)
239a1e991dSShuo Chen  : loop_(loop),
24aed8b24fSShuo Chen    fd_(fdArg),
259a1e991dSShuo Chen    events_(0),
269a1e991dSShuo Chen    revents_(0),
279a1e991dSShuo Chen    index_(-1)
289a1e991dSShuo Chen{
299a1e991dSShuo Chen}
309a1e991dSShuo Chen
319a1e991dSShuo Chenvoid Channel::update()
329a1e991dSShuo Chen{
339a1e991dSShuo Chen  loop_->updateChannel(this);
349a1e991dSShuo Chen}
359a1e991dSShuo Chen
369a1e991dSShuo Chenvoid Channel::handleEvent()
379a1e991dSShuo Chen{
380615e80eSShuo Chen  if (revents_ & POLLNVAL) {
399a1e991dSShuo Chen    LOG_WARN << "Channel::handle_event() POLLNVAL";
409a1e991dSShuo Chen  }
419a1e991dSShuo Chen
420615e80eSShuo Chen  if (revents_ & (POLLERR | POLLNVAL)) {
439a1e991dSShuo Chen    if (errorCallback_) errorCallback_();
449a1e991dSShuo Chen  }
450615e80eSShuo Chen  if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) {
469a1e991dSShuo Chen    if (readCallback_) readCallback_();
479a1e991dSShuo Chen  }
480615e80eSShuo Chen  if (revents_ & POLLOUT) {
499a1e991dSShuo Chen    if (writeCallback_) writeCallback_();
509a1e991dSShuo Chen  }
519a1e991dSShuo Chen}
52