"Fossies" - the Fresh Open Source Software Archive

Member "icinga2-2.10.5/lib/base/socketevents.hpp" (23 May 2019, 4089 Bytes) of package /linux/misc/icinga2-2.10.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "socketevents.hpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.10.2_vs_2.10.3.

    1 /******************************************************************************
    2  * Icinga 2                                                                   *
    3  * Copyright (C) 2012-2018 Icinga Development Team (https://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 "base/stream.hpp"
   26 #include <boost/thread/condition_variable.hpp>
   27 #include <thread>
   28 
   29 #ifndef _WIN32
   30 #   include <poll.h>
   31 #endif /* _WIN32 */
   32 
   33 namespace icinga
   34 {
   35 
   36 /**
   37  * Socket event interface
   38  *
   39  * @ingroup base
   40  */
   41 class SocketEvents : public Stream
   42 {
   43 public:
   44     DECLARE_PTR_TYPEDEFS(SocketEvents);
   45 
   46     ~SocketEvents();
   47 
   48     virtual void OnEvent(int revents);
   49 
   50     void Unregister();
   51 
   52     void ChangeEvents(int events);
   53 
   54     bool IsHandlingEvents() const;
   55 
   56     void *GetEnginePrivate() const;
   57     void SetEnginePrivate(void *priv);
   58 
   59 protected:
   60     SocketEvents(const Socket::Ptr& socket);
   61 
   62 private:
   63     int m_ID;
   64     SOCKET m_FD;
   65     bool m_Events;
   66     void *m_EnginePrivate;
   67 
   68     static int m_NextID;
   69 
   70     static void InitializeEngine();
   71 
   72     void WakeUpThread(bool wait = false);
   73 
   74     void Register();
   75 
   76     friend class SocketEventEnginePoll;
   77     friend class SocketEventEngineEpoll;
   78 };
   79 
   80 #define SOCKET_IOTHREADS 8
   81 
   82 struct SocketEventDescriptor
   83 {
   84     int Events{POLLIN};
   85     SocketEvents::Ptr EventInterface;
   86 };
   87 
   88 struct EventDescription
   89 {
   90     int REvents;
   91     SocketEventDescriptor Descriptor;
   92 };
   93 
   94 class SocketEventEngine
   95 {
   96 public:
   97     void Start();
   98 
   99     void WakeUpThread(int sid, bool wait);
  100 
  101     boost::mutex& GetMutex(int tid);
  102 
  103 protected:
  104     virtual void InitializeThread(int tid) = 0;
  105     virtual void ThreadProc(int tid) = 0;
  106     virtual void Register(SocketEvents *se) = 0;
  107     virtual void Unregister(SocketEvents *se) = 0;
  108     virtual void ChangeEvents(SocketEvents *se, int events) = 0;
  109 
  110     std::thread m_Threads[SOCKET_IOTHREADS];
  111     SOCKET m_EventFDs[SOCKET_IOTHREADS][2];
  112     bool m_FDChanged[SOCKET_IOTHREADS];
  113     boost::mutex m_EventMutex[SOCKET_IOTHREADS];
  114     boost::condition_variable m_CV[SOCKET_IOTHREADS];
  115     std::map<SOCKET, SocketEventDescriptor> m_Sockets[SOCKET_IOTHREADS];
  116 
  117     friend class SocketEvents;
  118 };
  119 
  120 class SocketEventEnginePoll final : public SocketEventEngine
  121 {
  122 public:
  123     void Register(SocketEvents *se) override;
  124     void Unregister(SocketEvents *se) override;
  125     void ChangeEvents(SocketEvents *se, int events) override;
  126 
  127 protected:
  128     void InitializeThread(int tid) override;
  129     void ThreadProc(int tid) override;
  130 };
  131 
  132 #ifdef __linux__
  133 class SocketEventEngineEpoll : public SocketEventEngine
  134 {
  135 public:
  136     virtual void Register(SocketEvents *se);
  137     virtual void Unregister(SocketEvents *se);
  138     virtual void ChangeEvents(SocketEvents *se, int events);
  139 
  140 protected:
  141     virtual void InitializeThread(int tid);
  142     virtual void ThreadProc(int tid);
  143 
  144 private:
  145     SOCKET m_PollFDs[SOCKET_IOTHREADS];
  146 
  147     static int PollToEpoll(int events);
  148     static int EpollToPoll(int events);
  149 };
  150 #endif /* __linux__ */
  151 
  152 }
  153 
  154 #endif /* SOCKETEVENTS_H */