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