165c497a3SShuo Chen// excerpts from http://code.google.com/p/muduo/ 265c497a3SShuo Chen// 365c497a3SShuo Chen// Use of this source code is governed by a BSD-style license 465c497a3SShuo Chen// that can be found in the License file. 565c497a3SShuo Chen// 665c497a3SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 765c497a3SShuo Chen 865c497a3SShuo Chen#ifndef MUDUO_NET_POLLER_H 965c497a3SShuo Chen#define MUDUO_NET_POLLER_H 1065c497a3SShuo Chen 1165c497a3SShuo Chen#include <map> 1265c497a3SShuo Chen#include <vector> 1365c497a3SShuo Chen 1465c497a3SShuo Chen#include "datetime/Timestamp.h" 1565c497a3SShuo Chen#include "EventLoop.h" 1665c497a3SShuo Chen 1765c497a3SShuo Chenstruct pollfd; 1865c497a3SShuo Chen 1965c497a3SShuo Chennamespace muduo 2065c497a3SShuo Chen{ 2165c497a3SShuo Chen 2265c497a3SShuo Chenclass Channel; 2365c497a3SShuo Chen 2465c497a3SShuo Chen/// 2565c497a3SShuo Chen/// IO Multiplexing with poll(2). 2665c497a3SShuo Chen/// 2765c497a3SShuo Chen/// This class doesn't own the Channel objects. 2865c497a3SShuo Chenclass Poller : boost::noncopyable 2965c497a3SShuo Chen{ 3065c497a3SShuo Chen public: 3165c497a3SShuo Chen typedef std::vector<Channel*> ChannelList; 3265c497a3SShuo Chen 3365c497a3SShuo Chen Poller(EventLoop* loop); 3465c497a3SShuo Chen ~Poller(); 3565c497a3SShuo Chen 3665c497a3SShuo Chen /// Polls the I/O events. 3765c497a3SShuo Chen /// Must be called in the loop thread. 3865c497a3SShuo Chen Timestamp poll(int timeoutMs, ChannelList* activeChannels); 3965c497a3SShuo Chen 4065c497a3SShuo Chen /// Changes the interested I/O events. 4165c497a3SShuo Chen /// Must be called in the loop thread. 4265c497a3SShuo Chen void updateChannel(Channel* channel); 4365c497a3SShuo Chen /// Remove the channel, when it destructs. 4465c497a3SShuo Chen /// Must be called in the loop thread. 4565c497a3SShuo Chen void removeChannel(Channel* channel); 4665c497a3SShuo Chen 4765c497a3SShuo Chen void assertInLoopThread() { ownerLoop_->assertInLoopThread(); } 4865c497a3SShuo Chen 4965c497a3SShuo Chen private: 5065c497a3SShuo Chen void fillActiveChannels(int numEvents, 5165c497a3SShuo Chen ChannelList* activeChannels) const; 5265c497a3SShuo Chen 5365c497a3SShuo Chen typedef std::vector<struct pollfd> PollFdList; 5465c497a3SShuo Chen typedef std::map<int, Channel*> ChannelMap; 5565c497a3SShuo Chen 5665c497a3SShuo Chen EventLoop* ownerLoop_; 5765c497a3SShuo Chen PollFdList pollfds_; 5865c497a3SShuo Chen ChannelMap channels_; 5965c497a3SShuo Chen}; 6065c497a3SShuo Chen 6165c497a3SShuo Chen} 6265c497a3SShuo Chen#endif // MUDUO_NET_POLLER_H 63