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 1288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1299 | 1299 |
1300 // Also possibly start pinging. | 1300 // Also possibly start pinging. |
1301 // We could start pinging if: | 1301 // We could start pinging if: |
1302 // * The first connection was created. | 1302 // * The first connection was created. |
1303 // * ICE credentials were provided. | 1303 // * ICE credentials were provided. |
1304 // * A TCP connection became connected. | 1304 // * A TCP connection became connected. |
1305 MaybeStartPinging(); | 1305 MaybeStartPinging(); |
1306 } | 1306 } |
1307 | 1307 |
1308 void P2PTransportChannel::PruneConnections() { | 1308 void P2PTransportChannel::PruneConnections() { |
1309 // We can prune any connection for which there is a connected, writable | 1309 // We can prune any connection for which there is a connected, writable, |
1310 // connection on the same network with better or equal priority. We leave | 1310 // and receiving connection with the same network name with better or equal |
1311 // those with better priority just in case they become writable later (at | 1311 // priority. We leave those with better priority just in case they become |
1312 // which point, we would prune out the current selected connection). We leave | 1312 // writable later (at which point, we would prune out the current selected |
1313 // connections on other networks because they may not be using the same | 1313 // connection). We leave connections on other networks because they may not |
1314 // resources and they may represent very distinct paths over which we can | 1314 // be using the same resources and they may represent very distinct paths |
1315 // switch. If the |premier| connection is not connected, we may be | 1315 // over which we can switch. If the |premier| connection is not connected, |
1316 // reconnecting a TCP connection and temporarily do not prune connections in | 1316 // we may be reconnecting a TCP connection and temporarily do not prune |
1317 // this network. See the big comment in CompareConnectionStates. | 1317 // connections in this network. See the big comment in |
| 1318 // CompareConnectionStates. |
1318 | 1319 |
1319 // Get a list of the networks that we are using. | 1320 std::map<std::string, Connection*> premier_connection_by_network_name; |
1320 std::set<rtc::Network*> networks; | 1321 if (selected_connection_) { |
1321 for (const Connection* conn : connections_) { | 1322 // |selected_connection_| is always a premier connection. |
1322 networks.insert(conn->port()->Network()); | 1323 const std::string& network_name = |
| 1324 selected_connection_->port()->Network()->name(); |
| 1325 premier_connection_by_network_name[network_name] = selected_connection_; |
1323 } | 1326 } |
1324 for (rtc::Network* network : networks) { | 1327 for (Connection* conn : connections_) { |
1325 Connection* premier = GetBestConnectionOnNetwork(network); | 1328 const std::string& network_name = conn->port()->Network()->name(); |
1326 // Do not prune connections if the current selected connection is weak on | 1329 Connection* premier = premier_connection_by_network_name[network_name]; |
1327 // this network. Otherwise, it may delete connections prematurely. | 1330 // Since the connections are sorted, the first one with a given network name |
1328 if (!premier || premier->weak()) { | 1331 // is the premier connection for the network name. |
1329 continue; | 1332 // |premier| might be equal to |conn| if this is the selected connection. |
1330 } | 1333 if (premier == nullptr) { |
1331 | 1334 premier_connection_by_network_name[network_name] = conn; |
1332 for (Connection* conn : connections_) { | 1335 } else if (premier != conn && !premier->weak() && |
1333 if ((conn != premier) && (conn->port()->Network() == network) && | 1336 CompareConnectionCandidates(premier, conn) >= 0) { |
1334 (CompareConnectionCandidates(premier, conn) >= 0)) { | 1337 conn->Prune(); |
1335 conn->Prune(); | |
1336 } | |
1337 } | 1338 } |
1338 } | 1339 } |
1339 } | 1340 } |
1340 | 1341 |
1341 // Change the selected connection, and let listeners know. | 1342 // Change the selected connection, and let listeners know. |
1342 void P2PTransportChannel::SwitchSelectedConnection(Connection* conn) { | 1343 void P2PTransportChannel::SwitchSelectedConnection(Connection* conn) { |
1343 // Note: if conn is NULL, the previous |selected_connection_| has been | 1344 // Note: if conn is NULL, the previous |selected_connection_| has been |
1344 // destroyed, so don't use it. | 1345 // destroyed, so don't use it. |
1345 Connection* old_selected_connection = selected_connection_; | 1346 Connection* old_selected_connection = selected_connection_; |
1346 selected_connection_ = conn; | 1347 selected_connection_ = conn; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1464 bool P2PTransportChannel::ReadyToSend(Connection* connection) const { | 1465 bool P2PTransportChannel::ReadyToSend(Connection* connection) const { |
1465 // Note that we allow sending on an unreliable connection, because it's | 1466 // Note that we allow sending on an unreliable connection, because it's |
1466 // possible that it became unreliable simply due to bad chance. | 1467 // possible that it became unreliable simply due to bad chance. |
1467 // So this shouldn't prevent attempting to send media. | 1468 // So this shouldn't prevent attempting to send media. |
1468 return connection != nullptr && | 1469 return connection != nullptr && |
1469 (connection->writable() || | 1470 (connection->writable() || |
1470 connection->write_state() == Connection::STATE_WRITE_UNRELIABLE || | 1471 connection->write_state() == Connection::STATE_WRITE_UNRELIABLE || |
1471 PresumedWritable(connection)); | 1472 PresumedWritable(connection)); |
1472 } | 1473 } |
1473 | 1474 |
1474 // If we have a selected connection, return it, otherwise return top one in the | |
1475 // list (later we will mark it best). | |
1476 Connection* P2PTransportChannel::GetBestConnectionOnNetwork( | |
1477 rtc::Network* network) const { | |
1478 // If the selected connection is on this network, then it wins. | |
1479 if (selected_connection_ && | |
1480 (selected_connection_->port()->Network() == network)) { | |
1481 return selected_connection_; | |
1482 } | |
1483 | |
1484 // Otherwise, we return the top-most in sorted order. | |
1485 for (size_t i = 0; i < connections_.size(); ++i) { | |
1486 if (connections_[i]->port()->Network() == network) { | |
1487 return connections_[i]; | |
1488 } | |
1489 } | |
1490 | |
1491 return NULL; | |
1492 } | |
1493 | |
1494 // Handle any queued up requests | 1475 // Handle any queued up requests |
1495 void P2PTransportChannel::OnMessage(rtc::Message *pmsg) { | 1476 void P2PTransportChannel::OnMessage(rtc::Message *pmsg) { |
1496 switch (pmsg->message_id) { | 1477 switch (pmsg->message_id) { |
1497 case MSG_SORT_AND_UPDATE_STATE: | 1478 case MSG_SORT_AND_UPDATE_STATE: |
1498 SortConnectionsAndUpdateState(); | 1479 SortConnectionsAndUpdateState(); |
1499 break; | 1480 break; |
1500 case MSG_CHECK_AND_PING: | 1481 case MSG_CHECK_AND_PING: |
1501 OnCheckAndPing(); | 1482 OnCheckAndPing(); |
1502 break; | 1483 break; |
1503 case MSG_REGATHER_ON_FAILED_NETWORKS: | 1484 case MSG_REGATHER_ON_FAILED_NETWORKS: |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1987 | 1968 |
1988 // During the initial state when nothing has been pinged yet, return the first | 1969 // During the initial state when nothing has been pinged yet, return the first |
1989 // one in the ordered |connections_|. | 1970 // one in the ordered |connections_|. |
1990 return *(std::find_if(connections_.begin(), connections_.end(), | 1971 return *(std::find_if(connections_.begin(), connections_.end(), |
1991 [conn1, conn2](Connection* conn) { | 1972 [conn1, conn2](Connection* conn) { |
1992 return conn == conn1 || conn == conn2; | 1973 return conn == conn1 || conn == conn2; |
1993 })); | 1974 })); |
1994 } | 1975 } |
1995 | 1976 |
1996 } // namespace cricket | 1977 } // namespace cricket |
OLD | NEW |