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