Repository: envoyproxy/envoy
File: mobile/library/common/network/connectivity_manager.h (C/C++)

Code before:
                                                    SocketMode socket_mode) override;
  envoy_netconf_t addUpstreamSocketOptions(Socket::OptionsSharedPtr options) override;
  Extensions::Common::DynamicForwardProxy::DnsCacheSharedPtr dnsCache() override;
  envoy_netconf_t onNetworkMadeDefault(NetworkType network) override;

private:
  struct NetworkState {
    // The configuration key is passed through calls dispatched on the run loop to determine if
    // they're still valid/relevant at time of execution.
    envoy_netconf_t configuration_key_ ABSL_GUARDED_BY(mutex_);
    NetworkType network_ ABSL_GUARDED_BY(mutex_);
    uint8_t remaining_faults_ ABSL_GUARDED_BY(mutex_);
    SocketMode socket_mode_ ABSL_GUARDED_BY(mutex_);
    Thread::MutexBasicLockable mutex_;
  };
  Socket::OptionsSharedPtr getAlternateInterfaceSocketOptions(NetworkType network);
  InterfacePair getActiveAlternateInterface(NetworkType network, unsigned short family);

  bool enable_drain_post_dns_refresh_{false};
  bool enable_interface_binding_{false};
  absl::flat_hash_set<std::string> hosts_to_drain_;
  Extensions::Common::DynamicForwardProxy::DnsCache::AddUpdateCallbacksHandlePtr
      dns_callbacks_handle_{nullptr};
  Upstream::ClusterManager& cluster_manager_;
#ifdef ENVOY_ENABLE_QUIC
  std::unique_ptr<Quic::EnvoyMobileQuicNetworkObserverRegistryFactory>
      quic_observer_registry_factory_;
#endif
  DnsCacheManagerSharedPtr dns_cache_manager_;
  ProxySettingsConstSharedPtr proxy_settings_;
  static NetworkState network_state_;
  const bool quic_upstream_connection_handle_network_change_;
};

using ConnectivityManagerSharedPtr = std::shared_ptr<ConnectivityManager>;

/**
 * Provides access to the singleton ConnectivityManager.
 */
class ConnectivityManagerFactory {
public:
  ConnectivityManagerFactory(Server::Configuration::GenericFactoryContext& context)
      : context_(context) {}

  /**
   * @returns singleton ConnectivityManager instance.
   */
  ConnectivityManagerSharedPtr get();

private:
  Server::GenericFactoryContextImpl context_;

Review comment (suggestion):
nit: I looks like this is always created (when QUIC is compiled in). If so, consider making this a non-pointer member. That would avoid the need to explicitly construct it in the constructor.

Code after:
  virtual void onNetworkConnected(NetworkHandle network) PURE;
};

class ConnectivityManagerImpl : public ConnectivityManager,
                                public Singleton::Instance,
                                public Logger::Loggable<Logger::Id::upstream> {
public:
  /**
   * Sets the current OS default/preferred network class. Note this function is allowed to be
   * called from any thread.
   * @param network, the OS-preferred network.
   * @returns configuration key to associate with any related calls.
   */
  envoy_netconf_t setPreferredNetwork(int network);

  ConnectivityManagerImpl(Upstream::ClusterManager& cluster_manager,
                          DnsCacheManagerSharedPtr dns_cache_manager);

  // ConnectivityManager
  std::vector<InterfacePair> enumerateV4Interfaces() override;
  std::vector<InterfacePair> enumerateV6Interfaces() override;
  std::vector<InterfacePair> enumerateInterfaces(unsigned short family, unsigned int select_flags,
                                                 unsigned int reject_flags) override;
  int getPreferredNetwork() override;
  SocketMode getSocketMode() override;
  envoy_netconf_t getConfigurationKey() override;
  Envoy::Network::ProxySettingsConstSharedPtr getProxySettings() override;
  void reportNetworkUsage(envoy_netconf_t configuration_key, bool network_fault) override;
  void setProxySettings(ProxySettingsConstSharedPtr new_proxy_settings) override;
  void setDrainPostDnsRefreshEnabled(bool enabled) override;
  void setInterfaceBindingEnabled(bool enabled) override;
  void refreshDns(envoy_netconf_t configuration_key, bool drain_connections) override;
  void resetConnectivityState() override;
  envoy_netconf_t addUpstreamSocketOptions(Socket::OptionsSharedPtr options) override;
  Extensions::Common::DynamicForwardProxy::DnsCacheSharedPtr dnsCache() override;

  // These interfaces are only used to handle Android network change notifications.
  void onDefaultNetworkChangedAndroid(ConnectionType connection_type, NetworkHandle net_id);
  void onNetworkDisconnectAndroid(NetworkHandle net_id);
  void onNetworkConnectAndroid(ConnectionType connection_type, NetworkHandle net_id);
  void purgeActiveNetworkListAndroid(const std::vector<NetworkHandle>& active_network_ids);
  void setDefaultNetworkChangeCallback(DefaultNetworkChangeCallback cb) {
    default_network_change_callback_ = cb;
  }
  void setNetworkChangeObserver(NetworkChangeObserver* observer) { observer_ = observer; }

  // Refresh DNS regardless of configuration key change.
  void doRefreshDns(envoy_netconf_t configuration_key, bool drain_connections);

private:
  // The states of the current default network picked by the platform.