OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 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 #include "webrtc/p2p/base/stunrequest.h" | 11 #include "webrtc/p2p/base/stunrequest.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <memory> | 14 #include <memory> |
15 | 15 |
16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
17 #include "webrtc/base/helpers.h" | 17 #include "webrtc/base/helpers.h" |
18 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
19 #include "webrtc/base/stringencode.h" | 19 #include "webrtc/base/stringencode.h" |
20 | 20 |
21 namespace cricket { | 21 namespace cricket { |
22 | 22 |
23 const uint32_t MSG_STUN_SEND = 1; | 23 const uint32_t MSG_STUN_SEND = 1; |
24 | 24 |
25 const int MAX_SENDS = 9; | 25 // RFC 5389 says SHOULD be 500ms. |
26 const int DELAY_UNIT = 100; // 100 milliseconds | 26 // For years, this was 100ms, but for networks that |
27 const int DELAY_MAX_FACTOR = 16; | 27 // experience moments of high RTT (such as 2G networks), this doesn't |
| 28 // work well. |
| 29 const int STUN_INITIAL_RTO = 250; // milliseconds |
| 30 |
| 31 // The timeout doubles each retransmission, up to this many times |
| 32 // RFC 5389 says SHOULD retransmit 7 times. |
| 33 // This has been 8 for years (not sure why). |
| 34 const int STUN_MAX_RETRANSMISSIONS = 8; // Total sends: 9 |
| 35 |
| 36 // We also cap the doubling, even though the standard doesn't say to. |
| 37 // This has been 1.6 seconds for years, but for networks that |
| 38 // experience moments of high RTT (such as 2G networks), this doesn't |
| 39 // work well. |
| 40 const int STUN_MAX_RTO = 8000; // milliseconds, or 5 doublings |
28 | 41 |
29 StunRequestManager::StunRequestManager(rtc::Thread* thread) | 42 StunRequestManager::StunRequestManager(rtc::Thread* thread) |
30 : thread_(thread) { | 43 : thread_(thread) { |
31 } | 44 } |
32 | 45 |
33 StunRequestManager::~StunRequestManager() { | 46 StunRequestManager::~StunRequestManager() { |
34 while (requests_.begin() != requests_.end()) { | 47 while (requests_.begin() != requests_.end()) { |
35 StunRequest *request = requests_.begin()->second; | 48 StunRequest *request = requests_.begin()->second; |
36 requests_.erase(requests_.begin()); | 49 requests_.erase(requests_.begin()); |
37 delete request; | 50 delete request; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 msg_->Write(&buf); | 232 msg_->Write(&buf); |
220 manager_->SignalSendPacket(buf.Data(), buf.Length(), this); | 233 manager_->SignalSendPacket(buf.Data(), buf.Length(), this); |
221 | 234 |
222 OnSent(); | 235 OnSent(); |
223 manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this, | 236 manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this, |
224 MSG_STUN_SEND, NULL); | 237 MSG_STUN_SEND, NULL); |
225 } | 238 } |
226 | 239 |
227 void StunRequest::OnSent() { | 240 void StunRequest::OnSent() { |
228 count_ += 1; | 241 count_ += 1; |
229 if (count_ == MAX_SENDS) { | 242 int retransmissions = (count_ - 1); |
| 243 if (retransmissions >= STUN_MAX_RETRANSMISSIONS) { |
230 timeout_ = true; | 244 timeout_ = true; |
231 } | 245 } |
232 LOG(LS_VERBOSE) << "Sent STUN request " << count_ | 246 LOG(LS_VERBOSE) << "Sent STUN request " << count_ |
233 << "; resend delay = " << resend_delay(); | 247 << "; resend delay = " << resend_delay(); |
234 } | 248 } |
235 | 249 |
236 int StunRequest::resend_delay() { | 250 int StunRequest::resend_delay() { |
237 if (count_ == 0) { | 251 if (count_ == 0) { |
238 return 0; | 252 return 0; |
239 } | 253 } |
240 return DELAY_UNIT * std::min(1 << (count_-1), DELAY_MAX_FACTOR); | 254 int retransmissions = (count_ - 1); |
| 255 int rto = STUN_INITIAL_RTO << retransmissions; |
| 256 return std::min(rto, STUN_MAX_RTO); |
241 } | 257 } |
242 | 258 |
243 } // namespace cricket | 259 } // namespace cricket |
OLD | NEW |