Line data Source code
1 : // Copyright (c) 2020-2024 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef BITCOIN_UTIL_EDGE_H 6 : #define BITCOIN_UTIL_EDGE_H 7 : 8 : #include <compat/compat.h> 9 : 10 : #include <assert.h> 11 : #include <cstdint> 12 : #include <string> 13 : 14 : enum class SocketEventsMode : int8_t; 15 : 16 : /** 17 : * A manager for abstracting logic surrounding edge-triggered socket events 18 : * modes like kqueue and epoll. 19 : */ 20 : // TODO: simplify this class to 2-3 flags; kick out everything else to Sock/~Sock and inherited classes 21 : class EdgeTriggeredEvents 22 : { 23 : public: 24 : explicit EdgeTriggeredEvents(SocketEventsMode events_mode); 25 : ~EdgeTriggeredEvents(); 26 : 27 2821 : bool IsValid() const { return m_valid; } 28 29784563 : int GetFileDescriptor() const { assert(m_fd != -1); return m_fd; } 29 : 30 : /* Add socket to interest list */ 31 : bool AddSocket(SOCKET socket) const; 32 : /* Remove socket from interest list */ 33 : bool RemoveSocket(SOCKET socket) const; 34 : 35 : /* Register events for socket */ 36 : bool RegisterEvents(SOCKET socket) const; 37 : /* Unregister events for socket */ 38 : bool UnregisterEvents(SOCKET socket) const; 39 : 40 : private: 41 : friend class WakeupPipe; 42 : /* Register wakeup pipe with EdgeTriggeredEvents instance */ 43 : bool RegisterPipe(int wakeup_pipe); 44 : /* Unregister wakeup pipe with EdgeTriggeredEvents instance */ 45 : bool UnregisterPipe(int wakeup_pipe); 46 : 47 : private: 48 : bool RegisterEntity(int entity, const std::string& entity_name) const; 49 : bool UnregisterEntity(int entity, const std::string& entity_name) const; 50 : 51 : private: 52 : /* Flag set if pipe has been registered with instance */ 53 : bool m_pipe_registered{false}; 54 : /* Instance validity flag set during construction */ 55 : bool m_valid{false}; 56 : /* Flag for storing selected socket events mode */ 57 : SocketEventsMode m_mode; 58 : /* File descriptor used to interact with events mode */ 59 : int m_fd{-1}; 60 : }; 61 : 62 : #endif // BITCOIN_UTIL_EDGE_H