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