140161064SShuo Chen// excerpts from http://code.google.com/p/muduo/ 240161064SShuo Chen// 340161064SShuo Chen// Use of this source code is governed by a BSD-style license 440161064SShuo Chen// that can be found in the License file. 540161064SShuo Chen// 640161064SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 740161064SShuo Chen 840161064SShuo Chen#ifndef MUDUO_NET_CHANNEL_H 940161064SShuo Chen#define MUDUO_NET_CHANNEL_H 1040161064SShuo Chen 1140161064SShuo Chen#include <boost/function.hpp> 1240161064SShuo Chen#include <boost/noncopyable.hpp> 1340161064SShuo Chen 1440161064SShuo Chen#include <datetime/Timestamp.h> 1540161064SShuo Chen 1640161064SShuo Chennamespace muduo 1740161064SShuo Chen{ 1840161064SShuo Chen 1940161064SShuo Chenclass EventLoop; 2040161064SShuo Chen 2140161064SShuo Chen/// 2240161064SShuo Chen/// A selectable I/O channel. 2340161064SShuo Chen/// 2440161064SShuo Chen/// This class doesn't own the file descriptor. 2540161064SShuo Chen/// The file descriptor could be a socket, 2640161064SShuo Chen/// an eventfd, a timerfd, or a signalfd 2740161064SShuo Chenclass Channel : boost::noncopyable 2840161064SShuo Chen{ 2940161064SShuo Chen public: 3040161064SShuo Chen typedef boost::function<void()> EventCallback; 3140161064SShuo Chen typedef boost::function<void(Timestamp)> ReadEventCallback; 3240161064SShuo Chen 3340161064SShuo Chen Channel(EventLoop* loop, int fd); 3440161064SShuo Chen ~Channel(); 3540161064SShuo Chen 3640161064SShuo Chen void handleEvent(Timestamp receiveTime); 3740161064SShuo Chen void setReadCallback(const ReadEventCallback& cb) 3840161064SShuo Chen { readCallback_ = cb; } 3940161064SShuo Chen void setWriteCallback(const EventCallback& cb) 4040161064SShuo Chen { writeCallback_ = cb; } 4140161064SShuo Chen void setErrorCallback(const EventCallback& cb) 4240161064SShuo Chen { errorCallback_ = cb; } 4340161064SShuo Chen void setCloseCallback(const EventCallback& cb) 4440161064SShuo Chen { closeCallback_ = cb; } 4540161064SShuo Chen 4640161064SShuo Chen int fd() const { return fd_; } 4740161064SShuo Chen int events() const { return events_; } 4840161064SShuo Chen void set_revents(int revt) { revents_ = revt; } 4940161064SShuo Chen bool isNoneEvent() const { return events_ == kNoneEvent; } 5040161064SShuo Chen 5140161064SShuo Chen void enableReading() { events_ |= kReadEvent; update(); } 5240161064SShuo Chen void enableWriting() { events_ |= kWriteEvent; update(); } 5340161064SShuo Chen void disableWriting() { events_ &= ~kWriteEvent; update(); } 5440161064SShuo Chen void disableAll() { events_ = kNoneEvent; update(); } 5540161064SShuo Chen bool isWriting() const { return events_ & kWriteEvent; } 5640161064SShuo Chen 5740161064SShuo Chen // for Poller 5840161064SShuo Chen int index() { return index_; } 5940161064SShuo Chen void set_index(int idx) { index_ = idx; } 6040161064SShuo Chen 6140161064SShuo Chen EventLoop* ownerLoop() { return loop_; } 6240161064SShuo Chen 6340161064SShuo Chen private: 6440161064SShuo Chen void update(); 6540161064SShuo Chen 6640161064SShuo Chen static const int kNoneEvent; 6740161064SShuo Chen static const int kReadEvent; 6840161064SShuo Chen static const int kWriteEvent; 6940161064SShuo Chen 7040161064SShuo Chen EventLoop* loop_; 7140161064SShuo Chen const int fd_; 7240161064SShuo Chen int events_; 7340161064SShuo Chen int revents_; 7440161064SShuo Chen int index_; // used by Poller. 7540161064SShuo Chen 7640161064SShuo Chen bool eventHandling_; 7740161064SShuo Chen 7840161064SShuo Chen ReadEventCallback readCallback_; 7940161064SShuo Chen EventCallback writeCallback_; 8040161064SShuo Chen EventCallback errorCallback_; 8140161064SShuo Chen EventCallback closeCallback_; 8240161064SShuo Chen}; 8340161064SShuo Chen 8440161064SShuo Chen} 8540161064SShuo Chen#endif // MUDUO_NET_CHANNEL_H 86