Channel.h revision 0615e80e
1bfe73648SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2bfe73648SShuo Chen// 3bfe73648SShuo Chen// Use of this source code is governed by a BSD-style license 4bfe73648SShuo Chen// that can be found in the License file. 5bfe73648SShuo Chen// 6bfe73648SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7bfe73648SShuo Chen 8bfe73648SShuo Chen#ifndef MUDUO_NET_CHANNEL_H 9bfe73648SShuo Chen#define MUDUO_NET_CHANNEL_H 10bfe73648SShuo Chen 11bfe73648SShuo Chen#include <boost/function.hpp> 12bfe73648SShuo Chen#include <boost/noncopyable.hpp> 13bfe73648SShuo Chen 14bfe73648SShuo Chennamespace muduo 15bfe73648SShuo Chen{ 16bfe73648SShuo Chen 17bfe73648SShuo Chenclass EventLoop; 18bfe73648SShuo Chen 19bfe73648SShuo Chen/// 20bfe73648SShuo Chen/// A selectable I/O channel. 21bfe73648SShuo Chen/// 22bfe73648SShuo Chen/// This class doesn't own the file descriptor. 23bfe73648SShuo Chen/// The file descriptor could be a socket, 24bfe73648SShuo Chen/// an eventfd, a timerfd, or a signalfd 25bfe73648SShuo Chenclass Channel : boost::noncopyable 26bfe73648SShuo Chen{ 27bfe73648SShuo Chen public: 28bfe73648SShuo Chen typedef boost::function<void()> EventCallback; 29bfe73648SShuo Chen 30bfe73648SShuo Chen Channel(EventLoop* loop, int fd); 31bfe73648SShuo Chen 32bfe73648SShuo Chen void handleEvent(); 33bfe73648SShuo Chen void setReadCallback(const EventCallback& cb) 34bfe73648SShuo Chen { readCallback_ = cb; } 35bfe73648SShuo Chen void setWriteCallback(const EventCallback& cb) 36bfe73648SShuo Chen { writeCallback_ = cb; } 37bfe73648SShuo Chen void setErrorCallback(const EventCallback& cb) 38bfe73648SShuo Chen { errorCallback_ = cb; } 39bfe73648SShuo Chen 40bfe73648SShuo Chen int fd() const { return fd_; } 41bfe73648SShuo Chen int events() const { return events_; } 42bfe73648SShuo Chen void set_revents(int revt) { revents_ = revt; } 430615e80eSShuo Chen bool isNoneEvent() const { return events_ == kNoneEvent; } 44bfe73648SShuo Chen 45bfe73648SShuo Chen void enableReading() { events_ |= kReadEvent; update(); } 46bfe73648SShuo Chen // void enableWriting() { events_ |= kWriteEvent; update(); } 47bfe73648SShuo Chen // void disableWriting() { events_ &= ~kWriteEvent; update(); } 48bfe73648SShuo Chen // void disableAll() { events_ = kNoneEvent; update(); } 49bfe73648SShuo Chen 50bfe73648SShuo Chen // for Poller 51bfe73648SShuo Chen int index() { return index_; } 52bfe73648SShuo Chen void set_index(int idx) { index_ = idx; } 53bfe73648SShuo Chen 54bfe73648SShuo Chen EventLoop* ownerLoop() { return loop_; } 55bfe73648SShuo Chen 56bfe73648SShuo Chen private: 57bfe73648SShuo Chen void update(); 58bfe73648SShuo Chen 590615e80eSShuo Chen static const int kNoneEvent; 60bfe73648SShuo Chen static const int kReadEvent; 61bfe73648SShuo Chen static const int kWriteEvent; 62bfe73648SShuo Chen 63bfe73648SShuo Chen EventLoop* loop_; 64bfe73648SShuo Chen const int fd_; 65bfe73648SShuo Chen int events_; 66bfe73648SShuo Chen int revents_; 67bfe73648SShuo Chen int index_; // used by Poller. 68bfe73648SShuo Chen 69bfe73648SShuo Chen EventCallback readCallback_; 70bfe73648SShuo Chen EventCallback writeCallback_; 71bfe73648SShuo Chen EventCallback errorCallback_; 72bfe73648SShuo Chen}; 73bfe73648SShuo Chen 74bfe73648SShuo Chen} 75bfe73648SShuo Chen#endif // MUDUO_NET_CHANNEL_H 76