1354280cfSShuo Chen// excerpts from http://code.google.com/p/muduo/
2354280cfSShuo Chen//
3354280cfSShuo Chen// Use of this source code is governed by a BSD-style license
4354280cfSShuo Chen// that can be found in the License file.
5354280cfSShuo Chen//
6354280cfSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
7354280cfSShuo Chen
8354280cfSShuo Chen#ifndef MUDUO_NET_POLLER_H
9354280cfSShuo Chen#define MUDUO_NET_POLLER_H
10354280cfSShuo Chen
11354280cfSShuo Chen#include <map>
12354280cfSShuo Chen#include <vector>
13354280cfSShuo Chen
14354280cfSShuo Chen#include "datetime/Timestamp.h"
15354280cfSShuo Chen#include "EventLoop.h"
16354280cfSShuo Chen
17354280cfSShuo Chenstruct pollfd;
18354280cfSShuo Chen
19354280cfSShuo Chennamespace muduo
20354280cfSShuo Chen{
21354280cfSShuo Chen
22354280cfSShuo Chenclass Channel;
23354280cfSShuo Chen
24354280cfSShuo Chen///
25354280cfSShuo Chen/// IO Multiplexing with poll(2).
26354280cfSShuo Chen///
27354280cfSShuo Chen/// This class doesn't own the Channel objects.
28354280cfSShuo Chenclass Poller : boost::noncopyable
29354280cfSShuo Chen{
30354280cfSShuo Chen public:
31354280cfSShuo Chen  typedef std::vector<Channel*> ChannelList;
32354280cfSShuo Chen
33354280cfSShuo Chen  Poller(EventLoop* loop);
34354280cfSShuo Chen  ~Poller();
35354280cfSShuo Chen
36354280cfSShuo Chen  /// Polls the I/O events.
37354280cfSShuo Chen  /// Must be called in the loop thread.
38354280cfSShuo Chen  Timestamp poll(int timeoutMs, ChannelList* activeChannels);
39354280cfSShuo Chen
40354280cfSShuo Chen  /// Changes the interested I/O events.
41354280cfSShuo Chen  /// Must be called in the loop thread.
42354280cfSShuo Chen  void updateChannel(Channel* channel);
43354280cfSShuo Chen  /// Remove the channel, when it destructs.
44354280cfSShuo Chen  /// Must be called in the loop thread.
45354280cfSShuo Chen  void removeChannel(Channel* channel);
46354280cfSShuo Chen
47354280cfSShuo Chen  void assertInLoopThread() { ownerLoop_->assertInLoopThread(); }
48354280cfSShuo Chen
49354280cfSShuo Chen private:
50354280cfSShuo Chen  void fillActiveChannels(int numEvents,
51354280cfSShuo Chen                          ChannelList* activeChannels) const;
52354280cfSShuo Chen
53354280cfSShuo Chen  typedef std::vector<struct pollfd> PollFdList;
54354280cfSShuo Chen  typedef std::map<int, Channel*> ChannelMap;
55354280cfSShuo Chen
56354280cfSShuo Chen  EventLoop* ownerLoop_;
57354280cfSShuo Chen  PollFdList pollfds_;
58354280cfSShuo Chen  ChannelMap channels_;
59354280cfSShuo Chen};
60354280cfSShuo Chen
61354280cfSShuo Chen}
62354280cfSShuo Chen#endif  // MUDUO_NET_POLLER_H
63