| 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/p2ptransportchannel.h" | 11 #include "webrtc/p2p/base/p2ptransportchannel.h" |
| 12 | 12 |
| 13 #include <set> | 13 #include <set> |
| 14 #include <algorithm> | |
| 15 #include "webrtc/p2p/base/common.h" | 14 #include "webrtc/p2p/base/common.h" |
| 16 #include "webrtc/p2p/base/relayport.h" // For RELAY_PORT_TYPE. | 15 #include "webrtc/p2p/base/relayport.h" // For RELAY_PORT_TYPE. |
| 17 #include "webrtc/p2p/base/stunport.h" // For STUN_PORT_TYPE. | 16 #include "webrtc/p2p/base/stunport.h" // For STUN_PORT_TYPE. |
| 18 #include "webrtc/base/common.h" | 17 #include "webrtc/base/common.h" |
| 19 #include "webrtc/base/crc32.h" | 18 #include "webrtc/base/crc32.h" |
| 20 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
| 21 #include "webrtc/base/stringencode.h" | 20 #include "webrtc/base/stringencode.h" |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 if (prefs_cmp > 0) | 162 if (prefs_cmp > 0) |
| 164 return false; | 163 return false; |
| 165 | 164 |
| 166 return b_conn->rtt() <= a_conn->rtt() + kMinImprovement; | 165 return b_conn->rtt() <= a_conn->rtt() + kMinImprovement; |
| 167 } | 166 } |
| 168 | 167 |
| 169 } // unnamed namespace | 168 } // unnamed namespace |
| 170 | 169 |
| 171 namespace cricket { | 170 namespace cricket { |
| 172 | 171 |
| 173 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, | 172 P2PTransportChannel::P2PTransportChannel(const std::string& content_name, |
| 174 int component, | 173 int component, |
| 175 P2PTransport* transport, | 174 P2PTransport* transport, |
| 176 PortAllocator* allocator) | 175 PortAllocator *allocator) : |
| 177 : TransportChannelImpl(transport_name, component), | 176 TransportChannelImpl(content_name, component), |
| 178 transport_(transport), | 177 transport_(transport), |
| 179 allocator_(allocator), | 178 allocator_(allocator), |
| 180 worker_thread_(rtc::Thread::Current()), | 179 worker_thread_(rtc::Thread::Current()), |
| 181 incoming_only_(false), | 180 incoming_only_(false), |
| 182 error_(0), | 181 waiting_for_signaling_(false), |
| 183 best_connection_(NULL), | 182 error_(0), |
| 184 pending_best_connection_(NULL), | 183 best_connection_(NULL), |
| 185 sort_dirty_(false), | 184 pending_best_connection_(NULL), |
| 186 was_writable_(false), | 185 sort_dirty_(false), |
| 187 remote_ice_mode_(ICEMODE_FULL), | 186 was_writable_(false), |
| 188 ice_role_(ICEROLE_UNKNOWN), | 187 remote_ice_mode_(ICEMODE_FULL), |
| 189 tiebreaker_(0), | 188 ice_role_(ICEROLE_UNKNOWN), |
| 190 remote_candidate_generation_(0), | 189 tiebreaker_(0), |
| 191 gathering_state_(kIceGatheringNew), | 190 remote_candidate_generation_(0), |
| 192 check_receiving_delay_(MIN_CHECK_RECEIVING_DELAY * 5), | 191 check_receiving_delay_(MIN_CHECK_RECEIVING_DELAY * 5), |
| 193 receiving_timeout_(MIN_CHECK_RECEIVING_DELAY * 50) {} | 192 receiving_timeout_(MIN_CHECK_RECEIVING_DELAY * 50) { |
| 193 } |
| 194 | 194 |
| 195 P2PTransportChannel::~P2PTransportChannel() { | 195 P2PTransportChannel::~P2PTransportChannel() { |
| 196 ASSERT(worker_thread_ == rtc::Thread::Current()); | 196 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 197 | 197 |
| 198 for (uint32 i = 0; i < allocator_sessions_.size(); ++i) | 198 for (uint32 i = 0; i < allocator_sessions_.size(); ++i) |
| 199 delete allocator_sessions_[i]; | 199 delete allocator_sessions_[i]; |
| 200 } | 200 } |
| 201 | 201 |
| 202 // Add the allocator session to our list so that we know which sessions | 202 // Add the allocator session to our list so that we know which sessions |
| 203 // are still active. | 203 // are still active. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 224 connection->set_receiving_timeout(receiving_timeout_); | 224 connection->set_receiving_timeout(receiving_timeout_); |
| 225 connection->SignalReadPacket.connect( | 225 connection->SignalReadPacket.connect( |
| 226 this, &P2PTransportChannel::OnReadPacket); | 226 this, &P2PTransportChannel::OnReadPacket); |
| 227 connection->SignalReadyToSend.connect( | 227 connection->SignalReadyToSend.connect( |
| 228 this, &P2PTransportChannel::OnReadyToSend); | 228 this, &P2PTransportChannel::OnReadyToSend); |
| 229 connection->SignalStateChange.connect( | 229 connection->SignalStateChange.connect( |
| 230 this, &P2PTransportChannel::OnConnectionStateChange); | 230 this, &P2PTransportChannel::OnConnectionStateChange); |
| 231 connection->SignalDestroyed.connect( | 231 connection->SignalDestroyed.connect( |
| 232 this, &P2PTransportChannel::OnConnectionDestroyed); | 232 this, &P2PTransportChannel::OnConnectionDestroyed); |
| 233 connection->SignalNominated.connect(this, &P2PTransportChannel::OnNominated); | 233 connection->SignalNominated.connect(this, &P2PTransportChannel::OnNominated); |
| 234 had_connection_ = true; | |
| 235 } | 234 } |
| 236 | 235 |
| 237 void P2PTransportChannel::SetIceRole(IceRole ice_role) { | 236 void P2PTransportChannel::SetIceRole(IceRole ice_role) { |
| 238 ASSERT(worker_thread_ == rtc::Thread::Current()); | 237 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 239 if (ice_role_ != ice_role) { | 238 if (ice_role_ != ice_role) { |
| 240 ice_role_ = ice_role; | 239 ice_role_ = ice_role; |
| 241 for (std::vector<PortInterface *>::iterator it = ports_.begin(); | 240 for (std::vector<PortInterface *>::iterator it = ports_.begin(); |
| 242 it != ports_.end(); ++it) { | 241 it != ports_.end(); ++it) { |
| 243 (*it)->SetIceRole(ice_role); | 242 (*it)->SetIceRole(ice_role); |
| 244 } | 243 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 259 // Currently a channel is considered ICE completed once there is no | 258 // Currently a channel is considered ICE completed once there is no |
| 260 // more than one connection per Network. This works for a single NIC | 259 // more than one connection per Network. This works for a single NIC |
| 261 // with both IPv4 and IPv6 enabled. However, this condition won't | 260 // with both IPv4 and IPv6 enabled. However, this condition won't |
| 262 // happen when there are multiple NICs and all of them have | 261 // happen when there are multiple NICs and all of them have |
| 263 // connectivity. | 262 // connectivity. |
| 264 // TODO(guoweis): Change Completion to be driven by a channel level | 263 // TODO(guoweis): Change Completion to be driven by a channel level |
| 265 // timer. | 264 // timer. |
| 266 TransportChannelState P2PTransportChannel::GetState() const { | 265 TransportChannelState P2PTransportChannel::GetState() const { |
| 267 std::set<rtc::Network*> networks; | 266 std::set<rtc::Network*> networks; |
| 268 | 267 |
| 269 if (connections_.empty()) { | 268 if (connections_.size() == 0) { |
| 270 return had_connection_ ? TransportChannelState::STATE_FAILED | 269 return TransportChannelState::STATE_FAILED; |
| 271 : TransportChannelState::STATE_INIT; | |
| 272 } | 270 } |
| 273 | 271 |
| 274 for (uint32 i = 0; i < connections_.size(); ++i) { | 272 for (uint32 i = 0; i < connections_.size(); ++i) { |
| 275 rtc::Network* network = connections_[i]->port()->Network(); | 273 rtc::Network* network = connections_[i]->port()->Network(); |
| 276 if (networks.find(network) == networks.end()) { | 274 if (networks.find(network) == networks.end()) { |
| 277 networks.insert(network); | 275 networks.insert(network); |
| 278 } else { | 276 } else { |
| 279 LOG_J(LS_VERBOSE, this) << "Ice not completed yet for this channel as " | 277 LOG_J(LS_VERBOSE, this) << "Ice not completed yet for this channel as " |
| 280 << network->ToString() | 278 << network->ToString() |
| 281 << " has more than 1 connection."; | 279 << " has more than 1 connection."; |
| 282 return TransportChannelState::STATE_CONNECTING; | 280 return TransportChannelState::STATE_CONNECTING; |
| 283 } | 281 } |
| 284 } | 282 } |
| 285 LOG_J(LS_VERBOSE, this) << "Ice is completed for this channel."; | 283 LOG_J(LS_VERBOSE, this) << "Ice is completed for this channel."; |
| 286 | 284 |
| 287 return TransportChannelState::STATE_COMPLETED; | 285 return TransportChannelState::STATE_COMPLETED; |
| 288 } | 286 } |
| 289 | 287 |
| 290 void P2PTransportChannel::SetIceCredentials(const std::string& ice_ufrag, | 288 void P2PTransportChannel::SetIceCredentials(const std::string& ice_ufrag, |
| 291 const std::string& ice_pwd) { | 289 const std::string& ice_pwd) { |
| 292 ASSERT(worker_thread_ == rtc::Thread::Current()); | 290 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 291 bool ice_restart = false; |
| 292 if (!ice_ufrag_.empty() && !ice_pwd_.empty()) { |
| 293 // Restart candidate allocation if there is any change in either |
| 294 // ice ufrag or password. |
| 295 ice_restart = |
| 296 IceCredentialsChanged(ice_ufrag_, ice_pwd_, ice_ufrag, ice_pwd); |
| 297 } |
| 298 |
| 293 ice_ufrag_ = ice_ufrag; | 299 ice_ufrag_ = ice_ufrag; |
| 294 ice_pwd_ = ice_pwd; | 300 ice_pwd_ = ice_pwd; |
| 295 // Note: Candidate gathering will restart when MaybeStartGathering is next | 301 |
| 296 // called. | 302 if (ice_restart) { |
| 303 // Restart candidate gathering. |
| 304 Allocate(); |
| 305 } |
| 297 } | 306 } |
| 298 | 307 |
| 299 void P2PTransportChannel::SetRemoteIceCredentials(const std::string& ice_ufrag, | 308 void P2PTransportChannel::SetRemoteIceCredentials(const std::string& ice_ufrag, |
| 300 const std::string& ice_pwd) { | 309 const std::string& ice_pwd) { |
| 301 ASSERT(worker_thread_ == rtc::Thread::Current()); | 310 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 302 bool ice_restart = false; | 311 bool ice_restart = false; |
| 303 if (!remote_ice_ufrag_.empty() && !remote_ice_pwd_.empty()) { | 312 if (!remote_ice_ufrag_.empty() && !remote_ice_pwd_.empty()) { |
| 304 ice_restart = (remote_ice_ufrag_ != ice_ufrag) || | 313 ice_restart = (remote_ice_ufrag_ != ice_ufrag) || |
| 305 (remote_ice_pwd_!= ice_pwd); | 314 (remote_ice_pwd_!= ice_pwd); |
| 306 } | 315 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 // Go into the state of processing candidates, and running in general | 352 // Go into the state of processing candidates, and running in general |
| 344 void P2PTransportChannel::Connect() { | 353 void P2PTransportChannel::Connect() { |
| 345 ASSERT(worker_thread_ == rtc::Thread::Current()); | 354 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 346 if (ice_ufrag_.empty() || ice_pwd_.empty()) { | 355 if (ice_ufrag_.empty() || ice_pwd_.empty()) { |
| 347 ASSERT(false); | 356 ASSERT(false); |
| 348 LOG(LS_ERROR) << "P2PTransportChannel::Connect: The ice_ufrag_ and the " | 357 LOG(LS_ERROR) << "P2PTransportChannel::Connect: The ice_ufrag_ and the " |
| 349 << "ice_pwd_ are not set."; | 358 << "ice_pwd_ are not set."; |
| 350 return; | 359 return; |
| 351 } | 360 } |
| 352 | 361 |
| 362 // Kick off an allocator session |
| 363 Allocate(); |
| 364 |
| 353 // Start pinging as the ports come in. | 365 // Start pinging as the ports come in. |
| 354 thread()->Post(this, MSG_PING); | 366 thread()->Post(this, MSG_PING); |
| 355 | 367 |
| 356 thread()->PostDelayed( | 368 thread()->PostDelayed( |
| 357 check_receiving_delay_, this, MSG_CHECK_RECEIVING); | 369 check_receiving_delay_, this, MSG_CHECK_RECEIVING); |
| 358 } | 370 } |
| 359 | 371 |
| 360 void P2PTransportChannel::MaybeStartGathering() { | |
| 361 // Start gathering if we never started before, or if an ICE restart occurred. | |
| 362 if (allocator_sessions_.empty() || | |
| 363 IceCredentialsChanged(allocator_sessions_.back()->ice_ufrag(), | |
| 364 allocator_sessions_.back()->ice_pwd(), ice_ufrag_, | |
| 365 ice_pwd_)) { | |
| 366 if (gathering_state_ != kIceGatheringGathering) { | |
| 367 gathering_state_ = kIceGatheringGathering; | |
| 368 SignalGatheringState(this); | |
| 369 } | |
| 370 // Time for a new allocator | |
| 371 AddAllocatorSession(allocator_->CreateSession( | |
| 372 SessionId(), transport_name(), component(), ice_ufrag_, ice_pwd_)); | |
| 373 } | |
| 374 } | |
| 375 | |
| 376 // A new port is available, attempt to make connections for it | 372 // A new port is available, attempt to make connections for it |
| 377 void P2PTransportChannel::OnPortReady(PortAllocatorSession *session, | 373 void P2PTransportChannel::OnPortReady(PortAllocatorSession *session, |
| 378 PortInterface* port) { | 374 PortInterface* port) { |
| 379 ASSERT(worker_thread_ == rtc::Thread::Current()); | 375 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 380 | 376 |
| 381 // Set in-effect options on the new port | 377 // Set in-effect options on the new port |
| 382 for (OptionMap::const_iterator it = options_.begin(); | 378 for (OptionMap::const_iterator it = options_.begin(); |
| 383 it != options_.end(); | 379 it != options_.end(); |
| 384 ++it) { | 380 ++it) { |
| 385 int val = port->SetOption(it->first, it->second); | 381 int val = port->SetOption(it->first, it->second); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 410 for (iter = remote_candidates_.begin(); iter != remote_candidates_.end(); | 406 for (iter = remote_candidates_.begin(); iter != remote_candidates_.end(); |
| 411 ++iter) { | 407 ++iter) { |
| 412 CreateConnection(port, *iter, iter->origin_port()); | 408 CreateConnection(port, *iter, iter->origin_port()); |
| 413 } | 409 } |
| 414 | 410 |
| 415 SortConnections(); | 411 SortConnections(); |
| 416 } | 412 } |
| 417 | 413 |
| 418 // A new candidate is available, let listeners know | 414 // A new candidate is available, let listeners know |
| 419 void P2PTransportChannel::OnCandidatesReady( | 415 void P2PTransportChannel::OnCandidatesReady( |
| 420 PortAllocatorSession* session, | 416 PortAllocatorSession *session, const std::vector<Candidate>& candidates) { |
| 421 const std::vector<Candidate>& candidates) { | |
| 422 ASSERT(worker_thread_ == rtc::Thread::Current()); | 417 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 423 for (size_t i = 0; i < candidates.size(); ++i) { | 418 for (size_t i = 0; i < candidates.size(); ++i) { |
| 424 SignalCandidateGathered(this, candidates[i]); | 419 SignalCandidateReady(this, candidates[i]); |
| 425 } | 420 } |
| 426 } | 421 } |
| 427 | 422 |
| 428 void P2PTransportChannel::OnCandidatesAllocationDone( | 423 void P2PTransportChannel::OnCandidatesAllocationDone( |
| 429 PortAllocatorSession* session) { | 424 PortAllocatorSession* session) { |
| 430 ASSERT(worker_thread_ == rtc::Thread::Current()); | 425 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 431 gathering_state_ = kIceGatheringComplete; | 426 SignalCandidatesAllocationDone(this); |
| 432 LOG(LS_INFO) << "P2PTransportChannel: " << transport_name() << ", component " | |
| 433 << component() << " gathering complete"; | |
| 434 SignalGatheringState(this); | |
| 435 } | 427 } |
| 436 | 428 |
| 437 // Handle stun packets | 429 // Handle stun packets |
| 438 void P2PTransportChannel::OnUnknownAddress( | 430 void P2PTransportChannel::OnUnknownAddress( |
| 439 PortInterface* port, | 431 PortInterface* port, |
| 440 const rtc::SocketAddress& address, ProtocolType proto, | 432 const rtc::SocketAddress& address, ProtocolType proto, |
| 441 IceMessage* stun_msg, const std::string &remote_username, | 433 IceMessage* stun_msg, const std::string &remote_username, |
| 442 bool port_muxed) { | 434 bool port_muxed) { |
| 443 ASSERT(worker_thread_ == rtc::Thread::Current()); | 435 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 444 | 436 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 int remote_candidate_priority; | 487 int remote_candidate_priority; |
| 496 | 488 |
| 497 // The priority of the candidate is set to the PRIORITY attribute | 489 // The priority of the candidate is set to the PRIORITY attribute |
| 498 // from the request. | 490 // from the request. |
| 499 const StunUInt32Attribute* priority_attr = | 491 const StunUInt32Attribute* priority_attr = |
| 500 stun_msg->GetUInt32(STUN_ATTR_PRIORITY); | 492 stun_msg->GetUInt32(STUN_ATTR_PRIORITY); |
| 501 if (!priority_attr) { | 493 if (!priority_attr) { |
| 502 LOG(LS_WARNING) << "P2PTransportChannel::OnUnknownAddress - " | 494 LOG(LS_WARNING) << "P2PTransportChannel::OnUnknownAddress - " |
| 503 << "No STUN_ATTR_PRIORITY found in the " | 495 << "No STUN_ATTR_PRIORITY found in the " |
| 504 << "stun request message"; | 496 << "stun request message"; |
| 505 port->SendBindingErrorResponse(stun_msg, address, STUN_ERROR_BAD_REQUEST, | 497 port->SendBindingErrorResponse(stun_msg, address, |
| 498 STUN_ERROR_BAD_REQUEST, |
| 506 STUN_ERROR_REASON_BAD_REQUEST); | 499 STUN_ERROR_REASON_BAD_REQUEST); |
| 507 return; | 500 return; |
| 508 } | 501 } |
| 509 remote_candidate_priority = priority_attr->value(); | 502 remote_candidate_priority = priority_attr->value(); |
| 510 | 503 |
| 511 // RFC 5245 | 504 // RFC 5245 |
| 512 // If the source transport address of the request does not match any | 505 // If the source transport address of the request does not match any |
| 513 // existing remote candidates, it represents a new peer reflexive remote | 506 // existing remote candidates, it represents a new peer reflexive remote |
| 514 // candidate. | 507 // candidate. |
| 515 remote_candidate = | 508 remote_candidate = |
| (...skipping 29 matching lines...) Expand all Loading... |
| 545 STUN_ERROR_SERVER_ERROR, | 538 STUN_ERROR_SERVER_ERROR, |
| 546 STUN_ERROR_REASON_SERVER_ERROR); | 539 STUN_ERROR_REASON_SERVER_ERROR); |
| 547 return; | 540 return; |
| 548 } | 541 } |
| 549 } | 542 } |
| 550 | 543 |
| 551 Connection* connection = port->CreateConnection( | 544 Connection* connection = port->CreateConnection( |
| 552 remote_candidate, cricket::PortInterface::ORIGIN_THIS_PORT); | 545 remote_candidate, cricket::PortInterface::ORIGIN_THIS_PORT); |
| 553 if (!connection) { | 546 if (!connection) { |
| 554 ASSERT(false); | 547 ASSERT(false); |
| 555 port->SendBindingErrorResponse(stun_msg, address, STUN_ERROR_SERVER_ERROR, | 548 port->SendBindingErrorResponse(stun_msg, address, |
| 549 STUN_ERROR_SERVER_ERROR, |
| 556 STUN_ERROR_REASON_SERVER_ERROR); | 550 STUN_ERROR_REASON_SERVER_ERROR); |
| 557 return; | 551 return; |
| 558 } | 552 } |
| 559 | 553 |
| 560 LOG(LS_INFO) << "Adding connection from " | 554 LOG(LS_INFO) << "Adding connection from " |
| 561 << (remote_candidate_is_new ? "peer reflexive" : "resurrected") | 555 << (remote_candidate_is_new ? "peer reflexive" : "resurrected") |
| 562 << " candidate: " << remote_candidate.ToString(); | 556 << " candidate: " << remote_candidate.ToString(); |
| 563 AddConnection(connection); | 557 AddConnection(connection); |
| 564 connection->ReceivedPing(); | 558 connection->ReceivedPing(); |
| 565 | 559 |
| 566 bool received_use_candidate = | 560 bool received_use_candidate = |
| 567 stun_msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != nullptr; | 561 stun_msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != nullptr; |
| 568 if (received_use_candidate && ice_role_ == ICEROLE_CONTROLLED) { | 562 if (received_use_candidate && ice_role_ == ICEROLE_CONTROLLED) { |
| 569 connection->set_nominated(true); | 563 connection->set_nominated(true); |
| 570 OnNominated(connection); | 564 OnNominated(connection); |
| 571 } | 565 } |
| 572 | 566 |
| 573 // Update the list of connections since we just added another. We do this | 567 // Update the list of connections since we just added another. We do this |
| 574 // after sending the response since it could (in principle) delete the | 568 // after sending the response since it could (in principle) delete the |
| 575 // connection in question. | 569 // connection in question. |
| 576 SortConnections(); | 570 SortConnections(); |
| 577 } | 571 } |
| 578 | 572 |
| 579 void P2PTransportChannel::OnRoleConflict(PortInterface* port) { | 573 void P2PTransportChannel::OnRoleConflict(PortInterface* port) { |
| 580 SignalRoleConflict(this); // STUN ping will be sent when SetRole is called | 574 SignalRoleConflict(this); // STUN ping will be sent when SetRole is called |
| 581 // from Transport. | 575 // from Transport. |
| 582 } | 576 } |
| 583 | 577 |
| 578 // When the signalling channel is ready, we can really kick off the allocator |
| 579 void P2PTransportChannel::OnSignalingReady() { |
| 580 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 581 if (waiting_for_signaling_) { |
| 582 waiting_for_signaling_ = false; |
| 583 AddAllocatorSession(allocator_->CreateSession( |
| 584 SessionId(), content_name(), component(), ice_ufrag_, ice_pwd_)); |
| 585 } |
| 586 } |
| 587 |
| 584 void P2PTransportChannel::OnNominated(Connection* conn) { | 588 void P2PTransportChannel::OnNominated(Connection* conn) { |
| 585 ASSERT(worker_thread_ == rtc::Thread::Current()); | 589 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 586 ASSERT(ice_role_ == ICEROLE_CONTROLLED); | 590 ASSERT(ice_role_ == ICEROLE_CONTROLLED); |
| 587 | 591 |
| 588 if (conn->write_state() == Connection::STATE_WRITABLE) { | 592 if (conn->write_state() == Connection::STATE_WRITABLE) { |
| 589 if (best_connection_ != conn) { | 593 if (best_connection_ != conn) { |
| 590 pending_best_connection_ = NULL; | 594 pending_best_connection_ = NULL; |
| 591 LOG(LS_INFO) << "Switching best connection on controlled side: " | 595 LOG(LS_INFO) << "Switching best connection on controlled side: " |
| 592 << conn->ToString(); | 596 << conn->ToString(); |
| 593 SwitchBestConnectionTo(conn); | 597 SwitchBestConnectionTo(conn); |
| 594 // Now we have selected the best connection, time to prune other existing | 598 // Now we have selected the best connection, time to prune other existing |
| 595 // connections and update the read/write state of the channel. | 599 // connections and update the read/write state of the channel. |
| 596 RequestSort(); | 600 RequestSort(); |
| 597 } | 601 } |
| 598 } else { | 602 } else { |
| 599 LOG(LS_INFO) << "Not switching the best connection on controlled side yet," | 603 LOG(LS_INFO) << "Not switching the best connection on controlled side yet," |
| 600 << " because it's not writable: " << conn->ToString(); | 604 << " because it's not writable: " << conn->ToString(); |
| 601 pending_best_connection_ = conn; | 605 pending_best_connection_ = conn; |
| 602 } | 606 } |
| 603 } | 607 } |
| 604 | 608 |
| 605 void P2PTransportChannel::AddRemoteCandidate(const Candidate& candidate) { | 609 void P2PTransportChannel::OnCandidate(const Candidate& candidate) { |
| 606 ASSERT(worker_thread_ == rtc::Thread::Current()); | 610 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 607 | 611 |
| 608 uint32 generation = candidate.generation(); | 612 uint32 generation = candidate.generation(); |
| 609 // Network may not guarantee the order of the candidate delivery. If a | 613 // Network may not guarantee the order of the candidate delivery. If a |
| 610 // remote candidate with an older generation arrives, drop it. | 614 // remote candidate with an older generation arrives, drop it. |
| 611 if (generation != 0 && generation < remote_candidate_generation_) { | 615 if (generation != 0 && generation < remote_candidate_generation_) { |
| 612 LOG(LS_WARNING) << "Dropping a remote candidate because its generation " | 616 LOG(LS_WARNING) << "Dropping a remote candidate because its generation " |
| 613 << generation | 617 << generation |
| 614 << " is lower than the current remote generation " | 618 << " is lower than the current remote generation " |
| 615 << remote_candidate_generation_; | 619 << remote_candidate_generation_; |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 836 return sent; | 840 return sent; |
| 837 } | 841 } |
| 838 | 842 |
| 839 bool P2PTransportChannel::GetStats(ConnectionInfos *infos) { | 843 bool P2PTransportChannel::GetStats(ConnectionInfos *infos) { |
| 840 ASSERT(worker_thread_ == rtc::Thread::Current()); | 844 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 841 // Gather connection infos. | 845 // Gather connection infos. |
| 842 infos->clear(); | 846 infos->clear(); |
| 843 | 847 |
| 844 std::vector<Connection *>::const_iterator it; | 848 std::vector<Connection *>::const_iterator it; |
| 845 for (it = connections_.begin(); it != connections_.end(); ++it) { | 849 for (it = connections_.begin(); it != connections_.end(); ++it) { |
| 846 Connection* connection = *it; | 850 Connection *connection = *it; |
| 847 ConnectionInfo info; | 851 ConnectionInfo info; |
| 848 info.best_connection = (best_connection_ == connection); | 852 info.best_connection = (best_connection_ == connection); |
| 849 info.receiving = connection->receiving(); | 853 info.receiving = connection->receiving(); |
| 850 info.writable = | 854 info.writable = |
| 851 (connection->write_state() == Connection::STATE_WRITABLE); | 855 (connection->write_state() == Connection::STATE_WRITABLE); |
| 852 info.timeout = | 856 info.timeout = |
| 853 (connection->write_state() == Connection::STATE_WRITE_TIMEOUT); | 857 (connection->write_state() == Connection::STATE_WRITE_TIMEOUT); |
| 854 info.new_connection = !connection->reported(); | 858 info.new_connection = !connection->reported(); |
| 855 connection->set_reported(true); | 859 connection->set_reported(true); |
| 856 info.rtt = connection->rtt(); | 860 info.rtt = connection->rtt(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 870 } | 874 } |
| 871 | 875 |
| 872 rtc::DiffServCodePoint P2PTransportChannel::DefaultDscpValue() const { | 876 rtc::DiffServCodePoint P2PTransportChannel::DefaultDscpValue() const { |
| 873 OptionMap::const_iterator it = options_.find(rtc::Socket::OPT_DSCP); | 877 OptionMap::const_iterator it = options_.find(rtc::Socket::OPT_DSCP); |
| 874 if (it == options_.end()) { | 878 if (it == options_.end()) { |
| 875 return rtc::DSCP_NO_CHANGE; | 879 return rtc::DSCP_NO_CHANGE; |
| 876 } | 880 } |
| 877 return static_cast<rtc::DiffServCodePoint> (it->second); | 881 return static_cast<rtc::DiffServCodePoint> (it->second); |
| 878 } | 882 } |
| 879 | 883 |
| 884 // Begin allocate (or immediately re-allocate, if MSG_ALLOCATE pending) |
| 885 void P2PTransportChannel::Allocate() { |
| 886 // Time for a new allocator, lets make sure we have a signalling channel |
| 887 // to communicate candidates through first. |
| 888 waiting_for_signaling_ = true; |
| 889 SignalRequestSignaling(this); |
| 890 } |
| 891 |
| 880 // Monitor connection states. | 892 // Monitor connection states. |
| 881 void P2PTransportChannel::UpdateConnectionStates() { | 893 void P2PTransportChannel::UpdateConnectionStates() { |
| 882 uint32 now = rtc::Time(); | 894 uint32 now = rtc::Time(); |
| 883 | 895 |
| 884 // We need to copy the list of connections since some may delete themselves | 896 // We need to copy the list of connections since some may delete themselves |
| 885 // when we call UpdateState. | 897 // when we call UpdateState. |
| 886 for (uint32 i = 0; i < connections_.size(); ++i) | 898 for (uint32 i = 0; i < connections_.size(); ++i) |
| 887 connections_[i]->UpdateState(now); | 899 connections_[i]->UpdateState(now); |
| 888 } | 900 } |
| 889 | 901 |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1209 // a.1) |conn| is the best connection OR | 1221 // a.1) |conn| is the best connection OR |
| 1210 // a.2) there is no best connection OR | 1222 // a.2) there is no best connection OR |
| 1211 // a.3) the best connection is unwritable OR | 1223 // a.3) the best connection is unwritable OR |
| 1212 // a.4) |conn| has higher priority than best_connection. | 1224 // a.4) |conn| has higher priority than best_connection. |
| 1213 // b) we're doing LITE ICE AND | 1225 // b) we're doing LITE ICE AND |
| 1214 // b.1) |conn| is the best_connection AND | 1226 // b.1) |conn| is the best_connection AND |
| 1215 // b.2) |conn| is writable. | 1227 // b.2) |conn| is writable. |
| 1216 void P2PTransportChannel::PingConnection(Connection* conn) { | 1228 void P2PTransportChannel::PingConnection(Connection* conn) { |
| 1217 bool use_candidate = false; | 1229 bool use_candidate = false; |
| 1218 if (remote_ice_mode_ == ICEMODE_FULL && ice_role_ == ICEROLE_CONTROLLING) { | 1230 if (remote_ice_mode_ == ICEMODE_FULL && ice_role_ == ICEROLE_CONTROLLING) { |
| 1219 use_candidate = (conn == best_connection_) || (best_connection_ == NULL) || | 1231 use_candidate = (conn == best_connection_) || |
| 1232 (best_connection_ == NULL) || |
| 1220 (!best_connection_->writable()) || | 1233 (!best_connection_->writable()) || |
| 1221 (conn->priority() > best_connection_->priority()); | 1234 (conn->priority() > best_connection_->priority()); |
| 1222 } else if (remote_ice_mode_ == ICEMODE_LITE && conn == best_connection_) { | 1235 } else if (remote_ice_mode_ == ICEMODE_LITE && conn == best_connection_) { |
| 1223 use_candidate = best_connection_->writable(); | 1236 use_candidate = best_connection_->writable(); |
| 1224 } | 1237 } |
| 1225 conn->set_use_candidate_attr(use_candidate); | 1238 conn->set_use_candidate_attr(use_candidate); |
| 1226 conn->Ping(rtc::Time()); | 1239 conn->Ping(rtc::Time()); |
| 1227 } | 1240 } |
| 1228 | 1241 |
| 1229 // When a connection's state changes, we need to figure out who to use as | 1242 // When a connection's state changes, we need to figure out who to use as |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1291 std::vector<PortInterface*>::iterator iter = | 1304 std::vector<PortInterface*>::iterator iter = |
| 1292 std::find(ports_.begin(), ports_.end(), port); | 1305 std::find(ports_.begin(), ports_.end(), port); |
| 1293 if (iter != ports_.end()) | 1306 if (iter != ports_.end()) |
| 1294 ports_.erase(iter); | 1307 ports_.erase(iter); |
| 1295 | 1308 |
| 1296 LOG(INFO) << "Removed port from p2p socket: " | 1309 LOG(INFO) << "Removed port from p2p socket: " |
| 1297 << static_cast<int>(ports_.size()) << " remaining"; | 1310 << static_cast<int>(ports_.size()) << " remaining"; |
| 1298 } | 1311 } |
| 1299 | 1312 |
| 1300 // We data is available, let listeners know | 1313 // We data is available, let listeners know |
| 1301 void P2PTransportChannel::OnReadPacket(Connection* connection, | 1314 void P2PTransportChannel::OnReadPacket( |
| 1302 const char* data, | 1315 Connection *connection, const char *data, size_t len, |
| 1303 size_t len, | 1316 const rtc::PacketTime& packet_time) { |
| 1304 const rtc::PacketTime& packet_time) { | |
| 1305 ASSERT(worker_thread_ == rtc::Thread::Current()); | 1317 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 1306 | 1318 |
| 1307 // Do not deliver, if packet doesn't belong to the correct transport channel. | 1319 // Do not deliver, if packet doesn't belong to the correct transport channel. |
| 1308 if (!FindConnection(connection)) | 1320 if (!FindConnection(connection)) |
| 1309 return; | 1321 return; |
| 1310 | 1322 |
| 1311 // Let the client know of an incoming packet | 1323 // Let the client know of an incoming packet |
| 1312 SignalReadPacket(this, data, len, packet_time, 0); | 1324 SignalReadPacket(this, data, len, packet_time, 0); |
| 1313 | 1325 |
| 1314 // May need to switch the sending connection based on the receiving media path | 1326 // May need to switch the sending connection based on the receiving media path |
| 1315 // if this is the controlled side. | 1327 // if this is the controlled side. |
| 1316 if (ice_role_ == ICEROLE_CONTROLLED && !best_nominated_connection() && | 1328 if (ice_role_ == ICEROLE_CONTROLLED && !best_nominated_connection() && |
| 1317 connection->writable() && best_connection_ != connection) { | 1329 connection->writable() && best_connection_ != connection) { |
| 1318 SwitchBestConnectionTo(connection); | 1330 SwitchBestConnectionTo(connection); |
| 1319 } | 1331 } |
| 1320 } | 1332 } |
| 1321 | 1333 |
| 1322 void P2PTransportChannel::OnReadyToSend(Connection* connection) { | 1334 void P2PTransportChannel::OnReadyToSend(Connection* connection) { |
| 1323 if (connection == best_connection_ && writable()) { | 1335 if (connection == best_connection_ && writable()) { |
| 1324 SignalReadyToSend(this); | 1336 SignalReadyToSend(this); |
| 1325 } | 1337 } |
| 1326 } | 1338 } |
| 1327 | 1339 |
| 1328 } // namespace cricket | 1340 } // namespace cricket |
| OLD | NEW |