Channel.h revision 65c497a3
165c497a3SShuo Chen// excerpts from http://code.google.com/p/muduo/
265c497a3SShuo Chen//
365c497a3SShuo Chen// Use of this source code is governed by a BSD-style license
465c497a3SShuo Chen// that can be found in the License file.
565c497a3SShuo Chen//
665c497a3SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
765c497a3SShuo Chen
865c497a3SShuo Chen#ifndef MUDUO_NET_CHANNEL_H
965c497a3SShuo Chen#define MUDUO_NET_CHANNEL_H
1065c497a3SShuo Chen
1165c497a3SShuo Chen#include <boost/function.hpp>
1265c497a3SShuo Chen#include <boost/noncopyable.hpp>
1365c497a3SShuo Chen
1465c497a3SShuo Chennamespace muduo
1565c497a3SShuo Chen{
1665c497a3SShuo Chen
1765c497a3SShuo Chenclass EventLoop;
1865c497a3SShuo Chen
1965c497a3SShuo Chen///
2065c497a3SShuo Chen/// A selectable I/O channel.
2165c497a3SShuo Chen///
2265c497a3SShuo Chen/// This class doesn't own the file descriptor.
2365c497a3SShuo Chen/// The file descriptor could be a socket,
2465c497a3SShuo Chen/// an eventfd, a timerfd, or a signalfd
2565c497a3SShuo Chenclass Channel : boost::noncopyable
2665c497a3SShuo Chen{
2765c497a3SShuo Chen public:
2865c497a3SShuo Chen  typedef boost::function<void()> EventCallback;
2965c497a3SShuo Chen
3065c497a3SShuo Chen  Channel(EventLoop* loop, int fd);
3165c497a3SShuo Chen  ~Channel();
3265c497a3SShuo Chen
3365c497a3SShuo Chen  void handleEvent();
3465c497a3SShuo Chen  void setReadCallback(const EventCallback& cb)
3565c497a3SShuo Chen  { readCallback_ = cb; }
3665c497a3SShuo Chen  void setWriteCallback(const EventCallback& cb)
3765c497a3SShuo Chen  { writeCallback_ = cb; }
3865c497a3SShuo Chen  void setErrorCallback(const EventCallback& cb)
3965c497a3SShuo Chen  { errorCallback_ = cb; }
4065c497a3SShuo Chen  void setCloseCallback(const EventCallback& cb)
4165c497a3SShuo Chen  { closeCallback_ = cb; }
4265c497a3SShuo Chen
4365c497a3SShuo Chen  int fd() const { return fd_; }
4465c497a3SShuo Chen  int events() const { return events_; }
4565c497a3SShuo Chen  void set_revents(int revt) { revents_ = revt; }
4665c497a3SShuo Chen  bool isNoneEvent() const { return events_ == kNoneEvent; }
4765c497a3SShuo Chen
4865c497a3SShuo Chen  void enableReading() { events_ |= kReadEvent; update(); }
4965c497a3SShuo Chen  // void enableWriting() { events_ |= kWriteEvent; update(); }
5065c497a3SShuo Chen  // void disableWriting() { events_ &= ~kWriteEvent; update(); }
5165c497a3SShuo Chen  void disableAll() { events_ = kNoneEvent; update(); }
5265c497a3SShuo Chen
5365c497a3SShuo Chen  // for Poller
5465c497a3SShuo Chen  int index() { return index_; }
5565c497a3SShuo Chen  void set_index(int idx) { index_ = idx; }
5665c497a3SShuo Chen
5765c497a3SShuo Chen  EventLoop* ownerLoop() { return loop_; }
5865c497a3SShuo Chen
5965c497a3SShuo Chen private:
6065c497a3SShuo Chen  void update();
6165c497a3SShuo Chen
6265c497a3SShuo Chen  static const int kNoneEvent;
6365c497a3SShuo Chen  static const int kReadEvent;
6465c497a3SShuo Chen  static const int kWriteEvent;
6565c497a3SShuo Chen
6665c497a3SShuo Chen  EventLoop* loop_;
6765c497a3SShuo Chen  const int  fd_;
6865c497a3SShuo Chen  int        events_;
6965c497a3SShuo Chen  int        revents_;
7065c497a3SShuo Chen  int        index_; // used by Poller.
7165c497a3SShuo Chen
7265c497a3SShuo Chen  bool eventHandling_;
7365c497a3SShuo Chen
7465c497a3SShuo Chen  EventCallback readCallback_;
7565c497a3SShuo Chen  EventCallback writeCallback_;
7665c497a3SShuo Chen  EventCallback errorCallback_;
7765c497a3SShuo Chen  EventCallback closeCallback_;
7865c497a3SShuo Chen};
7965c497a3SShuo Chen
8065c497a3SShuo Chen}
8165c497a3SShuo Chen#endif  // MUDUO_NET_CHANNEL_H
82