| 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/common.h" | 17 #include "webrtc/base/common.h" | 
| 17 #include "webrtc/base/helpers.h" | 18 #include "webrtc/base/helpers.h" | 
| 18 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" | 
| 19 #include "webrtc/base/stringencode.h" | 20 #include "webrtc/base/stringencode.h" | 
| 20 | 21 | 
| 21 namespace cricket { | 22 namespace cricket { | 
| 22 | 23 | 
| 23 const uint32_t MSG_STUN_SEND = 1; | 24 const uint32_t MSG_STUN_SEND = 1; | 
| 24 | 25 | 
| 25 const int MAX_SENDS = 9; | 26 const int MAX_SENDS = 9; | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 37     delete request; | 38     delete request; | 
| 38   } | 39   } | 
| 39 } | 40 } | 
| 40 | 41 | 
| 41 void StunRequestManager::Send(StunRequest* request) { | 42 void StunRequestManager::Send(StunRequest* request) { | 
| 42   SendDelayed(request, 0); | 43   SendDelayed(request, 0); | 
| 43 } | 44 } | 
| 44 | 45 | 
| 45 void StunRequestManager::SendDelayed(StunRequest* request, int delay) { | 46 void StunRequestManager::SendDelayed(StunRequest* request, int delay) { | 
| 46   request->set_manager(this); | 47   request->set_manager(this); | 
| 47   ASSERT(requests_.find(request->id()) == requests_.end()); | 48   RTC_DCHECK(requests_.find(request->id()) == requests_.end()); | 
| 48   request->set_origin(origin_); | 49   request->set_origin(origin_); | 
| 49   request->Construct(); | 50   request->Construct(); | 
| 50   requests_[request->id()] = request; | 51   requests_[request->id()] = request; | 
| 51   if (delay > 0) { | 52   if (delay > 0) { | 
| 52     thread_->PostDelayed(RTC_FROM_HERE, delay, request, MSG_STUN_SEND, NULL); | 53     thread_->PostDelayed(RTC_FROM_HERE, delay, request, MSG_STUN_SEND, NULL); | 
| 53   } else { | 54   } else { | 
| 54     thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL); | 55     thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL); | 
| 55   } | 56   } | 
| 56 } | 57 } | 
| 57 | 58 | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 69   for (const auto kv : requests_) { | 70   for (const auto kv : requests_) { | 
| 70     StunRequest* request = kv.second; | 71     StunRequest* request = kv.second; | 
| 71     if (msg_type == kAllRequests || msg_type == request->type()) { | 72     if (msg_type == kAllRequests || msg_type == request->type()) { | 
| 72       return true; | 73       return true; | 
| 73     } | 74     } | 
| 74   } | 75   } | 
| 75   return false; | 76   return false; | 
| 76 } | 77 } | 
| 77 | 78 | 
| 78 void StunRequestManager::Remove(StunRequest* request) { | 79 void StunRequestManager::Remove(StunRequest* request) { | 
| 79   ASSERT(request->manager() == this); | 80   RTC_DCHECK(request->manager() == this); | 
| 80   RequestMap::iterator iter = requests_.find(request->id()); | 81   RequestMap::iterator iter = requests_.find(request->id()); | 
| 81   if (iter != requests_.end()) { | 82   if (iter != requests_.end()) { | 
| 82     ASSERT(iter->second == request); | 83     RTC_DCHECK(iter->second == request); | 
| 83     requests_.erase(iter); | 84     requests_.erase(iter); | 
| 84     thread_->Clear(request); | 85     thread_->Clear(request); | 
| 85   } | 86   } | 
| 86 } | 87 } | 
| 87 | 88 | 
| 88 void StunRequestManager::Clear() { | 89 void StunRequestManager::Clear() { | 
| 89   std::vector<StunRequest*> requests; | 90   std::vector<StunRequest*> requests; | 
| 90   for (RequestMap::iterator i = requests_.begin(); i != requests_.end(); ++i) | 91   for (RequestMap::iterator i = requests_.begin(); i != requests_.end(); ++i) | 
| 91     requests.push_back(i->second); | 92     requests.push_back(i->second); | 
| 92 | 93 | 
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 158 } | 159 } | 
| 159 | 160 | 
| 160 StunRequest::StunRequest(StunMessage* request) | 161 StunRequest::StunRequest(StunMessage* request) | 
| 161     : count_(0), timeout_(false), manager_(0), | 162     : count_(0), timeout_(false), manager_(0), | 
| 162       msg_(request), tstamp_(0) { | 163       msg_(request), tstamp_(0) { | 
| 163   msg_->SetTransactionID( | 164   msg_->SetTransactionID( | 
| 164       rtc::CreateRandomString(kStunTransactionIdLength)); | 165       rtc::CreateRandomString(kStunTransactionIdLength)); | 
| 165 } | 166 } | 
| 166 | 167 | 
| 167 StunRequest::~StunRequest() { | 168 StunRequest::~StunRequest() { | 
| 168   ASSERT(manager_ != NULL); | 169   RTC_DCHECK(manager_ != NULL); | 
| 169   if (manager_) { | 170   if (manager_) { | 
| 170     manager_->Remove(this); | 171     manager_->Remove(this); | 
| 171     manager_->thread_->Clear(this); | 172     manager_->thread_->Clear(this); | 
| 172   } | 173   } | 
| 173   delete msg_; | 174   delete msg_; | 
| 174 } | 175 } | 
| 175 | 176 | 
| 176 void StunRequest::Construct() { | 177 void StunRequest::Construct() { | 
| 177   if (msg_->type() == 0) { | 178   if (msg_->type() == 0) { | 
| 178     if (!origin_.empty()) { | 179     if (!origin_.empty()) { | 
| 179       msg_->AddAttribute(new StunByteStringAttribute(STUN_ATTR_ORIGIN, | 180       msg_->AddAttribute(new StunByteStringAttribute(STUN_ATTR_ORIGIN, | 
| 180           origin_)); | 181           origin_)); | 
| 181     } | 182     } | 
| 182     Prepare(msg_); | 183     Prepare(msg_); | 
| 183     ASSERT(msg_->type() != 0); | 184     RTC_DCHECK(msg_->type() != 0); | 
| 184   } | 185   } | 
| 185 } | 186 } | 
| 186 | 187 | 
| 187 int StunRequest::type() { | 188 int StunRequest::type() { | 
| 188   ASSERT(msg_ != NULL); | 189   RTC_DCHECK(msg_ != NULL); | 
| 189   return msg_->type(); | 190   return msg_->type(); | 
| 190 } | 191 } | 
| 191 | 192 | 
| 192 const StunMessage* StunRequest::msg() const { | 193 const StunMessage* StunRequest::msg() const { | 
| 193   return msg_; | 194   return msg_; | 
| 194 } | 195 } | 
| 195 | 196 | 
| 196 int StunRequest::Elapsed() const { | 197 int StunRequest::Elapsed() const { | 
| 197   return static_cast<int>(rtc::TimeMillis() - tstamp_); | 198   return static_cast<int>(rtc::TimeMillis() - tstamp_); | 
| 198 } | 199 } | 
| 199 | 200 | 
| 200 | 201 | 
| 201 void StunRequest::set_manager(StunRequestManager* manager) { | 202 void StunRequest::set_manager(StunRequestManager* manager) { | 
| 202   ASSERT(!manager_); | 203   RTC_DCHECK(!manager_); | 
| 203   manager_ = manager; | 204   manager_ = manager; | 
| 204 } | 205 } | 
| 205 | 206 | 
| 206 void StunRequest::OnMessage(rtc::Message* pmsg) { | 207 void StunRequest::OnMessage(rtc::Message* pmsg) { | 
| 207   ASSERT(manager_ != NULL); | 208   RTC_DCHECK(manager_ != NULL); | 
| 208   ASSERT(pmsg->message_id == MSG_STUN_SEND); | 209   RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND); | 
| 209 | 210 | 
| 210   if (timeout_) { | 211   if (timeout_) { | 
| 211     OnTimeout(); | 212     OnTimeout(); | 
| 212     delete this; | 213     delete this; | 
| 213     return; | 214     return; | 
| 214   } | 215   } | 
| 215 | 216 | 
| 216   tstamp_ = rtc::TimeMillis(); | 217   tstamp_ = rtc::TimeMillis(); | 
| 217 | 218 | 
| 218   rtc::ByteBufferWriter buf; | 219   rtc::ByteBufferWriter buf; | 
| (...skipping 15 matching lines...) Expand all  Loading... | 
| 234 } | 235 } | 
| 235 | 236 | 
| 236 int StunRequest::resend_delay() { | 237 int StunRequest::resend_delay() { | 
| 237   if (count_ == 0) { | 238   if (count_ == 0) { | 
| 238     return 0; | 239     return 0; | 
| 239   } | 240   } | 
| 240   return DELAY_UNIT * std::min(1 << (count_-1), DELAY_MAX_FACTOR); | 241   return DELAY_UNIT * std::min(1 << (count_-1), DELAY_MAX_FACTOR); | 
| 241 } | 242 } | 
| 242 | 243 | 
| 243 }  // namespace cricket | 244 }  // namespace cricket | 
| OLD | NEW | 
|---|