OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_BASE_NETWORKMONITOR_H_ | 11 #ifndef WEBRTC_BASE_NETWORKMONITOR_H_ |
12 #define WEBRTC_BASE_NETWORKMONITOR_H_ | 12 #define WEBRTC_BASE_NETWORKMONITOR_H_ |
13 | 13 |
14 #include "webrtc/base/logging.h" | |
15 #include "webrtc/base/sigslot.h" | |
16 #include "webrtc/base/thread.h" | |
17 | 14 |
18 namespace rtc { | 15 // This header is deprecated and is just left here temporarily during |
19 | 16 // refactoring. See https://bugs.webrtc.org/7634 for more details. |
20 class IPAddress; | 17 #include "webrtc/rtc_base/networkmonitor.h" |
21 | |
22 enum class NetworkBindingResult { | |
23 SUCCESS = 0, // No error | |
24 FAILURE = -1, // Generic error | |
25 NOT_IMPLEMENTED = -2, | |
26 ADDRESS_NOT_FOUND = -3, | |
27 NETWORK_CHANGED = -4 | |
28 }; | |
29 | |
30 enum AdapterType { | |
31 // This enum resembles the one in Chromium net::ConnectionType. | |
32 ADAPTER_TYPE_UNKNOWN = 0, | |
33 ADAPTER_TYPE_ETHERNET = 1 << 0, | |
34 ADAPTER_TYPE_WIFI = 1 << 1, | |
35 ADAPTER_TYPE_CELLULAR = 1 << 2, | |
36 ADAPTER_TYPE_VPN = 1 << 3, | |
37 ADAPTER_TYPE_LOOPBACK = 1 << 4 | |
38 }; | |
39 | |
40 class NetworkBinderInterface { | |
41 public: | |
42 // Binds a socket to the network that is attached to |address| so that all | |
43 // packets on the socket |socket_fd| will be sent via that network. | |
44 // This is needed because some operating systems (like Android) require a | |
45 // special bind call to put packets on a non-default network interface. | |
46 virtual NetworkBindingResult BindSocketToNetwork( | |
47 int socket_fd, | |
48 const IPAddress& address) = 0; | |
49 virtual ~NetworkBinderInterface() {} | |
50 }; | |
51 | |
52 /* | |
53 * Receives network-change events via |OnNetworksChanged| and signals the | |
54 * networks changed event. | |
55 * | |
56 * Threading consideration: | |
57 * It is expected that all upstream operations (from native to Java) are | |
58 * performed from the worker thread. This includes creating, starting and | |
59 * stopping the monitor. This avoids the potential race condition when creating | |
60 * the singleton Java NetworkMonitor class. Downstream operations can be from | |
61 * any thread, but this class will forward all the downstream operations onto | |
62 * the worker thread. | |
63 * | |
64 * Memory consideration: | |
65 * NetworkMonitor is owned by the caller (NetworkManager). The global network | |
66 * monitor factory is owned by the factory itself but needs to be released from | |
67 * the factory creator. | |
68 */ | |
69 // Generic network monitor interface. It starts and stops monitoring network | |
70 // changes, and fires the SignalNetworksChanged event when networks change. | |
71 class NetworkMonitorInterface { | |
72 public: | |
73 NetworkMonitorInterface(); | |
74 virtual ~NetworkMonitorInterface(); | |
75 | |
76 sigslot::signal0<> SignalNetworksChanged; | |
77 | |
78 virtual void Start() = 0; | |
79 virtual void Stop() = 0; | |
80 | |
81 // Implementations should call this method on the base when networks change, | |
82 // and the base will fire SignalNetworksChanged on the right thread. | |
83 virtual void OnNetworksChanged() = 0; | |
84 | |
85 virtual AdapterType GetAdapterType(const std::string& interface_name) = 0; | |
86 }; | |
87 | |
88 class NetworkMonitorBase : public NetworkMonitorInterface, | |
89 public MessageHandler, | |
90 public sigslot::has_slots<> { | |
91 public: | |
92 NetworkMonitorBase(); | |
93 ~NetworkMonitorBase() override; | |
94 | |
95 void OnNetworksChanged() override; | |
96 | |
97 void OnMessage(Message* msg) override; | |
98 | |
99 protected: | |
100 Thread* worker_thread() { return worker_thread_; } | |
101 | |
102 private: | |
103 Thread* worker_thread_; | |
104 }; | |
105 | |
106 /* | |
107 * NetworkMonitorFactory creates NetworkMonitors. | |
108 */ | |
109 class NetworkMonitorFactory { | |
110 public: | |
111 // This is not thread-safe; it should be called once (or once per audio/video | |
112 // call) during the call initialization. | |
113 static void SetFactory(NetworkMonitorFactory* factory); | |
114 | |
115 static void ReleaseFactory(NetworkMonitorFactory* factory); | |
116 static NetworkMonitorFactory* GetFactory(); | |
117 | |
118 virtual NetworkMonitorInterface* CreateNetworkMonitor() = 0; | |
119 | |
120 virtual ~NetworkMonitorFactory(); | |
121 | |
122 protected: | |
123 NetworkMonitorFactory(); | |
124 }; | |
125 | |
126 } // namespace rtc | |
127 | 18 |
128 #endif // WEBRTC_BASE_NETWORKMONITOR_H_ | 19 #endif // WEBRTC_BASE_NETWORKMONITOR_H_ |
OLD | NEW |