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