19a1e991dSShuo Chen// excerpts from http://code.google.com/p/muduo/
29a1e991dSShuo Chen//
39a1e991dSShuo Chen// Use of this source code is governed by a BSD-style license
49a1e991dSShuo Chen// that can be found in the License file.
59a1e991dSShuo Chen//
69a1e991dSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
79a1e991dSShuo Chen
89a1e991dSShuo Chen#ifndef MUDUO_NET_CHANNEL_H
99a1e991dSShuo Chen#define MUDUO_NET_CHANNEL_H
109a1e991dSShuo Chen
119a1e991dSShuo Chen#include <boost/function.hpp>
129a1e991dSShuo Chen#include <boost/noncopyable.hpp>
139a1e991dSShuo Chen
149a1e991dSShuo Chennamespace muduo
159a1e991dSShuo Chen{
169a1e991dSShuo Chen
179a1e991dSShuo Chenclass EventLoop;
189a1e991dSShuo Chen
199a1e991dSShuo Chen///
209a1e991dSShuo Chen/// A selectable I/O channel.
219a1e991dSShuo Chen///
229a1e991dSShuo Chen/// This class doesn't own the file descriptor.
239a1e991dSShuo Chen/// The file descriptor could be a socket,
249a1e991dSShuo Chen/// an eventfd, a timerfd, or a signalfd
259a1e991dSShuo Chenclass Channel : boost::noncopyable
269a1e991dSShuo Chen{
279a1e991dSShuo Chen public:
289a1e991dSShuo Chen  typedef boost::function<void()> EventCallback;
299a1e991dSShuo Chen
309a1e991dSShuo Chen  Channel(EventLoop* loop, int fd);
319a1e991dSShuo Chen
329a1e991dSShuo Chen  void handleEvent();
339a1e991dSShuo Chen  void setReadCallback(const EventCallback& cb)
349a1e991dSShuo Chen  { readCallback_ = cb; }
359a1e991dSShuo Chen  void setWriteCallback(const EventCallback& cb)
369a1e991dSShuo Chen  { writeCallback_ = cb; }
379a1e991dSShuo Chen  void setErrorCallback(const EventCallback& cb)
389a1e991dSShuo Chen  { errorCallback_ = cb; }
399a1e991dSShuo Chen
409a1e991dSShuo Chen  int fd() const { return fd_; }
419a1e991dSShuo Chen  int events() const { return events_; }
429a1e991dSShuo Chen  void set_revents(int revt) { revents_ = revt; }
430615e80eSShuo Chen  bool isNoneEvent() const { return events_ == kNoneEvent; }
449a1e991dSShuo Chen
459a1e991dSShuo Chen  void enableReading() { events_ |= kReadEvent; update(); }
469a1e991dSShuo Chen  // void enableWriting() { events_ |= kWriteEvent; update(); }
479a1e991dSShuo Chen  // void disableWriting() { events_ &= ~kWriteEvent; update(); }
489a1e991dSShuo Chen  // void disableAll() { events_ = kNoneEvent; update(); }
499a1e991dSShuo Chen
509a1e991dSShuo Chen  // for Poller
519a1e991dSShuo Chen  int index() { return index_; }
529a1e991dSShuo Chen  void set_index(int idx) { index_ = idx; }
539a1e991dSShuo Chen
549a1e991dSShuo Chen  EventLoop* ownerLoop() { return loop_; }
559a1e991dSShuo Chen
569a1e991dSShuo Chen private:
579a1e991dSShuo Chen  void update();
589a1e991dSShuo Chen
590615e80eSShuo Chen  static const int kNoneEvent;
609a1e991dSShuo Chen  static const int kReadEvent;
619a1e991dSShuo Chen  static const int kWriteEvent;
629a1e991dSShuo Chen
639a1e991dSShuo Chen  EventLoop* loop_;
649a1e991dSShuo Chen  const int  fd_;
659a1e991dSShuo Chen  int        events_;
669a1e991dSShuo Chen  int        revents_;
679a1e991dSShuo Chen  int        index_; // used by Poller.
689a1e991dSShuo Chen
699a1e991dSShuo Chen  EventCallback readCallback_;
709a1e991dSShuo Chen  EventCallback writeCallback_;
719a1e991dSShuo Chen  EventCallback errorCallback_;
729a1e991dSShuo Chen};
739a1e991dSShuo Chen
749a1e991dSShuo Chen}
759a1e991dSShuo Chen#endif  // MUDUO_NET_CHANNEL_H
76