blob: d98b8031699f2b9d49376357c4895c6be87cc817 [file] [log] [blame]
honghaiz023f3ef2015-10-19 09:39:32 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_NETWORKMONITOR_H_
12#define RTC_BASE_NETWORKMONITOR_H_
honghaiz023f3ef2015-10-19 09:39:32 -070013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/logging.h"
15#include "rtc_base/sigslot.h"
16#include "rtc_base/thread.h"
honghaiz023f3ef2015-10-19 09:39:32 -070017
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018namespace rtc {
19
20class IPAddress;
21
22enum 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
Patrik Höglund581df612017-10-02 09:12:39 +000030enum 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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020040class 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.
71class 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
88class 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 */
109class 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
honghaiz023f3ef2015-10-19 09:39:32 -0700127
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200128#endif // RTC_BASE_NETWORKMONITOR_H_