Channel.h revision 714cd85f
165c497a3SShuo Chen// excerpts from http://code.google.com/p/muduo/
265c497a3SShuo Chen//
365c497a3SShuo Chen// Use of this source code is governed by a BSD-style license
465c497a3SShuo Chen// that can be found in the License file.
565c497a3SShuo Chen//
665c497a3SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
765c497a3SShuo Chen
865c497a3SShuo Chen#ifndef MUDUO_NET_CHANNEL_H
965c497a3SShuo Chen#define MUDUO_NET_CHANNEL_H
1065c497a3SShuo Chen
1165c497a3SShuo Chen#include <boost/function.hpp>
1265c497a3SShuo Chen#include <boost/noncopyable.hpp>
1365c497a3SShuo Chen
14714cd85fSShuo Chen#include <datetime/Timestamp.h>
15714cd85fSShuo Chen
1665c497a3SShuo Chennamespace muduo
1765c497a3SShuo Chen{
1865c497a3SShuo Chen
1965c497a3SShuo Chenclass EventLoop;
2065c497a3SShuo Chen
2165c497a3SShuo Chen///
2265c497a3SShuo Chen/// A selectable I/O channel.
2365c497a3SShuo Chen///
2465c497a3SShuo Chen/// This class doesn't own the file descriptor.
2565c497a3SShuo Chen/// The file descriptor could be a socket,
2665c497a3SShuo Chen/// an eventfd, a timerfd, or a signalfd
2765c497a3SShuo Chenclass Channel : boost::noncopyable
2865c497a3SShuo Chen{
2965c497a3SShuo Chen public:
3065c497a3SShuo Chen  typedef boost::function<void()> EventCallback;
31714cd85fSShuo Chen  typedef boost::function<void(Timestamp)> ReadEventCallback;
3265c497a3SShuo Chen
3365c497a3SShuo Chen  Channel(EventLoop* loop, int fd);
3465c497a3SShuo Chen  ~Channel();
3565c497a3SShuo Chen
36714cd85fSShuo Chen  void handleEvent(Timestamp receiveTime);
37714cd85fSShuo Chen  void setReadCallback(const ReadEventCallback& cb)
3865c497a3SShuo Chen  { readCallback_ = cb; }
3965c497a3SShuo Chen  void setWriteCallback(const EventCallback& cb)
4065c497a3SShuo Chen  { writeCallback_ = cb; }
4165c497a3SShuo Chen  void setErrorCallback(const EventCallback& cb)
4265c497a3SShuo Chen  { errorCallback_ = cb; }
4365c497a3SShuo Chen  void setCloseCallback(const EventCallback& cb)
4465c497a3SShuo Chen  { closeCallback_ = cb; }
4565c497a3SShuo Chen
4665c497a3SShuo Chen  int fd() const { return fd_; }
4765c497a3SShuo Chen  int events() const { return events_; }
4865c497a3SShuo Chen  void set_revents(int revt) { revents_ = revt; }
4965c497a3SShuo Chen  bool isNoneEvent() const { return events_ == kNoneEvent; }
5065c497a3SShuo Chen
5165c497a3SShuo Chen  void enableReading() { events_ |= kReadEvent; update(); }
5265c497a3SShuo Chen  // void enableWriting() { events_ |= kWriteEvent; update(); }
5365c497a3SShuo Chen  // void disableWriting() { events_ &= ~kWriteEvent; update(); }
5465c497a3SShuo Chen  void disableAll() { events_ = kNoneEvent; update(); }
5565c497a3SShuo Chen
5665c497a3SShuo Chen  // for Poller
5765c497a3SShuo Chen  int index() { return index_; }
5865c497a3SShuo Chen  void set_index(int idx) { index_ = idx; }
5965c497a3SShuo Chen
6065c497a3SShuo Chen  EventLoop* ownerLoop() { return loop_; }
6165c497a3SShuo Chen
6265c497a3SShuo Chen private:
6365c497a3SShuo Chen  void update();
6465c497a3SShuo Chen
6565c497a3SShuo Chen  static const int kNoneEvent;
6665c497a3SShuo Chen  static const int kReadEvent;
6765c497a3SShuo Chen  static const int kWriteEvent;
6865c497a3SShuo Chen
6965c497a3SShuo Chen  EventLoop* loop_;
7065c497a3SShuo Chen  const int  fd_;
7165c497a3SShuo Chen  int        events_;
7265c497a3SShuo Chen  int        revents_;
7365c497a3SShuo Chen  int        index_; // used by Poller.
7465c497a3SShuo Chen
7565c497a3SShuo Chen  bool eventHandling_;
7665c497a3SShuo Chen
77714cd85fSShuo Chen  ReadEventCallback readCallback_;
7865c497a3SShuo Chen  EventCallback writeCallback_;
7965c497a3SShuo Chen  EventCallback errorCallback_;
8065c497a3SShuo Chen  EventCallback closeCallback_;
8165c497a3SShuo Chen};
8265c497a3SShuo Chen
8365c497a3SShuo Chen}
8465c497a3SShuo Chen#endif  // MUDUO_NET_CHANNEL_H
85