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