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