Channel.h revision 354280cf
1354280cfSShuo Chen// excerpts from http://code.google.com/p/muduo/ 2354280cfSShuo Chen// 3354280cfSShuo Chen// Use of this source code is governed by a BSD-style license 4354280cfSShuo Chen// that can be found in the License file. 5354280cfSShuo Chen// 6354280cfSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7354280cfSShuo Chen 8354280cfSShuo Chen#ifndef MUDUO_NET_CHANNEL_H 9354280cfSShuo Chen#define MUDUO_NET_CHANNEL_H 10354280cfSShuo Chen 11354280cfSShuo Chen#include <boost/function.hpp> 12354280cfSShuo Chen#include <boost/noncopyable.hpp> 13354280cfSShuo Chen 14354280cfSShuo Chen#include <datetime/Timestamp.h> 15354280cfSShuo Chen 16354280cfSShuo Chennamespace muduo 17354280cfSShuo Chen{ 18354280cfSShuo Chen 19354280cfSShuo Chenclass EventLoop; 20354280cfSShuo Chen 21354280cfSShuo Chen/// 22354280cfSShuo Chen/// A selectable I/O channel. 23354280cfSShuo Chen/// 24354280cfSShuo Chen/// This class doesn't own the file descriptor. 25354280cfSShuo Chen/// The file descriptor could be a socket, 26354280cfSShuo Chen/// an eventfd, a timerfd, or a signalfd 27354280cfSShuo Chenclass Channel : boost::noncopyable 28354280cfSShuo Chen{ 29354280cfSShuo Chen public: 30354280cfSShuo Chen typedef boost::function<void()> EventCallback; 31354280cfSShuo Chen typedef boost::function<void(Timestamp)> ReadEventCallback; 32354280cfSShuo Chen 33354280cfSShuo Chen Channel(EventLoop* loop, int fd); 34354280cfSShuo Chen ~Channel(); 35354280cfSShuo Chen 36354280cfSShuo Chen void handleEvent(Timestamp receiveTime); 37354280cfSShuo Chen void setReadCallback(const ReadEventCallback& cb) 38354280cfSShuo Chen { readCallback_ = cb; } 39354280cfSShuo Chen void setWriteCallback(const EventCallback& cb) 40354280cfSShuo Chen { writeCallback_ = cb; } 41354280cfSShuo Chen void setErrorCallback(const EventCallback& cb) 42354280cfSShuo Chen { errorCallback_ = cb; } 43354280cfSShuo Chen void setCloseCallback(const EventCallback& cb) 44354280cfSShuo Chen { closeCallback_ = cb; } 45354280cfSShuo Chen 46354280cfSShuo Chen int fd() const { return fd_; } 47354280cfSShuo Chen int events() const { return events_; } 48354280cfSShuo Chen void set_revents(int revt) { revents_ = revt; } 49354280cfSShuo Chen bool isNoneEvent() const { return events_ == kNoneEvent; } 50354280cfSShuo Chen 51354280cfSShuo Chen void enableReading() { events_ |= kReadEvent; update(); } 52354280cfSShuo Chen void enableWriting() { events_ |= kWriteEvent; update(); } 53354280cfSShuo Chen void disableWriting() { events_ &= ~kWriteEvent; update(); } 54354280cfSShuo Chen void disableAll() { events_ = kNoneEvent; update(); } 55354280cfSShuo Chen bool isWriting() const { return events_ & kWriteEvent; } 56354280cfSShuo Chen 57354280cfSShuo Chen // for Poller 58354280cfSShuo Chen int index() { return index_; } 59354280cfSShuo Chen void set_index(int idx) { index_ = idx; } 60354280cfSShuo Chen 61354280cfSShuo Chen EventLoop* ownerLoop() { return loop_; } 62354280cfSShuo Chen 63354280cfSShuo Chen private: 64354280cfSShuo Chen void update(); 65354280cfSShuo Chen 66354280cfSShuo Chen static const int kNoneEvent; 67354280cfSShuo Chen static const int kReadEvent; 68354280cfSShuo Chen static const int kWriteEvent; 69354280cfSShuo Chen 70354280cfSShuo Chen EventLoop* loop_; 71354280cfSShuo Chen const int fd_; 72354280cfSShuo Chen int events_; 73354280cfSShuo Chen int revents_; 74354280cfSShuo Chen int index_; // used by Poller. 75354280cfSShuo Chen 76354280cfSShuo Chen bool eventHandling_; 77354280cfSShuo Chen 78354280cfSShuo Chen ReadEventCallback readCallback_; 79354280cfSShuo Chen EventCallback writeCallback_; 80354280cfSShuo Chen EventCallback errorCallback_; 81354280cfSShuo Chen EventCallback closeCallback_; 82354280cfSShuo Chen}; 83354280cfSShuo Chen 84354280cfSShuo Chen} 85354280cfSShuo Chen#endif // MUDUO_NET_CHANNEL_H 86