12a18e699SShuo Chen// excerpts from http://code.google.com/p/muduo/ 22a18e699SShuo Chen// 32a18e699SShuo Chen// Use of this source code is governed by a BSD-style license 42a18e699SShuo Chen// that can be found in the License file. 52a18e699SShuo Chen// 62a18e699SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 72a18e699SShuo Chen 82a18e699SShuo Chen#ifndef MUDUO_NET_CHANNEL_H 92a18e699SShuo Chen#define MUDUO_NET_CHANNEL_H 102a18e699SShuo Chen 112a18e699SShuo Chen#include <boost/function.hpp> 122a18e699SShuo Chen#include <boost/noncopyable.hpp> 132a18e699SShuo Chen 142a18e699SShuo Chen#include <datetime/Timestamp.h> 152a18e699SShuo Chen 162a18e699SShuo Chennamespace muduo 172a18e699SShuo Chen{ 182a18e699SShuo Chen 192a18e699SShuo Chenclass EventLoop; 202a18e699SShuo Chen 212a18e699SShuo Chen/// 222a18e699SShuo Chen/// A selectable I/O channel. 232a18e699SShuo Chen/// 242a18e699SShuo Chen/// This class doesn't own the file descriptor. 252a18e699SShuo Chen/// The file descriptor could be a socket, 262a18e699SShuo Chen/// an eventfd, a timerfd, or a signalfd 272a18e699SShuo Chenclass Channel : boost::noncopyable 282a18e699SShuo Chen{ 292a18e699SShuo Chen public: 302a18e699SShuo Chen typedef boost::function<void()> EventCallback; 312a18e699SShuo Chen typedef boost::function<void(Timestamp)> ReadEventCallback; 322a18e699SShuo Chen 332a18e699SShuo Chen Channel(EventLoop* loop, int fd); 342a18e699SShuo Chen ~Channel(); 352a18e699SShuo Chen 362a18e699SShuo Chen void handleEvent(Timestamp receiveTime); 372a18e699SShuo Chen void setReadCallback(const ReadEventCallback& cb) 382a18e699SShuo Chen { readCallback_ = cb; } 392a18e699SShuo Chen void setWriteCallback(const EventCallback& cb) 402a18e699SShuo Chen { writeCallback_ = cb; } 412a18e699SShuo Chen void setErrorCallback(const EventCallback& cb) 422a18e699SShuo Chen { errorCallback_ = cb; } 432a18e699SShuo Chen void setCloseCallback(const EventCallback& cb) 442a18e699SShuo Chen { closeCallback_ = cb; } 452a18e699SShuo Chen 462a18e699SShuo Chen int fd() const { return fd_; } 472a18e699SShuo Chen int events() const { return events_; } 482a18e699SShuo Chen void set_revents(int revt) { revents_ = revt; } 492a18e699SShuo Chen bool isNoneEvent() const { return events_ == kNoneEvent; } 502a18e699SShuo Chen 512a18e699SShuo Chen void enableReading() { events_ |= kReadEvent; update(); } 52129fe122SShuo Chen void enableWriting() { events_ |= kWriteEvent; update(); } 53129fe122SShuo Chen void disableWriting() { events_ &= ~kWriteEvent; update(); } 542a18e699SShuo Chen void disableAll() { events_ = kNoneEvent; update(); } 55129fe122SShuo Chen bool isWriting() const { return events_ & kWriteEvent; } 562a18e699SShuo Chen 572a18e699SShuo Chen // for Poller 582a18e699SShuo Chen int index() { return index_; } 592a18e699SShuo Chen void set_index(int idx) { index_ = idx; } 602a18e699SShuo Chen 612a18e699SShuo Chen EventLoop* ownerLoop() { return loop_; } 622a18e699SShuo Chen 632a18e699SShuo Chen private: 642a18e699SShuo Chen void update(); 652a18e699SShuo Chen 662a18e699SShuo Chen static const int kNoneEvent; 672a18e699SShuo Chen static const int kReadEvent; 682a18e699SShuo Chen static const int kWriteEvent; 692a18e699SShuo Chen 702a18e699SShuo Chen EventLoop* loop_; 712a18e699SShuo Chen const int fd_; 722a18e699SShuo Chen int events_; 732a18e699SShuo Chen int revents_; 742a18e699SShuo Chen int index_; // used by Poller. 752a18e699SShuo Chen 762a18e699SShuo Chen bool eventHandling_; 772a18e699SShuo Chen 782a18e699SShuo Chen ReadEventCallback readCallback_; 792a18e699SShuo Chen EventCallback writeCallback_; 802a18e699SShuo Chen EventCallback errorCallback_; 812a18e699SShuo Chen EventCallback closeCallback_; 822a18e699SShuo Chen}; 832a18e699SShuo Chen 842a18e699SShuo Chen} 852a18e699SShuo Chen#endif // MUDUO_NET_CHANNEL_H 86