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 Run(StunProber::Observer* observer); | |
pthatcher1
2015/10/22 22:26:22
Run makes it sound synchronous.
Shouldn't we ca
| |
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 // Return the estimated execution time, i.e. |requests_per_ip_| * | |
136 // |all_servers_addrs_|.size() * |interval_ms_|. | |
pthatcher1
2015/10/22 22:26:22
This comment is just a duplication of the method n
| |
137 int estimated_execution_time() { | |
138 return requests_per_ip_ * all_servers_addrs_.size() * interval_ms_; | |
139 } | |
140 | |
114 private: | 141 private: |
115 // A requester tracks the requests and responses from a single socket to many | 142 // A requester tracks the requests and responses from a single socket to many |
116 // STUN servers. | 143 // STUN servers. |
117 class Requester; | 144 class Requester; |
118 | 145 |
146 // TODO(guoweis): Remove this once all dependencies move away from | |
147 // AsyncCallback. | |
148 class ObserverAdapter : public Observer { | |
149 public: | |
150 void set_callback(AsyncCallback callback) { callback_ = callback; } | |
151 void OnPrepared(StunProber* stunprober, Status status) { | |
152 if (status == SUCCESS) { | |
153 stunprober->Run(this); | |
154 } else { | |
155 callback_(stunprober, status); | |
156 } | |
157 } | |
158 void OnFinished(StunProber* stunprober, Status status) { | |
159 callback_(stunprober, status); | |
160 } | |
161 | |
162 private: | |
163 AsyncCallback callback_; | |
164 }; | |
165 | |
119 bool ResolveServerName(const rtc::SocketAddress& addr); | 166 bool ResolveServerName(const rtc::SocketAddress& addr); |
120 void OnServerResolved(rtc::AsyncResolverInterface* resolver); | 167 void OnServerResolved(rtc::AsyncResolverInterface* resolver); |
121 | 168 |
122 void OnSocketReady(rtc::AsyncPacketSocket* socket, | 169 void OnSocketReady(rtc::AsyncPacketSocket* socket, |
123 const rtc::SocketAddress& addr); | 170 const rtc::SocketAddress& addr); |
124 | 171 |
125 bool Done() { | 172 bool Done() { |
126 return num_request_sent_ >= requests_per_ip_ * all_servers_addrs_.size(); | 173 return num_request_sent_ >= requests_per_ip_ * all_servers_addrs_.size(); |
127 } | 174 } |
128 | 175 |
129 size_t total_socket_required() { | 176 size_t total_socket_required() { |
130 return (shared_socket_mode_ ? 1 : all_servers_addrs_.size()) * | 177 return (shared_socket_mode_ ? 1 : all_servers_addrs_.size()) * |
131 requests_per_ip_; | 178 requests_per_ip_; |
132 } | 179 } |
133 | 180 |
181 bool should_send_next_request(uint32_t now); | |
182 int get_wake_up_interval_ms(); | |
183 | |
134 bool SendNextRequest(); | 184 bool SendNextRequest(); |
135 | 185 |
136 // Will be invoked in 1ms intervals and schedule the next request from the | 186 // Will be invoked in 1ms intervals and schedule the next request from the |
137 // |current_requester_| if the time has passed for another request. | 187 // |current_requester_| if the time has passed for another request. |
138 void MaybeScheduleStunRequests(); | 188 void MaybeScheduleStunRequests(); |
139 | 189 |
140 // End the probe with the given |status|. Invokes |fininsh_callback|, which | 190 void ReportOnPrepared(StunProber::Status status); |
141 // may destroy the class. | 191 void ReportOnFinished(StunProber::Status status); |
142 void End(StunProber::Status status); | |
143 | 192 |
144 Requester* CreateRequester(); | 193 Requester* CreateRequester(); |
145 | 194 |
146 Requester* current_requester_ = nullptr; | 195 Requester* current_requester_ = nullptr; |
147 | 196 |
148 // The time when the next request should go out. | 197 // The time when the next request should go out. |
149 uint64_t next_request_time_ms_ = 0; | 198 uint64_t next_request_time_ms_ = 0; |
150 | 199 |
151 // Total requests sent so far. | 200 // Total requests sent so far. |
152 uint32_t num_request_sent_ = 0; | 201 uint32_t num_request_sent_ = 0; |
(...skipping 12 matching lines...) Expand all Loading... | |
165 // STUN server name to be resolved. | 214 // STUN server name to be resolved. |
166 std::vector<rtc::SocketAddress> servers_; | 215 std::vector<rtc::SocketAddress> servers_; |
167 | 216 |
168 // Weak references. | 217 // Weak references. |
169 rtc::PacketSocketFactory* socket_factory_; | 218 rtc::PacketSocketFactory* socket_factory_; |
170 rtc::Thread* thread_; | 219 rtc::Thread* thread_; |
171 | 220 |
172 // Accumulate all resolved addresses. | 221 // Accumulate all resolved addresses. |
173 std::vector<rtc::SocketAddress> all_servers_addrs_; | 222 std::vector<rtc::SocketAddress> all_servers_addrs_; |
174 | 223 |
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. | 224 // The set of STUN probe sockets and their state. |
180 std::vector<Requester*> requesters_; | 225 std::vector<Requester*> requesters_; |
181 | 226 |
182 rtc::ThreadChecker thread_checker_; | 227 rtc::ThreadChecker thread_checker_; |
183 | 228 |
184 // Temporary storage for created sockets. | 229 // Temporary storage for created sockets. |
185 std::vector<rtc::AsyncPacketSocket*> sockets_; | 230 std::vector<rtc::AsyncPacketSocket*> sockets_; |
186 // This tracks how many of the sockets are ready. | 231 // This tracks how many of the sockets are ready. |
187 size_t total_ready_sockets_ = 0; | 232 size_t total_ready_sockets_ = 0; |
188 | 233 |
189 rtc::AsyncInvoker invoker_; | 234 rtc::AsyncInvoker invoker_; |
190 | 235 |
236 Observer* observer_ = nullptr; | |
237 // TODO(guoweis): Remove this once all dependencies move away from | |
238 // AsyncCallback. | |
239 ObserverAdapter observer_adapter_; | |
240 | |
191 rtc::NetworkManager::NetworkList networks_; | 241 rtc::NetworkManager::NetworkList networks_; |
192 | 242 |
193 RTC_DISALLOW_COPY_AND_ASSIGN(StunProber); | 243 RTC_DISALLOW_COPY_AND_ASSIGN(StunProber); |
194 }; | 244 }; |
195 | 245 |
196 } // namespace stunprober | 246 } // namespace stunprober |
197 | 247 |
198 #endif // WEBRTC_P2P_STUNPROBER_STUNPROBER_H_ | 248 #endif // WEBRTC_P2P_STUNPROBER_STUNPROBER_H_ |
OLD | NEW |