Channel.cc revision aed8b24f
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{
389a1e991dSShuo Chen  if ((revents_ & POLLHUP) && !(revents_ & POLLIN))
399a1e991dSShuo Chen  {
409a1e991dSShuo Chen    // FIXME handleClose()
419a1e991dSShuo Chen  }
429a1e991dSShuo Chen
439a1e991dSShuo Chen  if (revents_ & POLLNVAL)
449a1e991dSShuo Chen  {
459a1e991dSShuo Chen    LOG_WARN << "Channel::handle_event() POLLNVAL";
469a1e991dSShuo Chen  }
479a1e991dSShuo Chen
489a1e991dSShuo Chen  if (revents_ & (POLLERR | POLLNVAL))
499a1e991dSShuo Chen  {
509a1e991dSShuo Chen    if (errorCallback_) errorCallback_();
519a1e991dSShuo Chen  }
529a1e991dSShuo Chen  if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
539a1e991dSShuo Chen  {
549a1e991dSShuo Chen    if (readCallback_) readCallback_();
559a1e991dSShuo Chen  }
569a1e991dSShuo Chen  if (revents_ & POLLOUT)
579a1e991dSShuo Chen  {
589a1e991dSShuo Chen    if (writeCallback_) writeCallback_();
599a1e991dSShuo Chen  }
609a1e991dSShuo Chen}
61