| 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 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 SendDelayed(request, 0); | 55 SendDelayed(request, 0); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void StunRequestManager::SendDelayed(StunRequest* request, int delay) { | 58 void StunRequestManager::SendDelayed(StunRequest* request, int delay) { |
| 59 request->set_manager(this); | 59 request->set_manager(this); |
| 60 RTC_DCHECK(requests_.find(request->id()) == requests_.end()); | 60 RTC_DCHECK(requests_.find(request->id()) == requests_.end()); |
| 61 request->set_origin(origin_); | 61 request->set_origin(origin_); |
| 62 request->Construct(); | 62 request->Construct(); |
| 63 requests_[request->id()] = request; | 63 requests_[request->id()] = request; |
| 64 if (delay > 0) { | 64 if (delay > 0) { |
| 65 thread_->PostDelayed(RTC_FROM_HERE, delay, request, MSG_STUN_SEND, NULL); | 65 thread_->PostDelayed(RTC_FROM_HERE, delay, request, MSG_STUN_SEND, nullptr); |
| 66 } else { | 66 } else { |
| 67 thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL); | 67 thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, nullptr); |
| 68 } | 68 } |
| 69 } | 69 } |
| 70 | 70 |
| 71 void StunRequestManager::Flush(int msg_type) { | 71 void StunRequestManager::Flush(int msg_type) { |
| 72 for (const auto kv : requests_) { | 72 for (const auto kv : requests_) { |
| 73 StunRequest* request = kv.second; | 73 StunRequest* request = kv.second; |
| 74 if (msg_type == kAllRequests || msg_type == request->type()) { | 74 if (msg_type == kAllRequests || msg_type == request->type()) { |
| 75 thread_->Clear(request, MSG_STUN_SEND); | 75 thread_->Clear(request, MSG_STUN_SEND); |
| 76 thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL); | 76 thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, nullptr); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 bool StunRequestManager::HasRequest(int msg_type) { | 81 bool StunRequestManager::HasRequest(int msg_type) { |
| 82 for (const auto kv : requests_) { | 82 for (const auto kv : requests_) { |
| 83 StunRequest* request = kv.second; | 83 StunRequest* request = kv.second; |
| 84 if (msg_type == kAllRequests || msg_type == request->type()) { | 84 if (msg_type == kAllRequests || msg_type == request->type()) { |
| 85 return true; | 85 return true; |
| 86 } | 86 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 } | 171 } |
| 172 | 172 |
| 173 StunRequest::StunRequest(StunMessage* request) | 173 StunRequest::StunRequest(StunMessage* request) |
| 174 : count_(0), timeout_(false), manager_(0), | 174 : count_(0), timeout_(false), manager_(0), |
| 175 msg_(request), tstamp_(0) { | 175 msg_(request), tstamp_(0) { |
| 176 msg_->SetTransactionID( | 176 msg_->SetTransactionID( |
| 177 rtc::CreateRandomString(kStunTransactionIdLength)); | 177 rtc::CreateRandomString(kStunTransactionIdLength)); |
| 178 } | 178 } |
| 179 | 179 |
| 180 StunRequest::~StunRequest() { | 180 StunRequest::~StunRequest() { |
| 181 RTC_DCHECK(manager_ != NULL); | 181 RTC_DCHECK(manager_ != nullptr); |
| 182 if (manager_) { | 182 if (manager_) { |
| 183 manager_->Remove(this); | 183 manager_->Remove(this); |
| 184 manager_->thread_->Clear(this); | 184 manager_->thread_->Clear(this); |
| 185 } | 185 } |
| 186 delete msg_; | 186 delete msg_; |
| 187 } | 187 } |
| 188 | 188 |
| 189 void StunRequest::Construct() { | 189 void StunRequest::Construct() { |
| 190 if (msg_->type() == 0) { | 190 if (msg_->type() == 0) { |
| 191 if (!origin_.empty()) { | 191 if (!origin_.empty()) { |
| 192 msg_->AddAttribute(new StunByteStringAttribute(STUN_ATTR_ORIGIN, | 192 msg_->AddAttribute(new StunByteStringAttribute(STUN_ATTR_ORIGIN, |
| 193 origin_)); | 193 origin_)); |
| 194 } | 194 } |
| 195 Prepare(msg_); | 195 Prepare(msg_); |
| 196 RTC_DCHECK(msg_->type() != 0); | 196 RTC_DCHECK(msg_->type() != 0); |
| 197 } | 197 } |
| 198 } | 198 } |
| 199 | 199 |
| 200 int StunRequest::type() { | 200 int StunRequest::type() { |
| 201 RTC_DCHECK(msg_ != NULL); | 201 RTC_DCHECK(msg_ != nullptr); |
| 202 return msg_->type(); | 202 return msg_->type(); |
| 203 } | 203 } |
| 204 | 204 |
| 205 const StunMessage* StunRequest::msg() const { | 205 const StunMessage* StunRequest::msg() const { |
| 206 return msg_; | 206 return msg_; |
| 207 } | 207 } |
| 208 | 208 |
| 209 int StunRequest::Elapsed() const { | 209 int StunRequest::Elapsed() const { |
| 210 return static_cast<int>(rtc::TimeMillis() - tstamp_); | 210 return static_cast<int>(rtc::TimeMillis() - tstamp_); |
| 211 } | 211 } |
| 212 | 212 |
| 213 | 213 |
| 214 void StunRequest::set_manager(StunRequestManager* manager) { | 214 void StunRequest::set_manager(StunRequestManager* manager) { |
| 215 RTC_DCHECK(!manager_); | 215 RTC_DCHECK(!manager_); |
| 216 manager_ = manager; | 216 manager_ = manager; |
| 217 } | 217 } |
| 218 | 218 |
| 219 void StunRequest::OnMessage(rtc::Message* pmsg) { | 219 void StunRequest::OnMessage(rtc::Message* pmsg) { |
| 220 RTC_DCHECK(manager_ != NULL); | 220 RTC_DCHECK(manager_ != nullptr); |
| 221 RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND); | 221 RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND); |
| 222 | 222 |
| 223 if (timeout_) { | 223 if (timeout_) { |
| 224 OnTimeout(); | 224 OnTimeout(); |
| 225 delete this; | 225 delete this; |
| 226 return; | 226 return; |
| 227 } | 227 } |
| 228 | 228 |
| 229 tstamp_ = rtc::TimeMillis(); | 229 tstamp_ = rtc::TimeMillis(); |
| 230 | 230 |
| 231 rtc::ByteBufferWriter buf; | 231 rtc::ByteBufferWriter buf; |
| 232 msg_->Write(&buf); | 232 msg_->Write(&buf); |
| 233 manager_->SignalSendPacket(buf.Data(), buf.Length(), this); | 233 manager_->SignalSendPacket(buf.Data(), buf.Length(), this); |
| 234 | 234 |
| 235 OnSent(); | 235 OnSent(); |
| 236 manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this, | 236 manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this, |
| 237 MSG_STUN_SEND, NULL); | 237 MSG_STUN_SEND, nullptr); |
| 238 } | 238 } |
| 239 | 239 |
| 240 void StunRequest::OnSent() { | 240 void StunRequest::OnSent() { |
| 241 count_ += 1; | 241 count_ += 1; |
| 242 int retransmissions = (count_ - 1); | 242 int retransmissions = (count_ - 1); |
| 243 if (retransmissions >= STUN_MAX_RETRANSMISSIONS) { | 243 if (retransmissions >= STUN_MAX_RETRANSMISSIONS) { |
| 244 timeout_ = true; | 244 timeout_ = true; |
| 245 } | 245 } |
| 246 LOG(LS_VERBOSE) << "Sent STUN request " << count_ | 246 LOG(LS_VERBOSE) << "Sent STUN request " << count_ |
| 247 << "; resend delay = " << resend_delay(); | 247 << "; resend delay = " << resend_delay(); |
| 248 } | 248 } |
| 249 | 249 |
| 250 int StunRequest::resend_delay() { | 250 int StunRequest::resend_delay() { |
| 251 if (count_ == 0) { | 251 if (count_ == 0) { |
| 252 return 0; | 252 return 0; |
| 253 } | 253 } |
| 254 int retransmissions = (count_ - 1); | 254 int retransmissions = (count_ - 1); |
| 255 int rto = STUN_INITIAL_RTO << retransmissions; | 255 int rto = STUN_INITIAL_RTO << retransmissions; |
| 256 return std::min(rto, STUN_MAX_RTO); | 256 return std::min(rto, STUN_MAX_RTO); |
| 257 } | 257 } |
| 258 | 258 |
| 259 } // namespace cricket | 259 } // namespace cricket |
| OLD | NEW |