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