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