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 |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "webrtc/base/socketaddress.h" | 25 #include "webrtc/base/socketaddress.h" |
26 #include "webrtc/base/thread.h" | 26 #include "webrtc/base/thread.h" |
27 #include "webrtc/base/thread_checker.h" | 27 #include "webrtc/base/thread_checker.h" |
28 #include "webrtc/typedefs.h" | 28 #include "webrtc/typedefs.h" |
29 | 29 |
30 namespace rtc { | 30 namespace rtc { |
31 class AsyncPacketSocket; | 31 class AsyncPacketSocket; |
32 class PacketSocketFactory; | 32 class PacketSocketFactory; |
33 class Thread; | 33 class Thread; |
34 class NetworkManager; | 34 class NetworkManager; |
| 35 class AsyncResolverInterface; |
35 } // namespace rtc | 36 } // namespace rtc |
36 | 37 |
37 namespace stunprober { | 38 namespace stunprober { |
38 | 39 |
39 class StunProber; | 40 class StunProber; |
40 | 41 |
41 static const int kMaxUdpBufferSize = 1200; | 42 static const int kMaxUdpBufferSize = 1200; |
42 | 43 |
43 typedef rtc::Callback2<void, StunProber*, int> AsyncCallback; | 44 typedef rtc::Callback2<void, StunProber*, int> AsyncCallback; |
44 | 45 |
45 enum NatType { | 46 enum NatType { |
46 NATTYPE_INVALID, | 47 NATTYPE_INVALID, |
47 NATTYPE_NONE, // Not behind a NAT. | 48 NATTYPE_NONE, // Not behind a NAT. |
48 NATTYPE_UNKNOWN, // Behind a NAT but type can't be determine. | 49 NATTYPE_UNKNOWN, // Behind a NAT but type can't be determine. |
49 NATTYPE_SYMMETRIC, // Behind a symmetric NAT. | 50 NATTYPE_SYMMETRIC, // Behind a symmetric NAT. |
50 NATTYPE_NON_SYMMETRIC // Behind a non-symmetric NAT. | 51 NATTYPE_NON_SYMMETRIC // Behind a non-symmetric NAT. |
51 }; | 52 }; |
52 | 53 |
53 class StunProber : public sigslot::has_slots<> { | 54 class StunProber : public sigslot::has_slots<> { |
54 public: | 55 public: |
55 enum Status { // Used in UMA_HISTOGRAM_ENUMERATION. | 56 enum Status { // Used in UMA_HISTOGRAM_ENUMERATION. |
56 SUCCESS, // Successfully received bytes from the server. | 57 SUCCESS, // Successfully received bytes from the server. |
57 GENERIC_FAILURE, // Generic failure. | 58 GENERIC_FAILURE, // Generic failure. |
58 RESOLVE_FAILED, // Host resolution failed. | 59 RESOLVE_FAILED, // Host resolution failed. |
59 WRITE_FAILED, // Sending a message to the server failed. | 60 WRITE_FAILED, // Sending a message to the server failed. |
60 READ_FAILED, // Reading the reply from the server failed. | 61 READ_FAILED, // Reading the reply from the server failed. |
61 }; | 62 }; |
62 | 63 |
| 64 class Observer { |
| 65 public: |
| 66 virtual ~Observer() = default; |
| 67 virtual void OnPrepared(StunProber* prober, StunProber::Status status) = 0; |
| 68 virtual void OnFinished(StunProber* prober, StunProber::Status status) = 0; |
| 69 }; |
| 70 |
63 struct Stats { | 71 struct Stats { |
64 Stats() {} | 72 Stats() {} |
65 | 73 |
66 int num_request_sent = 0; | 74 int num_request_sent = 0; |
67 int num_response_received = 0; | 75 int num_response_received = 0; |
68 NatType nat_type = NATTYPE_INVALID; | 76 NatType nat_type = NATTYPE_INVALID; |
69 int average_rtt_ms = -1; | 77 int average_rtt_ms = -1; |
70 int success_percent = 0; | 78 int success_percent = 0; |
71 int target_request_interval_ns = 0; | 79 int target_request_interval_ns = 0; |
72 int actual_request_interval_ns = 0; | 80 int actual_request_interval_ns = 0; |
(...skipping 18 matching lines...) Expand all Loading... |
91 // |shared_socket_mode| is false, each request will be done with a new socket. | 99 // |shared_socket_mode| is false, each request will be done with a new socket. |
92 // Otherwise, a unique socket will be used for a single round of requests | 100 // Otherwise, a unique socket will be used for a single round of requests |
93 // against all resolved IPs. No single socket will be used against a given IP | 101 // against all resolved IPs. No single socket will be used against a given IP |
94 // more than once. The interval of requests will be as close to the requested | 102 // more than once. The interval of requests will be as close to the requested |
95 // inter-probe interval |stun_ta_interval_ms| as possible. After sending out | 103 // inter-probe interval |stun_ta_interval_ms| as possible. After sending out |
96 // the last scheduled request, the probe will wait |timeout_ms| for request | 104 // the last scheduled request, the probe will wait |timeout_ms| for request |
97 // responses and then call |finish_callback|. |requests_per_ip| indicates how | 105 // responses and then call |finish_callback|. |requests_per_ip| indicates how |
98 // many requests should be tried for each resolved IP address. In shared mode, | 106 // many requests should be tried for each resolved IP address. In shared mode, |
99 // (the number of sockets to be created) equals to |requests_per_ip|. In | 107 // (the number of sockets to be created) equals to |requests_per_ip|. In |
100 // non-shared mode, (the number of sockets) equals to requests_per_ip * (the | 108 // non-shared mode, (the number of sockets) equals to requests_per_ip * (the |
101 // number of resolved IP addresses). | 109 // number of resolved IP addresses). TODO(guoweis): Remove this once |
| 110 // everything moved to Prepare() and Run(). |
102 bool Start(const std::vector<rtc::SocketAddress>& servers, | 111 bool Start(const std::vector<rtc::SocketAddress>& servers, |
103 bool shared_socket_mode, | 112 bool shared_socket_mode, |
104 int stun_ta_interval_ms, | 113 int stun_ta_interval_ms, |
105 int requests_per_ip, | 114 int requests_per_ip, |
106 int timeout_ms, | 115 int timeout_ms, |
107 const AsyncCallback finish_callback); | 116 const AsyncCallback finish_callback); |
108 | 117 |
| 118 // TODO(guoweis): The combination of Prepare() and Run() are equivalent to the |
| 119 // Start() above. Remove Start() once everything is migrated. |
| 120 bool Prepare(const std::vector<rtc::SocketAddress>& servers, |
| 121 bool shared_socket_mode, |
| 122 int stun_ta_interval_ms, |
| 123 int requests_per_ip, |
| 124 int timeout_ms, |
| 125 StunProber::Observer* observer); |
| 126 |
| 127 // Start to send out the STUN probes. |
| 128 bool Start(StunProber::Observer* observer); |
| 129 |
109 // Method to retrieve the Stats once |finish_callback| is invoked. Returning | 130 // Method to retrieve the Stats once |finish_callback| is invoked. Returning |
110 // false when the result is inconclusive, for example, whether it's behind a | 131 // false when the result is inconclusive, for example, whether it's behind a |
111 // NAT or not. | 132 // NAT or not. |
112 bool GetStats(Stats* stats) const; | 133 bool GetStats(Stats* stats) const; |
113 | 134 |
| 135 int estimated_execution_time() { |
| 136 return static_cast<int>(requests_per_ip_ * all_servers_addrs_.size() * |
| 137 interval_ms_); |
| 138 } |
| 139 |
114 private: | 140 private: |
115 // A requester tracks the requests and responses from a single socket to many | 141 // A requester tracks the requests and responses from a single socket to many |
116 // STUN servers. | 142 // STUN servers. |
117 class Requester; | 143 class Requester; |
118 | 144 |
| 145 // TODO(guoweis): Remove this once all dependencies move away from |
| 146 // AsyncCallback. |
| 147 class ObserverAdapter : public Observer { |
| 148 public: |
| 149 void set_callback(AsyncCallback callback) { callback_ = callback; } |
| 150 void OnPrepared(StunProber* stunprober, Status status) { |
| 151 if (status == SUCCESS) { |
| 152 stunprober->Start(this); |
| 153 } else { |
| 154 callback_(stunprober, status); |
| 155 } |
| 156 } |
| 157 void OnFinished(StunProber* stunprober, Status status) { |
| 158 callback_(stunprober, status); |
| 159 } |
| 160 |
| 161 private: |
| 162 AsyncCallback callback_; |
| 163 }; |
| 164 |
119 bool ResolveServerName(const rtc::SocketAddress& addr); | 165 bool ResolveServerName(const rtc::SocketAddress& addr); |
120 void OnServerResolved(rtc::AsyncResolverInterface* resolver); | 166 void OnServerResolved(rtc::AsyncResolverInterface* resolver); |
121 | 167 |
122 void OnSocketReady(rtc::AsyncPacketSocket* socket, | 168 void OnSocketReady(rtc::AsyncPacketSocket* socket, |
123 const rtc::SocketAddress& addr); | 169 const rtc::SocketAddress& addr); |
124 | 170 |
125 bool Done() { | 171 bool Done() { |
126 return num_request_sent_ >= requests_per_ip_ * all_servers_addrs_.size(); | 172 return num_request_sent_ >= requests_per_ip_ * all_servers_addrs_.size(); |
127 } | 173 } |
128 | 174 |
129 size_t total_socket_required() { | 175 size_t total_socket_required() { |
130 return (shared_socket_mode_ ? 1 : all_servers_addrs_.size()) * | 176 return (shared_socket_mode_ ? 1 : all_servers_addrs_.size()) * |
131 requests_per_ip_; | 177 requests_per_ip_; |
132 } | 178 } |
133 | 179 |
| 180 bool should_send_next_request(uint32_t now); |
| 181 int get_wake_up_interval_ms(); |
| 182 |
134 bool SendNextRequest(); | 183 bool SendNextRequest(); |
135 | 184 |
136 // Will be invoked in 1ms intervals and schedule the next request from the | 185 // Will be invoked in 1ms intervals and schedule the next request from the |
137 // |current_requester_| if the time has passed for another request. | 186 // |current_requester_| if the time has passed for another request. |
138 void MaybeScheduleStunRequests(); | 187 void MaybeScheduleStunRequests(); |
139 | 188 |
140 // End the probe with the given |status|. Invokes |fininsh_callback|, which | 189 void ReportOnPrepared(StunProber::Status status); |
141 // may destroy the class. | 190 void ReportOnFinished(StunProber::Status status); |
142 void End(StunProber::Status status); | |
143 | 191 |
144 Requester* CreateRequester(); | 192 Requester* CreateRequester(); |
145 | 193 |
146 Requester* current_requester_ = nullptr; | 194 Requester* current_requester_ = nullptr; |
147 | 195 |
148 // The time when the next request should go out. | 196 // The time when the next request should go out. |
149 uint64_t next_request_time_ms_ = 0; | 197 uint64_t next_request_time_ms_ = 0; |
150 | 198 |
151 // Total requests sent so far. | 199 // Total requests sent so far. |
152 uint32_t num_request_sent_ = 0; | 200 uint32_t num_request_sent_ = 0; |
(...skipping 12 matching lines...) Expand all Loading... |
165 // STUN server name to be resolved. | 213 // STUN server name to be resolved. |
166 std::vector<rtc::SocketAddress> servers_; | 214 std::vector<rtc::SocketAddress> servers_; |
167 | 215 |
168 // Weak references. | 216 // Weak references. |
169 rtc::PacketSocketFactory* socket_factory_; | 217 rtc::PacketSocketFactory* socket_factory_; |
170 rtc::Thread* thread_; | 218 rtc::Thread* thread_; |
171 | 219 |
172 // Accumulate all resolved addresses. | 220 // Accumulate all resolved addresses. |
173 std::vector<rtc::SocketAddress> all_servers_addrs_; | 221 std::vector<rtc::SocketAddress> all_servers_addrs_; |
174 | 222 |
175 // Caller-supplied callback executed when testing is completed, called by | |
176 // End(). | |
177 AsyncCallback finished_callback_; | |
178 | |
179 // The set of STUN probe sockets and their state. | 223 // The set of STUN probe sockets and their state. |
180 std::vector<Requester*> requesters_; | 224 std::vector<Requester*> requesters_; |
181 | 225 |
182 rtc::ThreadChecker thread_checker_; | 226 rtc::ThreadChecker thread_checker_; |
183 | 227 |
184 // Temporary storage for created sockets. | 228 // Temporary storage for created sockets. |
185 std::vector<rtc::AsyncPacketSocket*> sockets_; | 229 std::vector<rtc::AsyncPacketSocket*> sockets_; |
186 // This tracks how many of the sockets are ready. | 230 // This tracks how many of the sockets are ready. |
187 size_t total_ready_sockets_ = 0; | 231 size_t total_ready_sockets_ = 0; |
188 | 232 |
189 rtc::AsyncInvoker invoker_; | 233 rtc::AsyncInvoker invoker_; |
190 | 234 |
| 235 Observer* observer_ = nullptr; |
| 236 // TODO(guoweis): Remove this once all dependencies move away from |
| 237 // AsyncCallback. |
| 238 ObserverAdapter observer_adapter_; |
| 239 |
191 rtc::NetworkManager::NetworkList networks_; | 240 rtc::NetworkManager::NetworkList networks_; |
192 | 241 |
193 RTC_DISALLOW_COPY_AND_ASSIGN(StunProber); | 242 RTC_DISALLOW_COPY_AND_ASSIGN(StunProber); |
194 }; | 243 }; |
195 | 244 |
196 } // namespace stunprober | 245 } // namespace stunprober |
197 | 246 |
198 #endif // WEBRTC_P2P_STUNPROBER_STUNPROBER_H_ | 247 #endif // WEBRTC_P2P_STUNPROBER_STUNPROBER_H_ |
OLD | NEW |