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