Channel.h revision 0615e80e
12745a763SShuo Chen// excerpts from http://code.google.com/p/muduo/
22745a763SShuo Chen//
32745a763SShuo Chen// Use of this source code is governed by a BSD-style license
42745a763SShuo Chen// that can be found in the License file.
52745a763SShuo Chen//
62745a763SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
72745a763SShuo Chen
82745a763SShuo Chen#ifndef MUDUO_NET_CHANNEL_H
92745a763SShuo Chen#define MUDUO_NET_CHANNEL_H
102745a763SShuo Chen
112745a763SShuo Chen#include <boost/function.hpp>
122745a763SShuo Chen#include <boost/noncopyable.hpp>
132745a763SShuo Chen
142745a763SShuo Chennamespace muduo
152745a763SShuo Chen{
162745a763SShuo Chen
172745a763SShuo Chenclass EventLoop;
182745a763SShuo Chen
192745a763SShuo Chen///
202745a763SShuo Chen/// A selectable I/O channel.
212745a763SShuo Chen///
222745a763SShuo Chen/// This class doesn't own the file descriptor.
232745a763SShuo Chen/// The file descriptor could be a socket,
242745a763SShuo Chen/// an eventfd, a timerfd, or a signalfd
252745a763SShuo Chenclass Channel : boost::noncopyable
262745a763SShuo Chen{
272745a763SShuo Chen public:
282745a763SShuo Chen  typedef boost::function<void()> EventCallback;
292745a763SShuo Chen
302745a763SShuo Chen  Channel(EventLoop* loop, int fd);
312745a763SShuo Chen
322745a763SShuo Chen  void handleEvent();
332745a763SShuo Chen  void setReadCallback(const EventCallback& cb)
342745a763SShuo Chen  { readCallback_ = cb; }
352745a763SShuo Chen  void setWriteCallback(const EventCallback& cb)
362745a763SShuo Chen  { writeCallback_ = cb; }
372745a763SShuo Chen  void setErrorCallback(const EventCallback& cb)
382745a763SShuo Chen  { errorCallback_ = cb; }
392745a763SShuo Chen
402745a763SShuo Chen  int fd() const { return fd_; }
412745a763SShuo Chen  int events() const { return events_; }
422745a763SShuo Chen  void set_revents(int revt) { revents_ = revt; }
430615e80eSShuo Chen  bool isNoneEvent() const { return events_ == kNoneEvent; }
442745a763SShuo Chen
452745a763SShuo Chen  void enableReading() { events_ |= kReadEvent; update(); }
462745a763SShuo Chen  // void enableWriting() { events_ |= kWriteEvent; update(); }
472745a763SShuo Chen  // void disableWriting() { events_ &= ~kWriteEvent; update(); }
482745a763SShuo Chen  // void disableAll() { events_ = kNoneEvent; update(); }
492745a763SShuo Chen
502745a763SShuo Chen  // for Poller
512745a763SShuo Chen  int index() { return index_; }
522745a763SShuo Chen  void set_index(int idx) { index_ = idx; }
532745a763SShuo Chen
542745a763SShuo Chen  EventLoop* ownerLoop() { return loop_; }
552745a763SShuo Chen
562745a763SShuo Chen private:
572745a763SShuo Chen  void update();
582745a763SShuo Chen
590615e80eSShuo Chen  static const int kNoneEvent;
602745a763SShuo Chen  static const int kReadEvent;
612745a763SShuo Chen  static const int kWriteEvent;
622745a763SShuo Chen
632745a763SShuo Chen  EventLoop* loop_;
642745a763SShuo Chen  const int  fd_;
652745a763SShuo Chen  int        events_;
662745a763SShuo Chen  int        revents_;
672745a763SShuo Chen  int        index_; // used by Poller.
682745a763SShuo Chen
692745a763SShuo Chen  EventCallback readCallback_;
702745a763SShuo Chen  EventCallback writeCallback_;
712745a763SShuo Chen  EventCallback errorCallback_;
722745a763SShuo Chen};
732745a763SShuo Chen
742745a763SShuo Chen}
752745a763SShuo Chen#endif  // MUDUO_NET_CHANNEL_H
76