1cbe8e7d0SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2cbe8e7d0SShuo Chen// 3cbe8e7d0SShuo Chen// Use of this source code is governed by a BSD-style license 4cbe8e7d0SShuo Chen// that can be found in the License file. 5cbe8e7d0SShuo Chen// 6cbe8e7d0SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7cbe8e7d0SShuo Chen 8cbe8e7d0SShuo Chen#ifndef MUDUO_NET_POLLER_H 9cbe8e7d0SShuo Chen#define MUDUO_NET_POLLER_H 10cbe8e7d0SShuo Chen 11cbe8e7d0SShuo Chen#include <map> 12cbe8e7d0SShuo Chen#include <vector> 13cbe8e7d0SShuo Chen 14cbe8e7d0SShuo Chen#include "datetime/Timestamp.h" 15cbe8e7d0SShuo Chen#include "EventLoop.h" 16cbe8e7d0SShuo Chen 17cbe8e7d0SShuo Chenstruct pollfd; 18cbe8e7d0SShuo Chen 19cbe8e7d0SShuo Chennamespace muduo 20cbe8e7d0SShuo Chen{ 21cbe8e7d0SShuo Chen 22cbe8e7d0SShuo Chenclass Channel; 23cbe8e7d0SShuo Chen 24cbe8e7d0SShuo Chen/// 25cbe8e7d0SShuo Chen/// IO Multiplexing with poll(2). 26cbe8e7d0SShuo Chen/// 27cbe8e7d0SShuo Chen/// This class doesn't own the Channel objects. 28cbe8e7d0SShuo Chenclass Poller : boost::noncopyable 29cbe8e7d0SShuo Chen{ 30cbe8e7d0SShuo Chen public: 31cbe8e7d0SShuo Chen typedef std::vector<Channel*> ChannelList; 32cbe8e7d0SShuo Chen 33cbe8e7d0SShuo Chen Poller(EventLoop* loop); 34cbe8e7d0SShuo Chen ~Poller(); 35cbe8e7d0SShuo Chen 36cbe8e7d0SShuo Chen /// Polls the I/O events. 37cbe8e7d0SShuo Chen /// Must be called in the loop thread. 38cbe8e7d0SShuo Chen Timestamp poll(int timeoutMs, ChannelList* activeChannels); 39cbe8e7d0SShuo Chen 40cbe8e7d0SShuo Chen /// Changes the interested I/O events. 41cbe8e7d0SShuo Chen /// Must be called in the loop thread. 42cbe8e7d0SShuo Chen void updateChannel(Channel* channel); 43cbe8e7d0SShuo Chen 440615e80eSShuo Chen void assertInLoopThread() { ownerLoop_->assertInLoopThread(); } 45cbe8e7d0SShuo Chen 46cbe8e7d0SShuo Chen private: 47cbe8e7d0SShuo Chen void fillActiveChannels(int numEvents, 48cbe8e7d0SShuo Chen ChannelList* activeChannels) const; 49cbe8e7d0SShuo Chen 50cbe8e7d0SShuo Chen typedef std::vector<struct pollfd> PollFdList; 51cbe8e7d0SShuo Chen typedef std::map<int, Channel*> ChannelMap; 52cbe8e7d0SShuo Chen 53cbe8e7d0SShuo Chen EventLoop* ownerLoop_; 54cbe8e7d0SShuo Chen PollFdList pollfds_; 55cbe8e7d0SShuo Chen ChannelMap channels_; 56cbe8e7d0SShuo Chen}; 57cbe8e7d0SShuo Chen 58cbe8e7d0SShuo Chen} 59cbe8e7d0SShuo Chen#endif // MUDUO_NET_POLLER_H 60