1048f6023SShuo Chen // excerpts from http://code.google.com/p/muduo/
2048f6023SShuo Chen //
3048f6023SShuo Chen // Use of this source code is governed by a BSD-style license
4048f6023SShuo Chen // that can be found in the License file.
5048f6023SShuo Chen //
6048f6023SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
7048f6023SShuo Chen 
8048f6023SShuo Chen #ifndef MUDUO_NET_CHANNEL_H
9048f6023SShuo Chen #define MUDUO_NET_CHANNEL_H
10048f6023SShuo Chen 
11048f6023SShuo Chen #include <boost/function.hpp>
12048f6023SShuo Chen #include <boost/noncopyable.hpp>
13048f6023SShuo Chen 
14048f6023SShuo Chen namespace muduo
15048f6023SShuo Chen {
16048f6023SShuo Chen 
17048f6023SShuo Chen class EventLoop;
18048f6023SShuo Chen 
19048f6023SShuo Chen ///
20048f6023SShuo Chen /// A selectable I/O channel.
21048f6023SShuo Chen ///
22048f6023SShuo Chen /// This class doesn't own the file descriptor.
23048f6023SShuo Chen /// The file descriptor could be a socket,
24048f6023SShuo Chen /// an eventfd, a timerfd, or a signalfd
25048f6023SShuo Chen class Channel : boost::noncopyable
26048f6023SShuo Chen {
27048f6023SShuo Chen  public:
28048f6023SShuo Chen   typedef boost::function<void()> EventCallback;
29048f6023SShuo Chen 
30048f6023SShuo Chen   Channel(EventLoop* loop, int fd);
31048f6023SShuo Chen+  ~Channel();
32048f6023SShuo Chen 
33048f6023SShuo Chen   void handleEvent();
34048f6023SShuo Chen   void setReadCallback(const EventCallback& cb)
35048f6023SShuo Chen   { readCallback_ = cb; }
36048f6023SShuo Chen   void setWriteCallback(const EventCallback& cb)
37048f6023SShuo Chen   { writeCallback_ = cb; }
38048f6023SShuo Chen   void setErrorCallback(const EventCallback& cb)
39048f6023SShuo Chen   { errorCallback_ = cb; }
40048f6023SShuo Chen+  void setCloseCallback(const EventCallback& cb)
41048f6023SShuo Chen+  { closeCallback_ = cb; }
42048f6023SShuo Chen 
43048f6023SShuo Chen   int fd() const { return fd_; }
44048f6023SShuo Chen   int events() const { return events_; }
45048f6023SShuo Chen   void set_revents(int revt) { revents_ = revt; }
46048f6023SShuo Chen   bool isNoneEvent() const { return events_ == kNoneEvent; }
47048f6023SShuo Chen 
48048f6023SShuo Chen   void enableReading() { events_ |= kReadEvent; update(); }
49048f6023SShuo Chen   // void enableWriting() { events_ |= kWriteEvent; update(); }
50048f6023SShuo Chen   // void disableWriting() { events_ &= ~kWriteEvent; update(); }
51048f6023SShuo Chen!  void disableAll() { events_ = kNoneEvent; update(); }
52048f6023SShuo Chen 
53048f6023SShuo Chen   // for Poller
54048f6023SShuo Chen   int index() { return index_; }
55048f6023SShuo Chen   void set_index(int idx) { index_ = idx; }
56048f6023SShuo Chen 
57048f6023SShuo Chen   EventLoop* ownerLoop() { return loop_; }
58048f6023SShuo Chen 
59048f6023SShuo Chen  private:
60048f6023SShuo Chen   void update();
61048f6023SShuo Chen 
62048f6023SShuo Chen   static const int kNoneEvent;
63048f6023SShuo Chen   static const int kReadEvent;
64048f6023SShuo Chen   static const int kWriteEvent;
65048f6023SShuo Chen 
66048f6023SShuo Chen   EventLoop* loop_;
67048f6023SShuo Chen   const int  fd_;
68048f6023SShuo Chen   int        events_;
69048f6023SShuo Chen   int        revents_;
70048f6023SShuo Chen   int        index_; // used by Poller.
71048f6023SShuo Chen 
72048f6023SShuo Chen+  bool eventHandling_;
73048f6023SShuo Chen+
74048f6023SShuo Chen   EventCallback readCallback_;
75048f6023SShuo Chen   EventCallback writeCallback_;
76048f6023SShuo Chen   EventCallback errorCallback_;
77048f6023SShuo Chen+  EventCallback closeCallback_;
78048f6023SShuo Chen };
79048f6023SShuo Chen 
80048f6023SShuo Chen }
81048f6023SShuo Chen #endif  // MUDUO_NET_CHANNEL_H
82