Open Lighting Architecture  0.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WindowsPoller.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * WindowsPoller.h
17  * A Poller for the Windows platform
18  * Copyright (C) 2014 Lukas Erlinghagen
19  */
20 
21 #ifndef COMMON_IO_WINDOWSPOLLER_H_
22 #define COMMON_IO_WINDOWSPOLLER_H_
23 
24 #include <ola/Clock.h>
25 #include <ola/ExportMap.h>
26 #include <ola/base/Macro.h>
27 #include <ola/io/Descriptor.h>
28 
29 #define WIN32_LEAN_AND_MEAN
30 #include <Windows.h>
31 
32 #include <map>
33 #include <set>
34 
35 #include "common/io/PollerInterface.h"
36 #include "common/io/TimeoutManager.h"
37 
38 namespace ola {
39 namespace io {
40 
46  public :
52  WindowsPoller(ExportMap *export_map, Clock *clock);
53 
54  ~WindowsPoller();
55 
56  bool AddReadDescriptor(class ReadFileDescriptor *descriptor);
57  bool AddReadDescriptor(class ConnectedDescriptor *descriptor,
58  bool delete_on_close);
59  bool RemoveReadDescriptor(class ReadFileDescriptor *descriptor);
60  bool RemoveReadDescriptor(class ConnectedDescriptor *descriptor);
61 
62  bool AddWriteDescriptor(class WriteFileDescriptor *descriptor);
63  bool RemoveWriteDescriptor(class WriteFileDescriptor *descriptor);
64 
65  const TimeStamp *WakeUpTime() const { return &m_wake_up_time; }
66 
67  bool Poll(TimeoutManager *timeout_manager,
68  const TimeInterval &poll_interval);
69 
70  private:
71  typedef struct {
72  ConnectedDescriptor *descriptor;
73  bool delete_on_close;
74  } connected_pipe_descriptor_t;
75 
76  struct connected_pipe_descriptor_t_lt {
77  bool operator()(const connected_pipe_descriptor_t &c1,
78  const connected_pipe_descriptor_t &c2) const {
79  return c1.descriptor->ReadDescriptor().m_handle.m_handle <
80  c2.descriptor->ReadDescriptor().m_handle.m_handle;
81  }
82  };
83 
84  typedef struct {
85  OVERLAPPED m_overlapped;
86  } overlapped_handle_context_t;
87 
88  typedef std::set<connected_pipe_descriptor_t,
89  connected_pipe_descriptor_t_lt>
90  ConnectedPipeDescriptorSet;
91  typedef std::set<ReadFileDescriptor*> SocketDescriptorSet;
92  typedef std::map<void*, overlapped_handle_context_t> OverlappedHandleMap;
93 
94  ExportMap *m_export_map;
95  CounterVariable *m_loop_iterations;
96  CounterVariable *m_loop_time;
97  Clock *m_clock;
98  TimeStamp m_wake_up_time;
99 
100  SocketDescriptorSet m_socket_read_descriptors;
101  ConnectedPipeDescriptorSet m_connected_pipe_read_descriptors;
102  OverlappedHandleMap m_overlapped_handle_map;
103 
104  DISALLOW_COPY_AND_ASSIGN(WindowsPoller);
105 };
106 } // namespace io
107 } // namespace ola
108 #endif // COMMON_IO_WINDOWSPOLLER_H_