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