Channel.cc revision aed8b24f
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 22aed8b24fSShuo ChenChannel::Channel(EventLoop* loop, int fdArg) 23cc7f415cSShuo Chen : loop_(loop), 24aed8b24fSShuo Chen fd_(fdArg), 25cc7f415cSShuo Chen events_(0), 26cc7f415cSShuo Chen revents_(0), 27cc7f415cSShuo Chen index_(-1) 28cc7f415cSShuo Chen{ 29cc7f415cSShuo Chen} 30cc7f415cSShuo Chen 31cc7f415cSShuo Chenvoid Channel::update() 32cc7f415cSShuo Chen{ 33cc7f415cSShuo Chen loop_->updateChannel(this); 34cc7f415cSShuo Chen} 35cc7f415cSShuo Chen 36cc7f415cSShuo Chenvoid Channel::handleEvent() 37cc7f415cSShuo Chen{ 38cc7f415cSShuo Chen if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) 39cc7f415cSShuo Chen { 40cc7f415cSShuo Chen // FIXME handleClose() 41cc7f415cSShuo Chen } 42cc7f415cSShuo Chen 43cc7f415cSShuo Chen if (revents_ & POLLNVAL) 44cc7f415cSShuo Chen { 45cc7f415cSShuo Chen LOG_WARN << "Channel::handle_event() POLLNVAL"; 46cc7f415cSShuo Chen } 47cc7f415cSShuo Chen 48cc7f415cSShuo Chen if (revents_ & (POLLERR | POLLNVAL)) 49cc7f415cSShuo Chen { 50cc7f415cSShuo Chen if (errorCallback_) errorCallback_(); 51cc7f415cSShuo Chen } 52cc7f415cSShuo Chen if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) 53cc7f415cSShuo Chen { 54cc7f415cSShuo Chen if (readCallback_) readCallback_(); 55cc7f415cSShuo Chen } 56cc7f415cSShuo Chen if (revents_ & POLLOUT) 57cc7f415cSShuo Chen { 58cc7f415cSShuo Chen if (writeCallback_) writeCallback_(); 59cc7f415cSShuo Chen } 60cc7f415cSShuo Chen} 61