Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: webrtc/base/networkmonitor.h

Issue 1556743002: Bind a socket to a network if the network handle is set. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Merge with head Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 14 #include "webrtc/base/logging.h"
15 #include "webrtc/base/scoped_ptr.h" 15 #include "webrtc/base/scoped_ptr.h"
16 #include "webrtc/base/sigslot.h" 16 #include "webrtc/base/sigslot.h"
17 #include "webrtc/base/thread.h" 17 #include "webrtc/base/thread.h"
18 18
19 namespace rtc { 19 namespace rtc {
20
21 class IPAddress;
22
23 // Error values are negative.
24 enum NetworkBindingResults {
25 OK = 0, // No error
26 ERR_FAILED = -1, // Generic error
27 ERR_NOT_IMPLEMENTED = -2,
28 ERR_ADDRESS_NOT_FOUND = -3,
29 ERR_NETWORK_CHANGED = -4
30 };
31
32 class 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
20 /* 41 /*
21 * Receives network-change events via |OnNetworksChanged| and signals the 42 * Receives network-change events via |OnNetworksChanged| and signals the
22 * networks changed event. 43 * networks changed event.
23 * 44 *
24 * Threading consideration: 45 * Threading consideration:
25 * It is expected that all upstream operations (from native to Java) are 46 * It is expected that all upstream operations (from native to Java) are
26 * performed from the worker thread. This includes creating, starting and 47 * performed from the worker thread. This includes creating, starting and
27 * stopping the monitor. This avoids the potential race condition when creating 48 * stopping the monitor. This avoids the potential race condition when creating
28 * the singleton Java NetworkMonitor class. Downstream operations can be from 49 * the singleton Java NetworkMonitor class. Downstream operations can be from
29 * any thread, but this class will forward all the downstream operations onto 50 * any thread, but this class will forward all the downstream operations onto
30 * the worker thread. 51 * the worker thread.
31 * 52 *
32 * Memory consideration: 53 * Memory consideration:
33 * NetworkMonitor is owned by the caller (NetworkManager). The global network 54 * NetworkMonitor is owned by the caller (NetworkManager). The global network
34 * monitor factory is owned by the factory itself but needs to be released from 55 * monitor factory is owned by the factory itself but needs to be released from
35 * the factory creator. 56 * the factory creator.
36 */ 57 */
37 // Generic network monitor interface. It starts and stops monitoring network 58 // Generic network monitor interface. It starts and stops monitoring network
38 // changes, and fires the SignalNetworksChanged event when networks change. 59 // changes, and fires the SignalNetworksChanged event when networks change.
39 class NetworkMonitorInterface { 60 class NetworkMonitorInterface : public NetworkBinderInterface {
40 public: 61 public:
41 NetworkMonitorInterface(); 62 NetworkMonitorInterface();
42 virtual ~NetworkMonitorInterface(); 63 virtual ~NetworkMonitorInterface();
43 64
44 sigslot::signal0<> SignalNetworksChanged; 65 sigslot::signal0<> SignalNetworksChanged;
45 66
46 virtual void Start() = 0; 67 virtual void Start() = 0;
47 virtual void Stop() = 0; 68 virtual void Stop() = 0;
48 69
49 // Implementations should call this method on the base when networks change, 70 // Implementations should call this method on the base when networks change,
50 // and the base will fire SignalNetworksChanged on the right thread. 71 // and the base will fire SignalNetworksChanged on the right thread.
51 virtual void OnNetworksChanged() = 0; 72 virtual void OnNetworksChanged() = 0;
52 }; 73 };
53 74
54 class NetworkMonitorBase : public NetworkMonitorInterface, 75 class NetworkMonitorBase : public NetworkMonitorInterface,
55 public MessageHandler, 76 public MessageHandler,
56 public sigslot::has_slots<> { 77 public sigslot::has_slots<> {
57 public: 78 public:
58 NetworkMonitorBase(); 79 NetworkMonitorBase();
59 ~NetworkMonitorBase() override; 80 ~NetworkMonitorBase() override;
60 81
61 void OnNetworksChanged() override; 82 void OnNetworksChanged() override;
62 83
63 void OnMessage(Message* msg) override; 84 void OnMessage(Message* msg) override;
64 85
86 protected:
87 Thread* worker_thread() { return worker_thread_; }
88
65 private: 89 private:
66 Thread* thread_; 90 Thread* worker_thread_;
67 }; 91 };
68 92
69 /* 93 /*
70 * NetworkMonitorFactory creates NetworkMonitors. 94 * NetworkMonitorFactory creates NetworkMonitors.
71 */ 95 */
72 class NetworkMonitorFactory { 96 class NetworkMonitorFactory {
73 public: 97 public:
74 // This is not thread-safe; it should be called once (or once per audio/video 98 // This is not thread-safe; it should be called once (or once per audio/video
75 // call) during the call initialization. 99 // call) during the call initialization.
76 static void SetFactory(NetworkMonitorFactory* factory); 100 static void SetFactory(NetworkMonitorFactory* factory);
77 101
78 static void ReleaseFactory(NetworkMonitorFactory* factory); 102 static void ReleaseFactory(NetworkMonitorFactory* factory);
79 static NetworkMonitorFactory* GetFactory(); 103 static NetworkMonitorFactory* GetFactory();
80 104
81 virtual NetworkMonitorInterface* CreateNetworkMonitor() = 0; 105 virtual NetworkMonitorInterface* CreateNetworkMonitor() = 0;
82 106
83 virtual ~NetworkMonitorFactory(); 107 virtual ~NetworkMonitorFactory();
84 108
85 protected: 109 protected:
86 NetworkMonitorFactory(); 110 NetworkMonitorFactory();
87 }; 111 };
88 112
89 } // namespace rtc 113 } // namespace rtc
90 114
91 #endif // WEBRTC_BASE_NETWORKMONITOR_H_ 115 #endif // WEBRTC_BASE_NETWORKMONITOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698