Channel.cc revision 2745a763
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
222745a763SShuo ChenChannel::Channel(EventLoop* loop, int fd__)
232745a763SShuo Chen  : loop_(loop),
242745a763SShuo Chen    fd_(fd__),
252745a763SShuo Chen    events_(0),
262745a763SShuo Chen    revents_(0),
272745a763SShuo Chen    index_(-1)
282745a763SShuo Chen{
292745a763SShuo Chen}
302745a763SShuo Chen
312745a763SShuo ChenChannel::~Channel()
322745a763SShuo Chen{
332745a763SShuo Chen}
342745a763SShuo Chen
352745a763SShuo Chenvoid Channel::update()
362745a763SShuo Chen{
372745a763SShuo Chen  loop_->updateChannel(this);
382745a763SShuo Chen}
392745a763SShuo Chen
402745a763SShuo Chenvoid Channel::handleEvent()
412745a763SShuo Chen{
422745a763SShuo Chen  if ((revents_ & POLLHUP) && !(revents_ & POLLIN))
432745a763SShuo Chen  {
442745a763SShuo Chen    // FIXME handleClose()
452745a763SShuo Chen  }
462745a763SShuo Chen
472745a763SShuo Chen  if (revents_ & POLLNVAL)
482745a763SShuo Chen  {
492745a763SShuo Chen    LOG_WARN << "Channel::handle_event() POLLNVAL";
502745a763SShuo Chen  }
512745a763SShuo Chen
522745a763SShuo Chen  if (revents_ & (POLLERR | POLLNVAL))
532745a763SShuo Chen  {
542745a763SShuo Chen    if (errorCallback_) errorCallback_();
552745a763SShuo Chen  }
562745a763SShuo Chen  if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
572745a763SShuo Chen  {
582745a763SShuo Chen    if (readCallback_) readCallback_();
592745a763SShuo Chen  }
602745a763SShuo Chen  if (revents_ & POLLOUT)
612745a763SShuo Chen  {
622745a763SShuo Chen    if (writeCallback_) writeCallback_();
632745a763SShuo Chen  }
642745a763SShuo Chen}
65