honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_NETWORKMONITOR_H_ |
| 12 | #define RTC_BASE_NETWORKMONITOR_H_ |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/logging.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 15 | #include "rtc_base/network_constants.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/sigslot.h" |
| 17 | #include "rtc_base/thread.h" |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 18 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 19 | namespace rtc { |
| 20 | |
| 21 | class IPAddress; |
| 22 | |
| 23 | enum class NetworkBindingResult { |
| 24 | SUCCESS = 0, // No error |
| 25 | FAILURE = -1, // Generic error |
| 26 | NOT_IMPLEMENTED = -2, |
| 27 | ADDRESS_NOT_FOUND = -3, |
| 28 | NETWORK_CHANGED = -4 |
| 29 | }; |
| 30 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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 | /* |
| 44 | * Receives network-change events via |OnNetworksChanged| and signals the |
| 45 | * networks changed event. |
| 46 | * |
| 47 | * Threading consideration: |
| 48 | * It is expected that all upstream operations (from native to Java) are |
| 49 | * performed from the worker thread. This includes creating, starting and |
| 50 | * stopping the monitor. This avoids the potential race condition when creating |
| 51 | * the singleton Java NetworkMonitor class. Downstream operations can be from |
| 52 | * any thread, but this class will forward all the downstream operations onto |
| 53 | * the worker thread. |
| 54 | * |
| 55 | * Memory consideration: |
| 56 | * NetworkMonitor is owned by the caller (NetworkManager). The global network |
| 57 | * monitor factory is owned by the factory itself but needs to be released from |
| 58 | * the factory creator. |
| 59 | */ |
| 60 | // Generic network monitor interface. It starts and stops monitoring network |
| 61 | // changes, and fires the SignalNetworksChanged event when networks change. |
| 62 | class NetworkMonitorInterface { |
| 63 | public: |
| 64 | NetworkMonitorInterface(); |
| 65 | virtual ~NetworkMonitorInterface(); |
| 66 | |
| 67 | sigslot::signal0<> SignalNetworksChanged; |
| 68 | |
| 69 | virtual void Start() = 0; |
| 70 | virtual void Stop() = 0; |
| 71 | |
| 72 | // Implementations should call this method on the base when networks change, |
| 73 | // and the base will fire SignalNetworksChanged on the right thread. |
| 74 | virtual void OnNetworksChanged() = 0; |
| 75 | |
| 76 | virtual AdapterType GetAdapterType(const std::string& interface_name) = 0; |
| 77 | }; |
| 78 | |
| 79 | class NetworkMonitorBase : public NetworkMonitorInterface, |
| 80 | public MessageHandler, |
| 81 | public sigslot::has_slots<> { |
| 82 | public: |
| 83 | NetworkMonitorBase(); |
| 84 | ~NetworkMonitorBase() override; |
| 85 | |
| 86 | void OnNetworksChanged() override; |
| 87 | |
| 88 | void OnMessage(Message* msg) override; |
| 89 | |
| 90 | protected: |
| 91 | Thread* worker_thread() { return worker_thread_; } |
| 92 | |
| 93 | private: |
| 94 | Thread* worker_thread_; |
| 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * NetworkMonitorFactory creates NetworkMonitors. |
| 99 | */ |
| 100 | class NetworkMonitorFactory { |
| 101 | public: |
| 102 | // This is not thread-safe; it should be called once (or once per audio/video |
| 103 | // call) during the call initialization. |
| 104 | static void SetFactory(NetworkMonitorFactory* factory); |
| 105 | |
| 106 | static void ReleaseFactory(NetworkMonitorFactory* factory); |
| 107 | static NetworkMonitorFactory* GetFactory(); |
| 108 | |
| 109 | virtual NetworkMonitorInterface* CreateNetworkMonitor() = 0; |
| 110 | |
| 111 | virtual ~NetworkMonitorFactory(); |
| 112 | |
| 113 | protected: |
| 114 | NetworkMonitorFactory(); |
| 115 | }; |
| 116 | |
| 117 | } // namespace rtc |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 118 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 119 | #endif // RTC_BASE_NETWORKMONITOR_H_ |