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 | |
| 11 | #ifndef WEBRTC_BASE_NETWORKMONITOR_H_ |
| 12 | #define WEBRTC_BASE_NETWORKMONITOR_H_ |
| 13 | |
| 14 | #include "webrtc/base/logging.h" |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 15 | #include "webrtc/base/sigslot.h" |
| 16 | #include "webrtc/base/thread.h" |
| 17 | |
| 18 | namespace rtc { |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 19 | |
| 20 | class IPAddress; |
| 21 | |
| 22 | // Error values are negative. |
| 23 | enum NetworkBindingResults { |
| 24 | NETWORK_BIND_SUCCESS = 0, // No error |
| 25 | NETWORK_BIND_FAILURE = -1, // Generic error |
| 26 | NETWORK_BIND_NOT_IMPLEMENTED = -2, |
| 27 | NETWORK_BIND_ADDRESS_NOT_FOUND = -3, |
| 28 | NETWORK_BIND_NETWORK_CHANGED = -4 |
| 29 | }; |
| 30 | |
honghaiz | a7ad7c3 | 2016-02-02 12:54:14 -0800 | [diff] [blame] | 31 | enum AdapterType { |
| 32 | // This enum resembles the one in Chromium net::ConnectionType. |
| 33 | ADAPTER_TYPE_UNKNOWN = 0, |
| 34 | ADAPTER_TYPE_ETHERNET = 1 << 0, |
| 35 | ADAPTER_TYPE_WIFI = 1 << 1, |
| 36 | ADAPTER_TYPE_CELLULAR = 1 << 2, |
| 37 | ADAPTER_TYPE_VPN = 1 << 3, |
| 38 | ADAPTER_TYPE_LOOPBACK = 1 << 4 |
| 39 | }; |
| 40 | |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 41 | class NetworkBinderInterface { |
| 42 | public: |
| 43 | // Binds a socket to the network that is attached to |address| so that all |
| 44 | // packets on the socket |socket_fd| will be sent via that network. |
| 45 | // This is needed because some operating systems (like Android) require a |
| 46 | // special bind call to put packets on a non-default network interface. |
| 47 | virtual int BindSocketToNetwork(int socket_fd, const IPAddress& address) = 0; |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 48 | virtual ~NetworkBinderInterface() {} |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Receives network-change events via |OnNetworksChanged| and signals the |
| 53 | * networks changed event. |
| 54 | * |
| 55 | * Threading consideration: |
| 56 | * It is expected that all upstream operations (from native to Java) are |
| 57 | * performed from the worker thread. This includes creating, starting and |
| 58 | * stopping the monitor. This avoids the potential race condition when creating |
| 59 | * the singleton Java NetworkMonitor class. Downstream operations can be from |
| 60 | * any thread, but this class will forward all the downstream operations onto |
| 61 | * the worker thread. |
| 62 | * |
| 63 | * Memory consideration: |
| 64 | * NetworkMonitor is owned by the caller (NetworkManager). The global network |
| 65 | * monitor factory is owned by the factory itself but needs to be released from |
| 66 | * the factory creator. |
| 67 | */ |
| 68 | // Generic network monitor interface. It starts and stops monitoring network |
| 69 | // changes, and fires the SignalNetworksChanged event when networks change. |
| 70 | class NetworkMonitorInterface { |
| 71 | public: |
| 72 | NetworkMonitorInterface(); |
| 73 | virtual ~NetworkMonitorInterface(); |
| 74 | |
| 75 | sigslot::signal0<> SignalNetworksChanged; |
| 76 | |
| 77 | virtual void Start() = 0; |
| 78 | virtual void Stop() = 0; |
| 79 | |
| 80 | // Implementations should call this method on the base when networks change, |
| 81 | // and the base will fire SignalNetworksChanged on the right thread. |
| 82 | virtual void OnNetworksChanged() = 0; |
honghaiz | a7ad7c3 | 2016-02-02 12:54:14 -0800 | [diff] [blame] | 83 | |
| 84 | virtual AdapterType GetAdapterType(const std::string& interface_name) = 0; |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | class NetworkMonitorBase : public NetworkMonitorInterface, |
| 88 | public MessageHandler, |
| 89 | public sigslot::has_slots<> { |
| 90 | public: |
| 91 | NetworkMonitorBase(); |
| 92 | ~NetworkMonitorBase() override; |
| 93 | |
| 94 | void OnNetworksChanged() override; |
| 95 | |
| 96 | void OnMessage(Message* msg) override; |
| 97 | |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 98 | protected: |
| 99 | Thread* worker_thread() { return worker_thread_; } |
| 100 | |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 101 | private: |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 102 | Thread* worker_thread_; |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * NetworkMonitorFactory creates NetworkMonitors. |
| 107 | */ |
| 108 | class NetworkMonitorFactory { |
| 109 | public: |
| 110 | // This is not thread-safe; it should be called once (or once per audio/video |
| 111 | // call) during the call initialization. |
| 112 | static void SetFactory(NetworkMonitorFactory* factory); |
| 113 | |
| 114 | static void ReleaseFactory(NetworkMonitorFactory* factory); |
| 115 | static NetworkMonitorFactory* GetFactory(); |
| 116 | |
| 117 | virtual NetworkMonitorInterface* CreateNetworkMonitor() = 0; |
| 118 | |
| 119 | virtual ~NetworkMonitorFactory(); |
| 120 | |
| 121 | protected: |
| 122 | NetworkMonitorFactory(); |
| 123 | }; |
| 124 | |
| 125 | } // namespace rtc |
| 126 | |
| 127 | #endif // WEBRTC_BASE_NETWORKMONITOR_H_ |