network_monitor.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2015 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_NETWORK_MONITOR_H_
  11. #define RTC_BASE_NETWORK_MONITOR_H_
  12. #include "rtc_base/network_constants.h"
  13. #include "rtc_base/third_party/sigslot/sigslot.h"
  14. namespace rtc {
  15. class IPAddress;
  16. enum class NetworkBindingResult {
  17. SUCCESS = 0, // No error
  18. FAILURE = -1, // Generic error
  19. NOT_IMPLEMENTED = -2,
  20. ADDRESS_NOT_FOUND = -3,
  21. NETWORK_CHANGED = -4
  22. };
  23. // NetworkPreference property set by operating system/firmware that has
  24. // information about connection strength to e.g WIFI router or CELL base towers.
  25. // GENERATED_JAVA_ENUM_PACKAGE: org.webrtc
  26. enum class NetworkPreference {
  27. NEUTRAL = 0,
  28. NOT_PREFERRED = -1,
  29. };
  30. const char* NetworkPreferenceToString(NetworkPreference preference);
  31. class NetworkBinderInterface {
  32. public:
  33. // Binds a socket to the network that is attached to |address| so that all
  34. // packets on the socket |socket_fd| will be sent via that network.
  35. // This is needed because some operating systems (like Android) require a
  36. // special bind call to put packets on a non-default network interface.
  37. virtual NetworkBindingResult BindSocketToNetwork(
  38. int socket_fd,
  39. const IPAddress& address) = 0;
  40. virtual ~NetworkBinderInterface() {}
  41. };
  42. /*
  43. * Receives network-change events via |OnNetworksChanged| and signals the
  44. * networks changed event.
  45. *
  46. * Threading consideration:
  47. * It is expected that all upstream operations (from native to Java) are
  48. * performed from the worker thread. This includes creating, starting and
  49. * stopping the monitor. This avoids the potential race condition when creating
  50. * the singleton Java NetworkMonitor class. Downstream operations can be from
  51. * any thread, but this class will forward all the downstream operations onto
  52. * the worker thread.
  53. *
  54. * Memory consideration:
  55. * NetworkMonitor is owned by the caller (NetworkManager). The global network
  56. * monitor factory is owned by the PeerConnectionFactory.
  57. */
  58. // Generic network monitor interface. It starts and stops monitoring network
  59. // changes, and fires the SignalNetworksChanged event when networks change.
  60. class NetworkMonitorInterface {
  61. public:
  62. NetworkMonitorInterface();
  63. virtual ~NetworkMonitorInterface();
  64. sigslot::signal0<> SignalNetworksChanged;
  65. virtual void Start() = 0;
  66. virtual void Stop() = 0;
  67. virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
  68. virtual AdapterType GetVpnUnderlyingAdapterType(
  69. const std::string& interface_name) = 0;
  70. virtual NetworkPreference GetNetworkPreference(
  71. const std::string& interface_name) = 0;
  72. // Is this interface available to use? WebRTC shouldn't attempt to use it if
  73. // this returns false.
  74. //
  75. // It's possible for this status to change, in which case
  76. // SignalNetworksChanged will be fired.
  77. //
  78. // These specific use case this was added for was a phone with two SIM cards,
  79. // where attempting to use all interfaces returned from getifaddrs caused the
  80. // connection to be dropped.
  81. virtual bool IsAdapterAvailable(const std::string& interface_name) {
  82. return true;
  83. }
  84. };
  85. } // namespace rtc
  86. #endif // RTC_BASE_NETWORK_MONITOR_H_