13b467340SShuo Chen // excerpts from http://code.google.com/p/muduo/
23b467340SShuo Chen //
33b467340SShuo Chen // Use of this source code is governed by a BSD-style license
43b467340SShuo Chen // that can be found in the License file.
53b467340SShuo Chen //
63b467340SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
73b467340SShuo Chen 
83b467340SShuo Chen #ifndef MUDUO_NET_CHANNEL_H
93b467340SShuo Chen #define MUDUO_NET_CHANNEL_H
103b467340SShuo Chen 
113b467340SShuo Chen #include <boost/function.hpp>
123b467340SShuo Chen #include <boost/noncopyable.hpp>
133b467340SShuo Chen 
143b467340SShuo Chen+#include <datetime/Timestamp.h>
153b467340SShuo Chen+
163b467340SShuo Chen namespace muduo
173b467340SShuo Chen {
183b467340SShuo Chen 
193b467340SShuo Chen class EventLoop;
203b467340SShuo Chen 
213b467340SShuo Chen ///
223b467340SShuo Chen /// A selectable I/O channel.
233b467340SShuo Chen ///
243b467340SShuo Chen /// This class doesn't own the file descriptor.
253b467340SShuo Chen /// The file descriptor could be a socket,
263b467340SShuo Chen /// an eventfd, a timerfd, or a signalfd
273b467340SShuo Chen class Channel : boost::noncopyable
283b467340SShuo Chen {
293b467340SShuo Chen  public:
303b467340SShuo Chen   typedef boost::function<void()> EventCallback;
313b467340SShuo Chen+  typedef boost::function<void(Timestamp)> ReadEventCallback;
323b467340SShuo Chen 
333b467340SShuo Chen   Channel(EventLoop* loop, int fd);
343b467340SShuo Chen   ~Channel();
353b467340SShuo Chen 
363b467340SShuo Chen!  void handleEvent(Timestamp receiveTime);
373b467340SShuo Chen!  void setReadCallback(const ReadEventCallback& cb)
383b467340SShuo Chen   { readCallback_ = cb; }
393b467340SShuo Chen   void setWriteCallback(const EventCallback& cb)
403b467340SShuo Chen   { writeCallback_ = cb; }
413b467340SShuo Chen   void setErrorCallback(const EventCallback& cb)
423b467340SShuo Chen   { errorCallback_ = cb; }
433b467340SShuo Chen   void setCloseCallback(const EventCallback& cb)
443b467340SShuo Chen   { closeCallback_ = cb; }
453b467340SShuo Chen 
463b467340SShuo Chen   int fd() const { return fd_; }
473b467340SShuo Chen   int events() const { return events_; }
483b467340SShuo Chen   void set_revents(int revt) { revents_ = revt; }
493b467340SShuo Chen   bool isNoneEvent() const { return events_ == kNoneEvent; }
503b467340SShuo Chen 
513b467340SShuo Chen   void enableReading() { events_ |= kReadEvent; update(); }
523b467340SShuo Chen   // void enableWriting() { events_ |= kWriteEvent; update(); }
533b467340SShuo Chen   // void disableWriting() { events_ &= ~kWriteEvent; update(); }
543b467340SShuo Chen   void disableAll() { events_ = kNoneEvent; update(); }
553b467340SShuo Chen 
563b467340SShuo Chen   // for Poller
573b467340SShuo Chen   int index() { return index_; }
583b467340SShuo Chen   void set_index(int idx) { index_ = idx; }
593b467340SShuo Chen 
603b467340SShuo Chen   EventLoop* ownerLoop() { return loop_; }
613b467340SShuo Chen 
623b467340SShuo Chen  private:
633b467340SShuo Chen   void update();
643b467340SShuo Chen 
653b467340SShuo Chen   static const int kNoneEvent;
663b467340SShuo Chen   static const int kReadEvent;
673b467340SShuo Chen   static const int kWriteEvent;
683b467340SShuo Chen 
693b467340SShuo Chen   EventLoop* loop_;
703b467340SShuo Chen   const int  fd_;
713b467340SShuo Chen   int        events_;
723b467340SShuo Chen   int        revents_;
733b467340SShuo Chen   int        index_; // used by Poller.
743b467340SShuo Chen 
753b467340SShuo Chen   bool eventHandling_;
763b467340SShuo Chen 
773b467340SShuo Chen!  ReadEventCallback readCallback_;
783b467340SShuo Chen   EventCallback writeCallback_;
793b467340SShuo Chen   EventCallback errorCallback_;
803b467340SShuo Chen   EventCallback closeCallback_;
813b467340SShuo Chen };
823b467340SShuo Chen 
833b467340SShuo Chen }
843b467340SShuo Chen #endif  // MUDUO_NET_CHANNEL_H
85