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); 3117c057c3SShuo Chen ~Channel(); 32bfe73648SShuo Chen 33bfe73648SShuo Chen void handleEvent(); 34bfe73648SShuo Chen void setReadCallback(const EventCallback& cb) 35bfe73648SShuo Chen { readCallback_ = cb; } 36bfe73648SShuo Chen void setWriteCallback(const EventCallback& cb) 37bfe73648SShuo Chen { writeCallback_ = cb; } 38bfe73648SShuo Chen void setErrorCallback(const EventCallback& cb) 39bfe73648SShuo Chen { errorCallback_ = cb; } 4017c057c3SShuo Chen void setCloseCallback(const EventCallback& cb) 4117c057c3SShuo Chen { closeCallback_ = cb; } 42bfe73648SShuo Chen 43bfe73648SShuo Chen int fd() const { return fd_; } 44bfe73648SShuo Chen int events() const { return events_; } 45bfe73648SShuo Chen void set_revents(int revt) { revents_ = revt; } 460615e80eSShuo Chen bool isNoneEvent() const { return events_ == kNoneEvent; } 47bfe73648SShuo Chen 48bfe73648SShuo Chen void enableReading() { events_ |= kReadEvent; update(); } 49bfe73648SShuo Chen // void enableWriting() { events_ |= kWriteEvent; update(); } 50bfe73648SShuo Chen // void disableWriting() { events_ &= ~kWriteEvent; update(); } 5117c057c3SShuo Chen void disableAll() { events_ = kNoneEvent; update(); } 52bfe73648SShuo Chen 53bfe73648SShuo Chen // for Poller 54bfe73648SShuo Chen int index() { return index_; } 55bfe73648SShuo Chen void set_index(int idx) { index_ = idx; } 56bfe73648SShuo Chen 57bfe73648SShuo Chen EventLoop* ownerLoop() { return loop_; } 58bfe73648SShuo Chen 59bfe73648SShuo Chen private: 60bfe73648SShuo Chen void update(); 61bfe73648SShuo Chen 620615e80eSShuo Chen static const int kNoneEvent; 63bfe73648SShuo Chen static const int kReadEvent; 64bfe73648SShuo Chen static const int kWriteEvent; 65bfe73648SShuo Chen 66bfe73648SShuo Chen EventLoop* loop_; 67bfe73648SShuo Chen const int fd_; 68bfe73648SShuo Chen int events_; 69bfe73648SShuo Chen int revents_; 70bfe73648SShuo Chen int index_; // used by Poller. 71bfe73648SShuo Chen 7217c057c3SShuo Chen bool eventHandling_; 7317c057c3SShuo Chen 74bfe73648SShuo Chen EventCallback readCallback_; 75bfe73648SShuo Chen EventCallback writeCallback_; 76bfe73648SShuo Chen EventCallback errorCallback_; 7717c057c3SShuo Chen EventCallback closeCallback_; 78bfe73648SShuo Chen}; 79bfe73648SShuo Chen 80bfe73648SShuo Chen} 81bfe73648SShuo Chen#endif // MUDUO_NET_CHANNEL_H 82