Channel.h revision 0615e80e
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
32  void handleEvent();
33  void setReadCallback(const EventCallback& cb)
34  { readCallback_ = cb; }
35  void setWriteCallback(const EventCallback& cb)
36  { writeCallback_ = cb; }
37  void setErrorCallback(const EventCallback& cb)
38  { errorCallback_ = cb; }
39
40  int fd() const { return fd_; }
41  int events() const { return events_; }
42  void set_revents(int revt) { revents_ = revt; }
43  bool isNoneEvent() const { return events_ == kNoneEvent; }
44
45  void enableReading() { events_ |= kReadEvent; update(); }
46  // void enableWriting() { events_ |= kWriteEvent; update(); }
47  // void disableWriting() { events_ &= ~kWriteEvent; update(); }
48  // void disableAll() { events_ = kNoneEvent; update(); }
49
50  // for Poller
51  int index() { return index_; }
52  void set_index(int idx) { index_ = idx; }
53
54  EventLoop* ownerLoop() { return loop_; }
55
56 private:
57  void update();
58
59  static const int kNoneEvent;
60  static const int kReadEvent;
61  static const int kWriteEvent;
62
63  EventLoop* loop_;
64  const int  fd_;
65  int        events_;
66  int        revents_;
67  int        index_; // used by Poller.
68
69  EventCallback readCallback_;
70  EventCallback writeCallback_;
71  EventCallback errorCallback_;
72};
73
74}
75#endif  // MUDUO_NET_CHANNEL_H
76