12a18e699SShuo Chen// excerpts from http://code.google.com/p/muduo/ 22a18e699SShuo Chen// 32a18e699SShuo Chen// Use of this source code is governed by a BSD-style license 42a18e699SShuo Chen// that can be found in the License file. 52a18e699SShuo Chen// 62a18e699SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 72a18e699SShuo Chen 82a18e699SShuo Chen#ifndef MUDUO_NET_POLLER_H 92a18e699SShuo Chen#define MUDUO_NET_POLLER_H 102a18e699SShuo Chen 112a18e699SShuo Chen#include <map> 122a18e699SShuo Chen#include <vector> 132a18e699SShuo Chen 142a18e699SShuo Chen#include "datetime/Timestamp.h" 152a18e699SShuo Chen#include "EventLoop.h" 162a18e699SShuo Chen 172a18e699SShuo Chenstruct pollfd; 182a18e699SShuo Chen 192a18e699SShuo Chennamespace muduo 202a18e699SShuo Chen{ 212a18e699SShuo Chen 222a18e699SShuo Chenclass Channel; 232a18e699SShuo Chen 242a18e699SShuo Chen/// 252a18e699SShuo Chen/// IO Multiplexing with poll(2). 262a18e699SShuo Chen/// 272a18e699SShuo Chen/// This class doesn't own the Channel objects. 282a18e699SShuo Chenclass Poller : boost::noncopyable 292a18e699SShuo Chen{ 302a18e699SShuo Chen public: 312a18e699SShuo Chen typedef std::vector<Channel*> ChannelList; 322a18e699SShuo Chen 332a18e699SShuo Chen Poller(EventLoop* loop); 342a18e699SShuo Chen ~Poller(); 352a18e699SShuo Chen 362a18e699SShuo Chen /// Polls the I/O events. 372a18e699SShuo Chen /// Must be called in the loop thread. 382a18e699SShuo Chen Timestamp poll(int timeoutMs, ChannelList* activeChannels); 392a18e699SShuo Chen 402a18e699SShuo Chen /// Changes the interested I/O events. 412a18e699SShuo Chen /// Must be called in the loop thread. 422a18e699SShuo Chen void updateChannel(Channel* channel); 432a18e699SShuo Chen /// Remove the channel, when it destructs. 442a18e699SShuo Chen /// Must be called in the loop thread. 452a18e699SShuo Chen void removeChannel(Channel* channel); 462a18e699SShuo Chen 472a18e699SShuo Chen void assertInLoopThread() { ownerLoop_->assertInLoopThread(); } 482a18e699SShuo Chen 492a18e699SShuo Chen private: 502a18e699SShuo Chen void fillActiveChannels(int numEvents, 512a18e699SShuo Chen ChannelList* activeChannels) const; 522a18e699SShuo Chen 532a18e699SShuo Chen typedef std::vector<struct pollfd> PollFdList; 542a18e699SShuo Chen typedef std::map<int, Channel*> ChannelMap; 552a18e699SShuo Chen 562a18e699SShuo Chen EventLoop* ownerLoop_; 572a18e699SShuo Chen PollFdList pollfds_; 582a18e699SShuo Chen ChannelMap channels_; 592a18e699SShuo Chen}; 602a18e699SShuo Chen 612a18e699SShuo Chen} 622a18e699SShuo Chen#endif // MUDUO_NET_POLLER_H 63