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
178
  SocketInterfaceExtension(SocketInterface& sock_interface) : sock_interface_(sock_interface) {}
20
  // Server::BootstrapExtension
21
6
  void onServerInitialized(Server::Instance&) override {}
22
  void onWorkerThreadInitialized() override {}
23

            
24
protected:
25
  SocketInterface& sock_interface_;
26
};
27

            
28
// Class to be derived by all SocketInterface implementations.
29
//
30
// It acts both as a SocketInterface and as a BootstrapExtensionFactory. The latter is used, on the
31
// one hand, to configure and initialize the interface and, on the other, for SocketInterface lookup
32
// by leveraging the FactoryRegistry. As required for all bootstrap extensions, all derived classes
33
// should register via the REGISTER_FACTORY() macro as BootstrapExtensionFactory.
34
//
35
// SocketInterface instances can be retrieved using the factory name, i.e., string returned by
36
// name() function implemented by all classes that derive SocketInterfaceBase, via
37
// Network::socketInterface(). When instantiating addresses, address resolvers should
38
// set the socket interface field to the name of the socket interface implementation that should
39
// be used to create sockets for said addresses.
40
class SocketInterfaceBase : public SocketInterface,
41
                            public Server::Configuration::BootstrapExtensionFactory {};
42

            
43
/**
44
 * Lookup SocketInterface instance by name
45
 * @param name Name of the socket interface to be looked up
46
 * @return Pointer to @ref SocketInterface instance that registered using the name of nullptr
47
 */
48
619
static inline const SocketInterface* socketInterface(std::string name) {
49
619
  auto factory =
50
619
      Registry::FactoryRegistry<Server::Configuration::BootstrapExtensionFactory>::getFactory(name);
51
619
  return dynamic_cast<SocketInterface*>(factory);
52
619
}
53

            
54
using SocketInterfaceSingleton = InjectableSingleton<SocketInterface>;
55
using SocketInterfaceLoader = ScopedInjectableLoader<SocketInterface>;
56

            
57
} // namespace Network
58
} // namespace Envoy