11bc8a9dfSShuo Chen // excerpts from http://code.google.com/p/muduo/ 21bc8a9dfSShuo Chen // 31bc8a9dfSShuo Chen // Use of this source code is governed by a BSD-style license 41bc8a9dfSShuo Chen // that can be found in the License file. 51bc8a9dfSShuo Chen // 61bc8a9dfSShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com) 71bc8a9dfSShuo Chen 81bc8a9dfSShuo Chen #ifndef MUDUO_NET_CHANNEL_H 91bc8a9dfSShuo Chen #define MUDUO_NET_CHANNEL_H 101bc8a9dfSShuo Chen 111bc8a9dfSShuo Chen #include <boost/function.hpp> 121bc8a9dfSShuo Chen #include <boost/noncopyable.hpp> 131bc8a9dfSShuo Chen 141bc8a9dfSShuo Chen #include <datetime/Timestamp.h> 151bc8a9dfSShuo Chen 161bc8a9dfSShuo Chen namespace muduo 171bc8a9dfSShuo Chen { 181bc8a9dfSShuo Chen 191bc8a9dfSShuo Chen class EventLoop; 201bc8a9dfSShuo Chen 211bc8a9dfSShuo Chen /// 221bc8a9dfSShuo Chen /// A selectable I/O channel. 231bc8a9dfSShuo Chen /// 241bc8a9dfSShuo Chen /// This class doesn't own the file descriptor. 251bc8a9dfSShuo Chen /// The file descriptor could be a socket, 261bc8a9dfSShuo Chen /// an eventfd, a timerfd, or a signalfd 271bc8a9dfSShuo Chen class Channel : boost::noncopyable 281bc8a9dfSShuo Chen { 291bc8a9dfSShuo Chen public: 301bc8a9dfSShuo Chen typedef boost::function<void()> EventCallback; 311bc8a9dfSShuo Chen typedef boost::function<void(Timestamp)> ReadEventCallback; 321bc8a9dfSShuo Chen 331bc8a9dfSShuo Chen Channel(EventLoop* loop, int fd); 341bc8a9dfSShuo Chen ~Channel(); 351bc8a9dfSShuo Chen 361bc8a9dfSShuo Chen void handleEvent(Timestamp receiveTime); 371bc8a9dfSShuo Chen void setReadCallback(const ReadEventCallback& cb) 381bc8a9dfSShuo Chen { readCallback_ = cb; } 391bc8a9dfSShuo Chen void setWriteCallback(const EventCallback& cb) 401bc8a9dfSShuo Chen { writeCallback_ = cb; } 411bc8a9dfSShuo Chen void setErrorCallback(const EventCallback& cb) 421bc8a9dfSShuo Chen { errorCallback_ = cb; } 431bc8a9dfSShuo Chen void setCloseCallback(const EventCallback& cb) 441bc8a9dfSShuo Chen { closeCallback_ = cb; } 451bc8a9dfSShuo Chen 461bc8a9dfSShuo Chen int fd() const { return fd_; } 471bc8a9dfSShuo Chen int events() const { return events_; } 481bc8a9dfSShuo Chen void set_revents(int revt) { revents_ = revt; } 491bc8a9dfSShuo Chen bool isNoneEvent() const { return events_ == kNoneEvent; } 501bc8a9dfSShuo Chen 511bc8a9dfSShuo Chen void enableReading() { events_ |= kReadEvent; update(); } 521bc8a9dfSShuo Chen! void enableWriting() { events_ |= kWriteEvent; update(); } 531bc8a9dfSShuo Chen! void disableWriting() { events_ &= ~kWriteEvent; update(); } 541bc8a9dfSShuo Chen void disableAll() { events_ = kNoneEvent; update(); } 551bc8a9dfSShuo Chen+ bool isWriting() const { return events_ & kWriteEvent; } 561bc8a9dfSShuo Chen 571bc8a9dfSShuo Chen // for Poller 581bc8a9dfSShuo Chen int index() { return index_; } 591bc8a9dfSShuo Chen void set_index(int idx) { index_ = idx; } 601bc8a9dfSShuo Chen 611bc8a9dfSShuo Chen EventLoop* ownerLoop() { return loop_; } 621bc8a9dfSShuo Chen 631bc8a9dfSShuo Chen private: 641bc8a9dfSShuo Chen void update(); 651bc8a9dfSShuo Chen 661bc8a9dfSShuo Chen static const int kNoneEvent; 671bc8a9dfSShuo Chen static const int kReadEvent; 681bc8a9dfSShuo Chen static const int kWriteEvent; 691bc8a9dfSShuo Chen 701bc8a9dfSShuo Chen EventLoop* loop_; 711bc8a9dfSShuo Chen const int fd_; 721bc8a9dfSShuo Chen int events_; 731bc8a9dfSShuo Chen int revents_; 741bc8a9dfSShuo Chen int index_; // used by Poller. 751bc8a9dfSShuo Chen 761bc8a9dfSShuo Chen bool eventHandling_; 771bc8a9dfSShuo Chen 781bc8a9dfSShuo Chen ReadEventCallback readCallback_; 791bc8a9dfSShuo Chen EventCallback writeCallback_; 801bc8a9dfSShuo Chen EventCallback errorCallback_; 811bc8a9dfSShuo Chen EventCallback closeCallback_; 821bc8a9dfSShuo Chen }; 831bc8a9dfSShuo Chen 841bc8a9dfSShuo Chen } 851bc8a9dfSShuo Chen #endif // MUDUO_NET_CHANNEL_H 86