Channel.cc revision cc7f415c
1cc7f415cSShuo Chen// excerpts from http://code.google.com/p/muduo/ 2cc7f415cSShuo Chen// 3cc7f415cSShuo Chen// Use of this source code is governed by a BSD-style license 4cc7f415cSShuo Chen// that can be found in the License file. 5cc7f415cSShuo Chen// 6cc7f415cSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7cc7f415cSShuo Chen 8cc7f415cSShuo Chen#include "Channel.h" 9cc7f415cSShuo Chen#include "EventLoop.h" 10cc7f415cSShuo Chen#include "logging/Logging.h" 11cc7f415cSShuo Chen 12cc7f415cSShuo Chen#include <sstream> 13cc7f415cSShuo Chen 14cc7f415cSShuo Chen#include <poll.h> 15cc7f415cSShuo Chen 16cc7f415cSShuo Chenusing namespace muduo; 17cc7f415cSShuo Chen 18cc7f415cSShuo Chenconst int Channel::kNoneEvent = 0; 19cc7f415cSShuo Chenconst int Channel::kReadEvent = POLLIN | POLLPRI; 20cc7f415cSShuo Chenconst int Channel::kWriteEvent = POLLOUT; 21cc7f415cSShuo Chen 22cc7f415cSShuo ChenChannel::Channel(EventLoop* loop, int fd__) 23cc7f415cSShuo Chen : loop_(loop), 24cc7f415cSShuo Chen fd_(fd__), 25cc7f415cSShuo Chen events_(0), 26cc7f415cSShuo Chen revents_(0), 27cc7f415cSShuo Chen index_(-1) 28cc7f415cSShuo Chen{ 29cc7f415cSShuo Chen} 30cc7f415cSShuo Chen 31cc7f415cSShuo ChenChannel::~Channel() 32cc7f415cSShuo Chen{ 33cc7f415cSShuo Chen} 34cc7f415cSShuo Chen 35cc7f415cSShuo Chenvoid Channel::update() 36cc7f415cSShuo Chen{ 37cc7f415cSShuo Chen loop_->updateChannel(this); 38cc7f415cSShuo Chen} 39cc7f415cSShuo Chen 40cc7f415cSShuo Chenvoid Channel::handleEvent() 41cc7f415cSShuo Chen{ 42cc7f415cSShuo Chen if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) 43cc7f415cSShuo Chen { 44cc7f415cSShuo Chen // FIXME handleClose() 45cc7f415cSShuo Chen } 46cc7f415cSShuo Chen 47cc7f415cSShuo Chen if (revents_ & POLLNVAL) 48cc7f415cSShuo Chen { 49cc7f415cSShuo Chen LOG_WARN << "Channel::handle_event() POLLNVAL"; 50cc7f415cSShuo Chen } 51cc7f415cSShuo Chen 52cc7f415cSShuo Chen if (revents_ & (POLLERR | POLLNVAL)) 53cc7f415cSShuo Chen { 54cc7f415cSShuo Chen if (errorCallback_) errorCallback_(); 55cc7f415cSShuo Chen } 56cc7f415cSShuo Chen if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) 57cc7f415cSShuo Chen { 58cc7f415cSShuo Chen if (readCallback_) readCallback_(); 59cc7f415cSShuo Chen } 60cc7f415cSShuo Chen if (revents_ & POLLOUT) 61cc7f415cSShuo Chen { 62cc7f415cSShuo Chen if (writeCallback_) writeCallback_(); 63cc7f415cSShuo Chen } 64cc7f415cSShuo Chen} 65