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