Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/config/typed_config.h" 4 : #include "envoy/network/socket_interface.h" 5 : #include "envoy/registry/registry.h" 6 : #include "envoy/server/bootstrap_extension_config.h" 7 : 8 : #include "source/common/singleton/threadsafe_singleton.h" 9 : 10 : #include "absl/container/flat_hash_map.h" 11 : 12 : namespace Envoy { 13 : namespace Network { 14 : 15 : // Wrapper for SocketInterface instances returned by createBootstrapExtension() which must be 16 : // implemented by all factories that derive SocketInterfaceBase 17 : class SocketInterfaceExtension : public Server::BootstrapExtension { 18 : public: 19 0 : SocketInterfaceExtension(SocketInterface& sock_interface) : sock_interface_(sock_interface) {} 20 : // Server::BootstrapExtension 21 0 : void onServerInitialized() override {} 22 : 23 : protected: 24 : SocketInterface& sock_interface_; 25 : }; 26 : 27 : // Class to be derived by all SocketInterface implementations. 28 : // 29 : // It acts both as a SocketInterface and as a BootstrapExtensionFactory. The latter is used, on the 30 : // one hand, to configure and initialize the interface and, on the other, for SocketInterface lookup 31 : // by leveraging the FactoryRegistry. As required for all bootstrap extensions, all derived classes 32 : // should register via the REGISTER_FACTORY() macro as BootstrapExtensionFactory. 33 : // 34 : // SocketInterface instances can be retrieved using the factory name, i.e., string returned by 35 : // name() function implemented by all classes that derive SocketInterfaceBase, via 36 : // Network::socketInterface(). When instantiating addresses, address resolvers should 37 : // set the socket interface field to the name of the socket interface implementation that should 38 : // be used to create sockets for said addresses. 39 : class SocketInterfaceBase : public SocketInterface, 40 : public Server::Configuration::BootstrapExtensionFactory {}; 41 : 42 : /** 43 : * Lookup SocketInterface instance by name 44 : * @param name Name of the socket interface to be looked up 45 : * @return Pointer to @ref SocketInterface instance that registered using the name of nullptr 46 : */ 47 0 : static inline const SocketInterface* socketInterface(std::string name) { 48 0 : auto factory = 49 0 : Registry::FactoryRegistry<Server::Configuration::BootstrapExtensionFactory>::getFactory(name); 50 0 : return dynamic_cast<SocketInterface*>(factory); 51 0 : } 52 : 53 : using SocketInterfaceSingleton = InjectableSingleton<SocketInterface>; 54 : using SocketInterfaceLoader = ScopedInjectableLoader<SocketInterface>; 55 : 56 : } // namespace Network 57 : } // namespace Envoy