| 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 then it was discovered that this doesn't |
| 27 const int DELAY_MAX_FACTOR = 16; | 27 // work well for networks with a high RTT (such as 2G networks). |
| 28 const int STUN_INITIAL_RTO = 250; // milliseconds |
| 29 // The timeout doubles each retransmission, up to this many times |
| 30 // RFC 5389 says SHOULD retransmit 7 times. |
| 31 const int STUN_MAX_RETRANSMISSIONS = 8; // Total sends: 9 |
| 32 const int STUN_MAX_SENDS = STUN_MAX_RETRANSMISSIONS + 1; |
| 33 // We also cap the doubling, even though the standard doesn't say to. |
| 34 const int STUN_MAX_RTO = 8000; // milliseconds, or 5 doublings |
| 35 // Total max timeouts: 39.75 seconds |
| 36 // For years, this was 9.5 seconds, but for networks that experience moments of |
| 37 // high RTT (such as 40s on 2G networks), this doesn't work well. |
| 28 | 38 |
| 29 StunRequestManager::StunRequestManager(rtc::Thread* thread) | 39 StunRequestManager::StunRequestManager(rtc::Thread* thread) |
| 30 : thread_(thread) { | 40 : thread_(thread) { |
| 31 } | 41 } |
| 32 | 42 |
| 33 StunRequestManager::~StunRequestManager() { | 43 StunRequestManager::~StunRequestManager() { |
| 34 while (requests_.begin() != requests_.end()) { | 44 while (requests_.begin() != requests_.end()) { |
| 35 StunRequest *request = requests_.begin()->second; | 45 StunRequest *request = requests_.begin()->second; |
| 36 requests_.erase(requests_.begin()); | 46 requests_.erase(requests_.begin()); |
| 37 delete request; | 47 delete request; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 msg_->Write(&buf); | 229 msg_->Write(&buf); |
| 220 manager_->SignalSendPacket(buf.Data(), buf.Length(), this); | 230 manager_->SignalSendPacket(buf.Data(), buf.Length(), this); |
| 221 | 231 |
| 222 OnSent(); | 232 OnSent(); |
| 223 manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this, | 233 manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this, |
| 224 MSG_STUN_SEND, NULL); | 234 MSG_STUN_SEND, NULL); |
| 225 } | 235 } |
| 226 | 236 |
| 227 void StunRequest::OnSent() { | 237 void StunRequest::OnSent() { |
| 228 count_ += 1; | 238 count_ += 1; |
| 229 if (count_ == MAX_SENDS) { | 239 if (count_ == STUN_MAX_SENDS) { |
| 230 timeout_ = true; | 240 timeout_ = true; |
| 231 } | 241 } |
| 232 LOG(LS_VERBOSE) << "Sent STUN request " << count_ | 242 LOG(LS_VERBOSE) << "Sent STUN request " << count_ |
| 233 << "; resend delay = " << resend_delay(); | 243 << "; resend delay = " << resend_delay(); |
| 234 } | 244 } |
| 235 | 245 |
| 236 int StunRequest::resend_delay() { | 246 int StunRequest::resend_delay() { |
| 237 if (count_ == 0) { | 247 if (count_ == 0) { |
| 238 return 0; | 248 return 0; |
| 239 } | 249 } |
| 240 return DELAY_UNIT * std::min(1 << (count_-1), DELAY_MAX_FACTOR); | 250 return std::min((STUN_INITIAL_RTO << (count_ - 1)), STUN_MAX_RTO); |
| 241 } | 251 } |
| 242 | 252 |
| 243 } // namespace cricket | 253 } // namespace cricket |
| OLD | NEW |