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