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