]> granicus.if.org Git - icinga2/blob - lib/base/socketevents.hpp
Remove unused includes
[icinga2] / lib / base / socketevents.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef SOCKETEVENTS_H
21 #define SOCKETEVENTS_H
22
23 #include "base/i2-base.hpp"
24 #include "base/socket.hpp"
25 #include <boost/thread/condition_variable.hpp>
26 #include <thread>
27
28 #ifndef _WIN32
29 #       include <poll.h>
30 #endif /* _WIN32 */
31
32 namespace icinga
33 {
34
35 /**
36  * Socket event interface
37  *
38  * @ingroup base
39  */
40 class SocketEvents
41 {
42 public:
43         ~SocketEvents();
44
45         virtual void OnEvent(int revents);
46
47         void Unregister();
48
49         void ChangeEvents(int events);
50
51         bool IsHandlingEvents() const;
52
53         void *GetEnginePrivate() const;
54         void SetEnginePrivate(void *priv);
55
56 protected:
57         SocketEvents(const Socket::Ptr& socket, Object *lifesupportObject);
58
59 private:
60         int m_ID;
61         SOCKET m_FD;
62         bool m_Events;
63         void *m_EnginePrivate;
64
65         static int m_NextID;
66
67         static void InitializeEngine();
68
69         void WakeUpThread(bool wait = false);
70
71         void Register(Object *lifesupportObject);
72
73         friend class SocketEventEnginePoll;
74         friend class SocketEventEngineEpoll;
75 };
76
77 #define SOCKET_IOTHREADS 8
78
79 struct SocketEventDescriptor
80 {
81         int Events{POLLIN};
82         SocketEvents *EventInterface{nullptr};
83         Object *LifesupportObject{nullptr};
84 };
85
86 struct EventDescription
87 {
88         int REvents;
89         SocketEventDescriptor Descriptor;
90         Object::Ptr LifesupportReference;
91 };
92
93 class SocketEventEngine
94 {
95 public:
96         void Start();
97
98         void WakeUpThread(int sid, bool wait);
99
100         boost::mutex& GetMutex(int tid);
101
102 protected:
103         virtual void InitializeThread(int tid) = 0;
104         virtual void ThreadProc(int tid) = 0;
105         virtual void Register(SocketEvents *se, Object *lifesupportObject) = 0;
106         virtual void Unregister(SocketEvents *se) = 0;
107         virtual void ChangeEvents(SocketEvents *se, int events) = 0;
108
109         std::thread m_Threads[SOCKET_IOTHREADS];
110         SOCKET m_EventFDs[SOCKET_IOTHREADS][2];
111         bool m_FDChanged[SOCKET_IOTHREADS];
112         boost::mutex m_EventMutex[SOCKET_IOTHREADS];
113         boost::condition_variable m_CV[SOCKET_IOTHREADS];
114         std::map<SOCKET, SocketEventDescriptor> m_Sockets[SOCKET_IOTHREADS];
115
116         friend class SocketEvents;
117 };
118
119 class SocketEventEnginePoll final : public SocketEventEngine
120 {
121 public:
122         void Register(SocketEvents *se, Object *lifesupportObject) override;
123         void Unregister(SocketEvents *se) override;
124         void ChangeEvents(SocketEvents *se, int events) override;
125
126 protected:
127         void InitializeThread(int tid) override;
128         void ThreadProc(int tid) override;
129 };
130
131 #ifdef __linux__
132 class SocketEventEngineEpoll : public SocketEventEngine
133 {
134 public:
135         virtual void Register(SocketEvents *se, Object *lifesupportObject);
136         virtual void Unregister(SocketEvents *se);
137         virtual void ChangeEvents(SocketEvents *se, int events);
138
139 protected:
140         virtual void InitializeThread(int tid);
141         virtual void ThreadProc(int tid);
142
143 private:
144         SOCKET m_PollFDs[SOCKET_IOTHREADS];
145
146         static int PollToEpoll(int events);
147         static int EpollToPoll(int events);
148 };
149 #endif /* __linux__ */
150
151 }
152
153 #endif /* SOCKETEVENTS_H */