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