Chromium Code Reviews| 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 <algorithm> | 13 #include <algorithm> |
| 14 #include <set> | 14 #include <set> |
| 15 | 15 |
| 16 #include "webrtc/base/common.h" | 16 #include "webrtc/base/common.h" |
| 17 #include "webrtc/base/crc32.h" | 17 #include "webrtc/base/crc32.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 #include "webrtc/p2p/base/candidate.h" | 20 #include "webrtc/p2p/base/candidate.h" |
| 21 #include "webrtc/p2p/base/candidatepairinterface.h" | 21 #include "webrtc/p2p/base/candidatepairinterface.h" |
| 22 #include "webrtc/p2p/base/common.h" | 22 #include "webrtc/p2p/base/common.h" |
| 23 #include "webrtc/p2p/base/relayport.h" // For RELAY_PORT_TYPE. | 23 #include "webrtc/p2p/base/relayport.h" // For RELAY_PORT_TYPE. |
| 24 #include "webrtc/p2p/base/stunport.h" // For STUN_PORT_TYPE. | 24 #include "webrtc/p2p/base/stunport.h" // For STUN_PORT_TYPE. |
| 25 #include "webrtc/system_wrappers/include/field_trial.h" | 25 #include "webrtc/system_wrappers/include/field_trial.h" |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 // messages for queuing up work for ourselves | 29 // messages for queuing up work for ourselves |
| 30 enum { MSG_SORT = 1, MSG_CHECK_AND_PING }; | 30 enum { |
| 31 MSG_SORT = 1, | |
| 32 MSG_CHECK_AND_PING, | |
| 33 MSG_CHECK_RESTORE_BACKUP_CONNECTION, | |
|
pthatcher1
2016/06/07 18:54:33
It seems like a better name would be MSG_REGATHER_
honghaiz3
2016/06/29 02:48:22
MSG_REGATHER_ON_FAILED_NETWORKS better?
| |
| 34 MSG_SIGNAL_CANDIDATES_REMOVED | |
|
pthatcher1
2016/06/07 18:54:33
Why does this require a thread post? To coalesce
honghaiz3
2016/06/29 02:48:22
Yes. We can merge multiple candidate removals into
| |
| 35 }; | |
| 31 | 36 |
| 32 // The minimum improvement in RTT that justifies a switch. | 37 // The minimum improvement in RTT that justifies a switch. |
| 33 static const double kMinImprovement = 10; | 38 const double kMinImprovement = 10; |
| 34 | 39 |
| 35 bool IsRelayRelay(cricket::Connection* conn) { | 40 bool IsRelayRelay(cricket::Connection* conn) { |
| 36 return conn->local_candidate().type() == cricket::RELAY_PORT_TYPE && | 41 return conn->local_candidate().type() == cricket::RELAY_PORT_TYPE && |
| 37 conn->remote_candidate().type() == cricket::RELAY_PORT_TYPE; | 42 conn->remote_candidate().type() == cricket::RELAY_PORT_TYPE; |
| 38 } | 43 } |
| 39 | 44 |
| 40 bool IsUdp(cricket::Connection* conn) { | 45 bool IsUdp(cricket::Connection* conn) { |
| 41 return conn->local_candidate().relay_protocol() == cricket::UDP_PROTOCOL_NAME; | 46 return conn->local_candidate().relay_protocol() == cricket::UDP_PROTOCOL_NAME; |
| 42 } | 47 } |
| 43 | 48 |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 // writable or not receiving. | 222 // writable or not receiving. |
| 218 const int WEAK_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 10000; | 223 const int WEAK_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 10000; |
| 219 | 224 |
| 220 // If the current best connection is both writable and receiving, then we will | 225 // If the current best connection is both writable and receiving, then we will |
| 221 // also try hard to make sure it is pinged at this rate (a little less than | 226 // also try hard to make sure it is pinged at this rate (a little less than |
| 222 // 2 * STRONG_PING_INTERVAL). | 227 // 2 * STRONG_PING_INTERVAL). |
| 223 static const int MAX_CURRENT_STRONG_INTERVAL = 900; // ms | 228 static const int MAX_CURRENT_STRONG_INTERVAL = 900; // ms |
| 224 | 229 |
| 225 static const int MIN_CHECK_RECEIVING_INTERVAL = 50; // ms | 230 static const int MIN_CHECK_RECEIVING_INTERVAL = 50; // ms |
| 226 | 231 |
| 232 // We periodically check and restore backup connections on existing networks | |
| 233 // that do not have any connection at this interval. | |
| 234 static const int CHECK_RESTORE_BACKUP_CONNECTION_INTERVAL = 5 * 60 * 1000; | |
|
pthatcher1
2016/06/07 18:54:33
I think a better name would be DEFAULT_REGATHER_FA
| |
| 235 | |
| 227 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, | 236 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, |
| 228 int component, | 237 int component, |
| 229 P2PTransport* transport, | 238 P2PTransport* transport, |
| 230 PortAllocator* allocator) | 239 PortAllocator* allocator) |
| 231 : P2PTransportChannel(transport_name, component, allocator) {} | 240 : P2PTransportChannel(transport_name, component, allocator) {} |
| 232 | 241 |
| 233 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, | 242 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, |
| 234 int component, | 243 int component, |
| 235 PortAllocator* allocator) | 244 PortAllocator* allocator) |
| 236 : TransportChannelImpl(transport_name, component), | 245 : TransportChannelImpl(transport_name, component), |
| 237 allocator_(allocator), | 246 allocator_(allocator), |
| 238 worker_thread_(rtc::Thread::Current()), | 247 worker_thread_(rtc::Thread::Current()), |
| 239 incoming_only_(false), | 248 incoming_only_(false), |
| 240 error_(0), | 249 error_(0), |
| 241 best_connection_(NULL), | 250 best_connection_(NULL), |
| 242 pending_best_connection_(NULL), | 251 pending_best_connection_(NULL), |
| 243 sort_dirty_(false), | 252 sort_dirty_(false), |
| 244 remote_ice_mode_(ICEMODE_FULL), | 253 remote_ice_mode_(ICEMODE_FULL), |
| 245 ice_role_(ICEROLE_UNKNOWN), | 254 ice_role_(ICEROLE_UNKNOWN), |
| 246 tiebreaker_(0), | 255 tiebreaker_(0), |
| 247 gathering_state_(kIceGatheringNew), | 256 gathering_state_(kIceGatheringNew), |
| 248 check_receiving_interval_(MIN_CHECK_RECEIVING_INTERVAL * 5), | 257 check_receiving_interval_(MIN_CHECK_RECEIVING_INTERVAL * 5), |
| 258 check_restore_backup_connection_interval_( | |
| 259 CHECK_RESTORE_BACKUP_CONNECTION_INTERVAL), | |
|
pthatcher1
2016/06/07 18:54:33
Can you put this in config_ instead of as its own
| |
| 249 config_(MIN_CHECK_RECEIVING_INTERVAL * 50 /* receiving_timeout */, | 260 config_(MIN_CHECK_RECEIVING_INTERVAL * 50 /* receiving_timeout */, |
| 250 0 /* backup_connection_ping_interval */, | 261 0 /* backup_connection_ping_interval */, |
| 251 false /* gather_continually */, | 262 false /* gather_continually */, |
| 252 false /* prioritize_most_likely_candidate_pairs */, | 263 false /* prioritize_most_likely_candidate_pairs */, |
| 253 MAX_CURRENT_STRONG_INTERVAL /* max_strong_interval */) { | 264 MAX_CURRENT_STRONG_INTERVAL /* max_strong_interval */) { |
| 254 uint32_t weak_ping_interval = ::strtoul( | 265 uint32_t weak_ping_interval = ::strtoul( |
| 255 webrtc::field_trial::FindFullName("WebRTC-StunInterPacketDelay").c_str(), | 266 webrtc::field_trial::FindFullName("WebRTC-StunInterPacketDelay").c_str(), |
| 256 nullptr, 10); | 267 nullptr, 10); |
| 257 if (weak_ping_interval) { | 268 if (weak_ping_interval) { |
| 258 weak_ping_interval_ = static_cast<int>(weak_ping_interval); | 269 weak_ping_interval_ = static_cast<int>(weak_ping_interval); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 273 session->SignalPortReady.connect(this, &P2PTransportChannel::OnPortReady); | 284 session->SignalPortReady.connect(this, &P2PTransportChannel::OnPortReady); |
| 274 session->SignalCandidatesReady.connect( | 285 session->SignalCandidatesReady.connect( |
| 275 this, &P2PTransportChannel::OnCandidatesReady); | 286 this, &P2PTransportChannel::OnCandidatesReady); |
| 276 session->SignalCandidatesAllocationDone.connect( | 287 session->SignalCandidatesAllocationDone.connect( |
| 277 this, &P2PTransportChannel::OnCandidatesAllocationDone); | 288 this, &P2PTransportChannel::OnCandidatesAllocationDone); |
| 278 | 289 |
| 279 // We now only want to apply new candidates that we receive to the ports | 290 // We now only want to apply new candidates that we receive to the ports |
| 280 // created by this new session because these are replacing those of the | 291 // created by this new session because these are replacing those of the |
| 281 // previous sessions. | 292 // previous sessions. |
| 282 ports_.clear(); | 293 ports_.clear(); |
| 294 // We do not need to signal the candidate removals because a new allocator | |
| 295 // session indicates an ICE restart, which will remove all old remote | |
| 296 // candidates on the other side. | |
| 297 candidates_removed_.clear(); | |
|
pthatcher1
2016/06/07 18:54:33
This feels like implicit magic to me. Could we in
| |
| 283 | 298 |
| 284 allocator_sessions_.push_back(std::move(session)); | 299 allocator_sessions_.push_back(std::move(session)); |
| 285 } | 300 } |
| 286 | 301 |
| 287 void P2PTransportChannel::AddConnection(Connection* connection) { | 302 void P2PTransportChannel::AddConnection(Connection* connection) { |
| 288 connections_.push_back(connection); | 303 connections_.push_back(connection); |
| 289 unpinged_connections_.insert(connection); | 304 unpinged_connections_.insert(connection); |
| 290 connection->set_remote_ice_mode(remote_ice_mode_); | 305 connection->set_remote_ice_mode(remote_ice_mode_); |
| 291 connection->set_receiving_timeout(config_.receiving_timeout); | 306 connection->set_receiving_timeout(config_.receiving_timeout); |
| 292 connection->SignalReadPacket.connect( | 307 connection->SignalReadPacket.connect( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 if (!had_connection_) { | 348 if (!had_connection_) { |
| 334 return TransportChannelState::STATE_INIT; | 349 return TransportChannelState::STATE_INIT; |
| 335 } | 350 } |
| 336 | 351 |
| 337 std::vector<Connection*> active_connections; | 352 std::vector<Connection*> active_connections; |
| 338 for (Connection* connection : connections_) { | 353 for (Connection* connection : connections_) { |
| 339 if (connection->active()) { | 354 if (connection->active()) { |
| 340 active_connections.push_back(connection); | 355 active_connections.push_back(connection); |
| 341 } | 356 } |
| 342 } | 357 } |
| 343 if (active_connections.empty()) { | 358 // TODO(honghaiz): We should expose one transport channel state to the |
| 359 // the external world (e.g. TransportController), so that we won't need to | |
| 360 // make both writable and channel state depend on IsRecoveringConnectivity. | |
| 361 if (active_connections.empty() && !IsRecoveringConnectivity()) { | |
|
pthatcher1
2016/06/07 18:54:33
I don't like the look of the complexity added to h
| |
| 344 return TransportChannelState::STATE_FAILED; | 362 return TransportChannelState::STATE_FAILED; |
| 345 } | 363 } |
| 346 | 364 |
| 347 std::set<rtc::Network*> networks; | 365 std::set<rtc::Network*> networks; |
| 348 for (Connection* connection : active_connections) { | 366 for (Connection* connection : active_connections) { |
| 349 rtc::Network* network = connection->port()->Network(); | 367 rtc::Network* network = connection->port()->Network(); |
| 350 if (networks.find(network) == networks.end()) { | 368 if (networks.find(network) == networks.end()) { |
| 351 networks.insert(network); | 369 networks.insert(network); |
| 352 } else { | 370 } else { |
| 353 LOG_J(LS_VERBOSE, this) << "Ice not completed yet for this channel as " | 371 LOG_J(LS_VERBOSE, this) << "Ice not completed yet for this channel as " |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 449 ASSERT(worker_thread_ == rtc::Thread::Current()); | 467 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 450 if (ice_ufrag_.empty() || ice_pwd_.empty()) { | 468 if (ice_ufrag_.empty() || ice_pwd_.empty()) { |
| 451 ASSERT(false); | 469 ASSERT(false); |
| 452 LOG(LS_ERROR) << "P2PTransportChannel::Connect: The ice_ufrag_ and the " | 470 LOG(LS_ERROR) << "P2PTransportChannel::Connect: The ice_ufrag_ and the " |
| 453 << "ice_pwd_ are not set."; | 471 << "ice_pwd_ are not set."; |
| 454 return; | 472 return; |
| 455 } | 473 } |
| 456 | 474 |
| 457 // Start checking and pinging as the ports come in. | 475 // Start checking and pinging as the ports come in. |
| 458 thread()->Post(this, MSG_CHECK_AND_PING); | 476 thread()->Post(this, MSG_CHECK_AND_PING); |
| 477 thread()->PostDelayed(check_restore_backup_connection_interval_, this, | |
| 478 MSG_CHECK_RESTORE_BACKUP_CONNECTION); | |
| 459 } | 479 } |
| 460 | 480 |
| 461 void P2PTransportChannel::MaybeStartGathering() { | 481 void P2PTransportChannel::MaybeStartGathering() { |
| 462 // Start gathering if we never started before, or if an ICE restart occurred. | 482 // Start gathering if we never started before, or if an ICE restart occurred. |
| 463 if (allocator_sessions_.empty() || | 483 if (allocator_sessions_.empty() || |
| 464 IceCredentialsChanged(allocator_sessions_.back()->ice_ufrag(), | 484 IceCredentialsChanged(allocator_sessions_.back()->ice_ufrag(), |
| 465 allocator_sessions_.back()->ice_pwd(), ice_ufrag_, | 485 allocator_sessions_.back()->ice_pwd(), ice_ufrag_, |
| 466 ice_pwd_)) { | 486 ice_pwd_)) { |
| 467 if (gathering_state_ != kIceGatheringGathering) { | 487 if (gathering_state_ != kIceGatheringGathering) { |
| 468 gathering_state_ = kIceGatheringGathering; | 488 gathering_state_ = kIceGatheringGathering; |
| (...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1230 RTC_DCHECK(state == STATE_CONNECTING || state == STATE_COMPLETED); | 1250 RTC_DCHECK(state == STATE_CONNECTING || state == STATE_COMPLETED); |
| 1231 break; | 1251 break; |
| 1232 default: | 1252 default: |
| 1233 RTC_DCHECK(false); | 1253 RTC_DCHECK(false); |
| 1234 break; | 1254 break; |
| 1235 } | 1255 } |
| 1236 state_ = state; | 1256 state_ = state; |
| 1237 SignalStateChanged(this); | 1257 SignalStateChanged(this); |
| 1238 } | 1258 } |
| 1239 | 1259 |
| 1240 bool writable = best_connection_ && best_connection_->writable(); | 1260 // If it is recovering connectivity, it is considered writable to the |
| 1261 // external world. | |
| 1262 // TODO(honghaiz): Expose the recovering state to the application. | |
| 1263 bool writable = (best_connection_ && best_connection_->writable()) || | |
| 1264 IsRecoveringConnectivity(); | |
| 1241 set_writable(writable); | 1265 set_writable(writable); |
| 1242 | 1266 |
| 1243 bool receiving = false; | 1267 bool receiving = false; |
| 1244 for (const Connection* connection : connections_) { | 1268 for (const Connection* connection : connections_) { |
| 1245 if (connection->receiving()) { | 1269 if (connection->receiving()) { |
| 1246 receiving = true; | 1270 receiving = true; |
| 1247 break; | 1271 break; |
| 1248 } | 1272 } |
| 1249 } | 1273 } |
| 1250 set_receiving(receiving); | 1274 set_receiving(receiving); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1299 | 1323 |
| 1300 // Handle any queued up requests | 1324 // Handle any queued up requests |
| 1301 void P2PTransportChannel::OnMessage(rtc::Message *pmsg) { | 1325 void P2PTransportChannel::OnMessage(rtc::Message *pmsg) { |
| 1302 switch (pmsg->message_id) { | 1326 switch (pmsg->message_id) { |
| 1303 case MSG_SORT: | 1327 case MSG_SORT: |
| 1304 OnSort(); | 1328 OnSort(); |
| 1305 break; | 1329 break; |
| 1306 case MSG_CHECK_AND_PING: | 1330 case MSG_CHECK_AND_PING: |
| 1307 OnCheckAndPing(); | 1331 OnCheckAndPing(); |
| 1308 break; | 1332 break; |
| 1333 case MSG_CHECK_RESTORE_BACKUP_CONNECTION: | |
| 1334 OnCheckAndRestoreBackupConnection(); | |
| 1335 break; | |
| 1336 case MSG_SIGNAL_CANDIDATES_REMOVED: | |
| 1337 if (!candidates_removed_.empty()) { | |
| 1338 LOG(LS_INFO) << "Signal removing " << candidates_removed_.size() | |
| 1339 << " candidates"; | |
| 1340 SignalCandidatesRemoved(this, candidates_removed_); | |
| 1341 candidates_removed_.clear(); | |
| 1342 } | |
| 1343 break; | |
| 1309 default: | 1344 default: |
| 1310 ASSERT(false); | 1345 ASSERT(false); |
| 1311 break; | 1346 break; |
| 1312 } | 1347 } |
| 1313 } | 1348 } |
| 1314 | 1349 |
| 1315 // Handle queued up sort request | 1350 // Handle queued up sort request |
| 1316 void P2PTransportChannel::OnSort() { | 1351 void P2PTransportChannel::OnSort() { |
| 1317 // Resort the connections based on the new statistics. | 1352 // Resort the connections based on the new statistics. |
| 1318 SortConnections(); | 1353 SortConnections(); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 1337 Connection* conn = FindNextPingableConnection(); | 1372 Connection* conn = FindNextPingableConnection(); |
| 1338 if (conn) { | 1373 if (conn) { |
| 1339 PingConnection(conn); | 1374 PingConnection(conn); |
| 1340 MarkConnectionPinged(conn); | 1375 MarkConnectionPinged(conn); |
| 1341 } | 1376 } |
| 1342 } | 1377 } |
| 1343 int delay = std::min(ping_interval, check_receiving_interval_); | 1378 int delay = std::min(ping_interval, check_receiving_interval_); |
| 1344 thread()->PostDelayed(delay, this, MSG_CHECK_AND_PING); | 1379 thread()->PostDelayed(delay, this, MSG_CHECK_AND_PING); |
| 1345 } | 1380 } |
| 1346 | 1381 |
| 1382 void P2PTransportChannel::OnCheckAndRestoreBackupConnection() { | |
| 1383 if (!config_.gather_continually || !allocator_->network_manager()) { | |
| 1384 return; | |
| 1385 } | |
| 1386 rtc::NetworkManager::NetworkList networks; | |
| 1387 allocator_->network_manager()->GetNetworks(&networks); | |
|
pthatcher1
2016/06/07 18:54:33
I'm not a fan of reaching into the allocator like
| |
| 1388 // Filter out loopback networks. | |
| 1389 auto network_end = std::remove_if( | |
| 1390 networks.begin(), networks.end(), [](rtc::Network* network) { | |
| 1391 return network->type() == rtc::ADAPTER_TYPE_LOOPBACK; | |
| 1392 }); | |
| 1393 | |
| 1394 // Filter out networks having connections in non-failed state. | |
| 1395 for (Connection* conn : connections_) { | |
| 1396 if (conn->state() == Connection::STATE_FAILED) { | |
| 1397 continue; | |
| 1398 } | |
| 1399 const std::string& interface_name = conn->port()->Network()->name(); | |
| 1400 network_end = std::remove_if(networks.begin(), network_end, | |
| 1401 [interface_name](rtc::Network* net) { | |
| 1402 return net->name() == interface_name; | |
| 1403 }); | |
| 1404 if (networks.begin() == network_end) { | |
| 1405 break; | |
| 1406 } | |
| 1407 } | |
| 1408 networks.erase(network_end, networks.end()); | |
|
pthatcher1
2016/06/07 18:54:33
This logic seems a bit complicated. Would it mak
honghaiz3
2016/06/29 02:48:23
Done.
| |
| 1409 if (!networks.empty()) { | |
| 1410 StartRegathering(&networks); | |
| 1411 } | |
|
pthatcher1
2016/06/07 18:54:33
It seems like a lot of this logic should be in a s
honghaiz3
2016/06/29 02:48:23
Done.
| |
| 1412 | |
| 1413 thread()->PostDelayed(check_restore_backup_connection_interval_, this, | |
| 1414 MSG_CHECK_RESTORE_BACKUP_CONNECTION); | |
| 1415 } | |
| 1416 | |
| 1347 // A connection is considered a backup connection if the channel state | 1417 // A connection is considered a backup connection if the channel state |
| 1348 // is completed, the connection is not the best connection and it is active. | 1418 // is completed, the connection is not the best connection and it is active. |
| 1349 bool P2PTransportChannel::IsBackupConnection(Connection* conn) const { | 1419 bool P2PTransportChannel::IsBackupConnection(Connection* conn) const { |
| 1350 return state_ == STATE_COMPLETED && conn != best_connection_ && | 1420 return state_ == STATE_COMPLETED && conn != best_connection_ && |
| 1351 conn->active(); | 1421 conn->active(); |
| 1352 } | 1422 } |
| 1353 | 1423 |
| 1354 // Is the connection in a state for us to even consider pinging the other side? | 1424 // Is the connection in a state for us to even consider pinging the other side? |
| 1355 // We consider a connection pingable even if it's not connected because that's | 1425 // We consider a connection pingable even if it's not connected because that's |
| 1356 // how a TCP connection is kicked into reconnecting on the active side. | 1426 // how a TCP connection is kicked into reconnecting on the active side. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1404 } | 1474 } |
| 1405 return conn_to_ping; | 1475 return conn_to_ping; |
| 1406 } | 1476 } |
| 1407 | 1477 |
| 1408 void P2PTransportChannel::MarkConnectionPinged(Connection* conn) { | 1478 void P2PTransportChannel::MarkConnectionPinged(Connection* conn) { |
| 1409 if (conn && pinged_connections_.insert(conn).second) { | 1479 if (conn && pinged_connections_.insert(conn).second) { |
| 1410 unpinged_connections_.erase(conn); | 1480 unpinged_connections_.erase(conn); |
| 1411 } | 1481 } |
| 1412 } | 1482 } |
| 1413 | 1483 |
| 1484 void P2PTransportChannel::set_check_restore_backup_connection_interval( | |
|
pthatcher1
2016/06/07 18:54:33
It sounds like this method should be called Regath
| |
| 1485 int interval) { | |
| 1486 check_restore_backup_connection_interval_ = interval; | |
| 1487 thread()->Clear(this, MSG_CHECK_RESTORE_BACKUP_CONNECTION, nullptr); | |
| 1488 thread()->PostDelayed(check_restore_backup_connection_interval_, this, | |
| 1489 MSG_CHECK_RESTORE_BACKUP_CONNECTION); | |
| 1490 } | |
| 1491 | |
| 1414 // Apart from sending ping from |conn| this method also updates | 1492 // Apart from sending ping from |conn| this method also updates |
| 1415 // |use_candidate_attr| flag. The criteria to update this flag is | 1493 // |use_candidate_attr| flag. The criteria to update this flag is |
| 1416 // explained below. | 1494 // explained below. |
| 1417 // Set USE-CANDIDATE if doing ICE AND this channel is in CONTROLLING AND | 1495 // Set USE-CANDIDATE if doing ICE AND this channel is in CONTROLLING AND |
| 1418 // a) Channel is in FULL ICE AND | 1496 // a) Channel is in FULL ICE AND |
| 1419 // a.1) |conn| is the best connection OR | 1497 // a.1) |conn| is the best connection OR |
| 1420 // a.2) there is no best connection OR | 1498 // a.2) there is no best connection OR |
| 1421 // a.3) the best connection is unwritable OR | 1499 // a.3) the best connection is unwritable OR |
| 1422 // a.4) |conn| has higher priority than best_connection. | 1500 // a.4) |conn| has higher priority than best_connection. |
| 1423 // b) we're doing LITE ICE AND | 1501 // b) we're doing LITE ICE AND |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1455 | 1533 |
| 1456 // May stop the allocator session when at least one connection becomes | 1534 // May stop the allocator session when at least one connection becomes |
| 1457 // strongly connected after starting to get ports and the local candidate of | 1535 // strongly connected after starting to get ports and the local candidate of |
| 1458 // the connection is at the latest generation. It is not enough to check | 1536 // the connection is at the latest generation. It is not enough to check |
| 1459 // that the connection becomes weakly connected because the connection may be | 1537 // that the connection becomes weakly connected because the connection may be |
| 1460 // changing from (writable, receiving) to (writable, not receiving). | 1538 // changing from (writable, receiving) to (writable, not receiving). |
| 1461 bool strongly_connected = !connection->weak(); | 1539 bool strongly_connected = !connection->weak(); |
| 1462 bool latest_generation = connection->local_candidate().generation() >= | 1540 bool latest_generation = connection->local_candidate().generation() >= |
| 1463 allocator_session()->generation(); | 1541 allocator_session()->generation(); |
| 1464 if (strongly_connected && latest_generation) { | 1542 if (strongly_connected && latest_generation) { |
| 1543 if (recovering_start_time_) { | |
| 1544 recovering_start_time_ = 0; | |
| 1545 } | |
|
pthatcher1
2016/06/07 18:54:33
***
| |
| 1465 MaybeStopPortAllocatorSessions(); | 1546 MaybeStopPortAllocatorSessions(); |
| 1466 } | 1547 } |
| 1467 | 1548 |
| 1468 // We have to unroll the stack before doing this because we may be changing | 1549 // We have to unroll the stack before doing this because we may be changing |
| 1469 // the state of connections while sorting. | 1550 // the state of connections while sorting. |
| 1470 RequestSort(); | 1551 RequestSort(); |
| 1471 } | 1552 } |
| 1472 | 1553 |
| 1473 // When a connection is removed, edit it out, and then update our best | 1554 // When a connection is removed, edit it out, and then update our best |
| 1474 // connection. | 1555 // connection. |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 1493 pending_best_connection_ = NULL; | 1574 pending_best_connection_ = NULL; |
| 1494 } | 1575 } |
| 1495 | 1576 |
| 1496 // If this is currently the best connection, then we need to pick a new one. | 1577 // If this is currently the best connection, then we need to pick a new one. |
| 1497 // The call to SortConnections will pick a new one. It looks at the current | 1578 // The call to SortConnections will pick a new one. It looks at the current |
| 1498 // best connection in order to avoid switching between fairly similar ones. | 1579 // best connection in order to avoid switching between fairly similar ones. |
| 1499 // Since this connection is no longer an option, we can just set best to NULL | 1580 // Since this connection is no longer an option, we can just set best to NULL |
| 1500 // and re-choose a best assuming that there was no best connection. | 1581 // and re-choose a best assuming that there was no best connection. |
| 1501 if (best_connection_ == connection) { | 1582 if (best_connection_ == connection) { |
| 1502 LOG(LS_INFO) << "Best connection destroyed. Will choose a new one."; | 1583 LOG(LS_INFO) << "Best connection destroyed. Will choose a new one."; |
| 1584 // When continual gathering is enabled, we will attempt re-gathering only | |
| 1585 // if the best connection was removed and it was writable. If it was not | |
| 1586 // writable, most likely it is in the process of gathering on the current | |
| 1587 // session. | |
| 1588 if (config_.gather_continually && writable()) { | |
| 1589 bool all_connections_failed = std::all_of( | |
| 1590 connections_.begin(), connections_.end(), [](Connection* conn) { | |
| 1591 return conn->state() == Connection::STATE_FAILED; | |
| 1592 }); | |
| 1593 // Start regathering on all active networks if all connections failed. | |
| 1594 if (all_connections_failed && !IsRecoveringConnectivity()) { | |
| 1595 recovering_start_time_ = rtc::TimeMillis(); | |
|
pthatcher1
2016/06/07 18:54:33
Should this be last_regather_time_, and should it
honghaiz3
2016/06/29 02:48:23
Done.
| |
| 1596 StartRegathering(nullptr); | |
| 1597 } | |
|
pthatcher1
2016/06/07 18:54:33
Can we put this in a separate methods called Maybe
honghaiz3
2016/06/29 02:48:23
Removed.
| |
| 1598 } | |
| 1503 SwitchBestConnectionTo(NULL); | 1599 SwitchBestConnectionTo(NULL); |
| 1504 RequestSort(); | 1600 RequestSort(); |
| 1505 } | 1601 } |
| 1506 | 1602 |
| 1507 UpdateState(); | 1603 UpdateState(); |
| 1508 } | 1604 } |
| 1509 | 1605 |
| 1510 // When a port is destroyed remove it from our list of ports to use for | 1606 // When a port is destroyed remove it from our list of ports to use for |
| 1511 // connection attempts. | 1607 // connection attempts. |
| 1512 void P2PTransportChannel::OnPortDestroyed(PortInterface* port) { | 1608 void P2PTransportChannel::OnPortDestroyed(PortInterface* port) { |
| 1513 ASSERT(worker_thread_ == rtc::Thread::Current()); | 1609 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 1514 | 1610 |
| 1515 // Remove this port from the list (if we didn't drop it already). | 1611 // Remove this port from the list (if we didn't drop it already). |
| 1516 std::vector<PortInterface*>::iterator iter = | 1612 std::vector<PortInterface*>::iterator iter = |
| 1517 std::find(ports_.begin(), ports_.end(), port); | 1613 std::find(ports_.begin(), ports_.end(), port); |
| 1518 if (iter != ports_.end()) | 1614 if (iter != ports_.end()) |
| 1519 ports_.erase(iter); | 1615 ports_.erase(iter); |
| 1520 | 1616 |
| 1521 LOG(INFO) << "Removed port from p2p socket: " | 1617 LOG(INFO) << "Removed port from p2p socket: " |
| 1522 << static_cast<int>(ports_.size()) << " remaining"; | 1618 << static_cast<int>(ports_.size()) << " remaining"; |
| 1523 } | 1619 } |
| 1524 | 1620 |
| 1525 void P2PTransportChannel::OnPortNetworkInactive(PortInterface* port) { | 1621 void P2PTransportChannel::OnPortNetworkInactive(PortInterface* port) { |
| 1526 // If it does not gather continually, the port will be removed from the list | 1622 // If it does not gather continually, the port will be removed from the list |
| 1527 // when ICE restarts. | 1623 // when ICE restarts, so don't need to remove it here. |
| 1528 if (!config_.gather_continually) { | 1624 if (!config_.gather_continually) { |
| 1529 return; | 1625 return; |
| 1530 } | 1626 } |
| 1531 auto it = std::find(ports_.begin(), ports_.end(), port); | 1627 auto it = std::find(ports_.begin(), ports_.end(), port); |
| 1532 // Don't need to do anything if the port has been deleted from the port list. | 1628 // Don't need to do anything if the port has been deleted from the port list. |
| 1533 if (it == ports_.end()) { | 1629 if (it == ports_.end()) { |
| 1534 return; | 1630 return; |
| 1535 } | 1631 } |
| 1536 ports_.erase(it); | 1632 ports_.erase(it); |
| 1537 LOG(INFO) << "Removed port due to inactive networks: " << ports_.size() | 1633 LOG(INFO) << "Removed port due to inactive networks: " << ports_.size() |
| 1538 << " remaining"; | 1634 << " remaining"; |
| 1539 std::vector<Candidate> candidates = port->Candidates(); | 1635 port->FailAndDestroyConnections(); |
|
pthatcher1
2016/06/07 18:54:33
It feels like this should be Close(), which should
| |
| 1540 for (Candidate& candidate : candidates) { | 1636 |
| 1637 std::vector<Candidate> candidates = | |
| 1638 allocator_session()->ReadyCandidates(port); | |
| 1639 for (Candidate candidate : candidates) { | |
| 1541 candidate.set_transport_name(transport_name()); | 1640 candidate.set_transport_name(transport_name()); |
| 1641 candidates_removed_.push_back(candidate); | |
| 1542 } | 1642 } |
| 1543 SignalCandidatesRemoved(this, candidates); | 1643 thread()->Post(this, MSG_SIGNAL_CANDIDATES_REMOVED); |
|
pthatcher1
2016/06/07 18:54:33
Why does this need a thread hop?
honghaiz3
2016/06/29 02:48:23
We can combine multiple candidate removal signals
| |
| 1644 } | |
| 1645 | |
| 1646 void P2PTransportChannel::StartRegathering( | |
| 1647 std::vector<rtc::Network*>* networks) { | |
| 1648 std::string log_msg; | |
| 1649 std::vector<PortInterface*> ports_to_remove; | |
| 1650 if (networks == nullptr) { | |
| 1651 log_msg = "Regathering on all networks"; | |
| 1652 ports_to_remove = ports_; | |
| 1653 } else { | |
| 1654 log_msg = "Regathering on networks:"; | |
| 1655 for (rtc::Network* network : *networks) { | |
| 1656 log_msg += " " + network->ToString(); | |
| 1657 } | |
| 1658 // Remove all ports using |networks|. | |
| 1659 for (PortInterface* port : ports_) { | |
| 1660 if (std::find(networks->begin(), networks->end(), port->Network()) != | |
| 1661 networks->end()) { | |
| 1662 ports_to_remove.push_back(port); | |
| 1663 } | |
| 1664 } | |
| 1665 } | |
| 1666 LOG(LS_INFO) << log_msg; | |
| 1667 for (PortInterface* port : ports_to_remove) { | |
| 1668 // Remove the port and signal the other side to remove the respective remote | |
| 1669 // candidates. | |
| 1670 OnPortNetworkInactive(port); | |
|
pthatcher1
2016/06/07 18:54:33
I feel like we're confounding two things by trigge
| |
| 1671 } | |
| 1672 | |
| 1673 allocator_session()->GetPortsOnNetworks(networks); | |
| 1544 } | 1674 } |
| 1545 | 1675 |
| 1546 // We data is available, let listeners know | 1676 // We data is available, let listeners know |
| 1547 void P2PTransportChannel::OnReadPacket(Connection* connection, | 1677 void P2PTransportChannel::OnReadPacket(Connection* connection, |
| 1548 const char* data, | 1678 const char* data, |
| 1549 size_t len, | 1679 size_t len, |
| 1550 const rtc::PacketTime& packet_time) { | 1680 const rtc::PacketTime& packet_time) { |
| 1551 ASSERT(worker_thread_ == rtc::Thread::Current()); | 1681 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 1552 | 1682 |
| 1553 // Do not deliver, if packet doesn't belong to the correct transport channel. | 1683 // Do not deliver, if packet doesn't belong to the correct transport channel. |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1688 | 1818 |
| 1689 // During the initial state when nothing has been pinged yet, return the first | 1819 // During the initial state when nothing has been pinged yet, return the first |
| 1690 // one in the ordered |connections_|. | 1820 // one in the ordered |connections_|. |
| 1691 return *(std::find_if(connections_.begin(), connections_.end(), | 1821 return *(std::find_if(connections_.begin(), connections_.end(), |
| 1692 [conn1, conn2](Connection* conn) { | 1822 [conn1, conn2](Connection* conn) { |
| 1693 return conn == conn1 || conn == conn2; | 1823 return conn == conn1 || conn == conn2; |
| 1694 })); | 1824 })); |
| 1695 } | 1825 } |
| 1696 | 1826 |
| 1697 } // namespace cricket | 1827 } // namespace cricket |
| OLD | NEW |