140161064SShuo Chen// excerpts from http://code.google.com/p/muduo/ 240161064SShuo Chen// 340161064SShuo Chen// Use of this source code is governed by a BSD-style license 440161064SShuo Chen// that can be found in the License file. 540161064SShuo Chen// 640161064SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 740161064SShuo Chen 840161064SShuo Chen#ifndef MUDUO_NET_POLLER_H 940161064SShuo Chen#define MUDUO_NET_POLLER_H 1040161064SShuo Chen 1140161064SShuo Chen#include <map> 1240161064SShuo Chen#include <vector> 1340161064SShuo Chen 1440161064SShuo Chen#include "datetime/Timestamp.h" 1540161064SShuo Chen#include "EventLoop.h" 1640161064SShuo Chen 1740161064SShuo Chenstruct pollfd; 1840161064SShuo Chen 1940161064SShuo Chennamespace muduo 2040161064SShuo Chen{ 2140161064SShuo Chen 2240161064SShuo Chenclass Channel; 2340161064SShuo Chen 2440161064SShuo Chen/// 2540161064SShuo Chen/// IO Multiplexing with poll(2). 2640161064SShuo Chen/// 2740161064SShuo Chen/// This class doesn't own the Channel objects. 2840161064SShuo Chenclass Poller : boost::noncopyable 2940161064SShuo Chen{ 3040161064SShuo Chen public: 3140161064SShuo Chen typedef std::vector<Channel*> ChannelList; 3240161064SShuo Chen 3340161064SShuo Chen Poller(EventLoop* loop); 3440161064SShuo Chen ~Poller(); 3540161064SShuo Chen 3640161064SShuo Chen /// Polls the I/O events. 3740161064SShuo Chen /// Must be called in the loop thread. 3840161064SShuo Chen Timestamp poll(int timeoutMs, ChannelList* activeChannels); 3940161064SShuo Chen 4040161064SShuo Chen /// Changes the interested I/O events. 4140161064SShuo Chen /// Must be called in the loop thread. 4240161064SShuo Chen void updateChannel(Channel* channel); 4340161064SShuo Chen /// Remove the channel, when it destructs. 4440161064SShuo Chen /// Must be called in the loop thread. 4540161064SShuo Chen void removeChannel(Channel* channel); 4640161064SShuo Chen 4740161064SShuo Chen void assertInLoopThread() { ownerLoop_->assertInLoopThread(); } 4840161064SShuo Chen 4940161064SShuo Chen private: 5040161064SShuo Chen void fillActiveChannels(int numEvents, 5140161064SShuo Chen ChannelList* activeChannels) const; 5240161064SShuo Chen 5340161064SShuo Chen typedef std::vector<struct pollfd> PollFdList; 5440161064SShuo Chen typedef std::map<int, Channel*> ChannelMap; 5540161064SShuo Chen 5640161064SShuo Chen EventLoop* ownerLoop_; 5740161064SShuo Chen PollFdList pollfds_; 5840161064SShuo Chen ChannelMap channels_; 5940161064SShuo Chen}; 6040161064SShuo Chen 6140161064SShuo Chen} 6240161064SShuo Chen#endif // MUDUO_NET_POLLER_H 63