14b7a333eSShuo Chen// excerpts from http://code.google.com/p/muduo/
24b7a333eSShuo Chen//
34b7a333eSShuo Chen// Use of this source code is governed by a BSD-style license
44b7a333eSShuo Chen// that can be found in the License file.
54b7a333eSShuo Chen//
64b7a333eSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
74b7a333eSShuo Chen
84b7a333eSShuo Chen#ifndef MUDUO_NET_CHANNEL_H
94b7a333eSShuo Chen#define MUDUO_NET_CHANNEL_H
104b7a333eSShuo Chen
114b7a333eSShuo Chen#include <boost/function.hpp>
124b7a333eSShuo Chen#include <boost/noncopyable.hpp>
134b7a333eSShuo Chen
144b7a333eSShuo Chennamespace muduo
154b7a333eSShuo Chen{
164b7a333eSShuo Chen
174b7a333eSShuo Chenclass EventLoop;
184b7a333eSShuo Chen
194b7a333eSShuo Chen///
204b7a333eSShuo Chen/// A selectable I/O channel.
214b7a333eSShuo Chen///
224b7a333eSShuo Chen/// This class doesn't own the file descriptor.
234b7a333eSShuo Chen/// The file descriptor could be a socket,
244b7a333eSShuo Chen/// an eventfd, a timerfd, or a signalfd
254b7a333eSShuo Chenclass Channel : boost::noncopyable
264b7a333eSShuo Chen{
274b7a333eSShuo Chen public:
284b7a333eSShuo Chen  typedef boost::function<void()> EventCallback;
294b7a333eSShuo Chen
304b7a333eSShuo Chen  Channel(EventLoop* loop, int fd);
314b7a333eSShuo Chen
324b7a333eSShuo Chen  void handleEvent();
334b7a333eSShuo Chen  void setReadCallback(const EventCallback& cb)
344b7a333eSShuo Chen  { readCallback_ = cb; }
354b7a333eSShuo Chen  void setWriteCallback(const EventCallback& cb)
364b7a333eSShuo Chen  { writeCallback_ = cb; }
374b7a333eSShuo Chen  void setErrorCallback(const EventCallback& cb)
384b7a333eSShuo Chen  { errorCallback_ = cb; }
394b7a333eSShuo Chen
404b7a333eSShuo Chen  int fd() const { return fd_; }
414b7a333eSShuo Chen  int events() const { return events_; }
424b7a333eSShuo Chen  void set_revents(int revt) { revents_ = revt; }
430615e80eSShuo Chen  bool isNoneEvent() const { return events_ == kNoneEvent; }
444b7a333eSShuo Chen
454b7a333eSShuo Chen  void enableReading() { events_ |= kReadEvent; update(); }
464b7a333eSShuo Chen  // void enableWriting() { events_ |= kWriteEvent; update(); }
474b7a333eSShuo Chen  // void disableWriting() { events_ &= ~kWriteEvent; update(); }
484b7a333eSShuo Chen  // void disableAll() { events_ = kNoneEvent; update(); }
494b7a333eSShuo Chen
504b7a333eSShuo Chen  // for Poller
514b7a333eSShuo Chen  int index() { return index_; }
524b7a333eSShuo Chen  void set_index(int idx) { index_ = idx; }
534b7a333eSShuo Chen
544b7a333eSShuo Chen  EventLoop* ownerLoop() { return loop_; }
554b7a333eSShuo Chen
564b7a333eSShuo Chen private:
574b7a333eSShuo Chen  void update();
584b7a333eSShuo Chen
590615e80eSShuo Chen  static const int kNoneEvent;
604b7a333eSShuo Chen  static const int kReadEvent;
614b7a333eSShuo Chen  static const int kWriteEvent;
624b7a333eSShuo Chen
634b7a333eSShuo Chen  EventLoop* loop_;
644b7a333eSShuo Chen  const int  fd_;
654b7a333eSShuo Chen  int        events_;
664b7a333eSShuo Chen  int        revents_;
674b7a333eSShuo Chen  int        index_; // used by Poller.
684b7a333eSShuo Chen
694b7a333eSShuo Chen  EventCallback readCallback_;
704b7a333eSShuo Chen  EventCallback writeCallback_;
714b7a333eSShuo Chen  EventCallback errorCallback_;
724b7a333eSShuo Chen};
734b7a333eSShuo Chen
744b7a333eSShuo Chen}
754b7a333eSShuo Chen#endif  // MUDUO_NET_CHANNEL_H
76