diff --git a/p2p/base/basic_ice_controller.cc b/p2p/base/basic_ice_controller.cc index e2d5eef22b..9877388cd8 100644 --- a/p2p/base/basic_ice_controller.cc +++ b/p2p/base/basic_ice_controller.cc @@ -33,7 +33,7 @@ static constexpr int a_and_b_equal = 0; bool LocalCandidateUsesPreferredNetwork( const cricket::Connection* conn, absl::optional network_preference) { - rtc::AdapterType network_type = conn->port()->Network()->type(); + rtc::AdapterType network_type = conn->network()->type(); return network_preference.has_value() && (network_type == network_preference); } @@ -380,19 +380,19 @@ const Connection* BasicIceController::LeastRecentlyPinged( return nullptr; } -std::map +std::map BasicIceController::GetBestConnectionByNetwork() const { // |connections_| has been sorted, so the first one in the list on a given // network is the best connection on the network, except that the selected // connection is always the best connection on the network. - std::map best_connection_by_network; + std::map best_connection_by_network; if (selected_connection_) { - best_connection_by_network[selected_connection_->port()->Network()] = + best_connection_by_network[selected_connection_->network()] = selected_connection_; } // TODO(honghaiz): Need to update this if |connections_| are not sorted. for (const Connection* conn : connections_) { - rtc::Network* network = conn->port()->Network(); + const rtc::Network* network = conn->network(); // This only inserts when the network does not exist in the map. best_connection_by_network.insert(std::make_pair(network, conn)); } @@ -660,8 +660,8 @@ int BasicIceController::CompareConnectionCandidates(const Connection* a, // If we're still tied at this point, prefer a younger generation. // (Younger generation means a larger generation number). - int cmp = (a->remote_candidate().generation() + a->port()->generation()) - - (b->remote_candidate().generation() + b->port()->generation()); + int cmp = (a->remote_candidate().generation() + a->generation()) - + (b->remote_candidate().generation() + b->generation()); if (cmp != 0) { return cmp; } @@ -766,13 +766,13 @@ std::vector BasicIceController::PruneConnections() { auto best_connection_by_network = GetBestConnectionByNetwork(); for (const Connection* conn : connections_) { const Connection* best_conn = selected_connection_; - if (!rtc::IPIsAny(conn->port()->Network()->ip())) { + if (!rtc::IPIsAny(conn->network()->ip())) { // If the connection is bound to a specific network interface (not an // "any address" network), compare it against the best connection for // that network interface rather than the best connection overall. This // ensures that at least one connection per network will be left // unpruned. - best_conn = best_connection_by_network[conn->port()->Network()]; + best_conn = best_connection_by_network[conn->network()]; } // Do not prune connections if the connection being compared against is // weak. Otherwise, it may delete connections prematurely. diff --git a/p2p/base/basic_ice_controller.h b/p2p/base/basic_ice_controller.h index a0917e7e51..ae1339fc03 100644 --- a/p2p/base/basic_ice_controller.h +++ b/p2p/base/basic_ice_controller.h @@ -100,7 +100,8 @@ class BasicIceController : public IceControllerInterface { int CalculateActiveWritablePingInterval(const Connection* conn, int64_t now) const; - std::map GetBestConnectionByNetwork() const; + std::map GetBestConnectionByNetwork() + const; std::vector GetBestWritableConnectionPerNetwork() const; bool ReadyToSend(const Connection* connection) const; diff --git a/p2p/base/connection.cc b/p2p/base/connection.cc index 1c55619819..cb8c7c7e3d 100644 --- a/p2p/base/connection.cc +++ b/p2p/base/connection.cc @@ -286,6 +286,14 @@ const Candidate& Connection::remote_candidate() const { return remote_candidate_; } +const rtc::Network* Connection::network() const { + return port()->Network(); +} + +int Connection::generation() const { + return port()->generation(); +} + uint64_t Connection::priority() const { uint64_t priority = 0; // RFC 5245 - 5.7.2. Computing Pair Priority and Ordering Pairs diff --git a/p2p/base/connection.h b/p2p/base/connection.h index e0a0ba9f2b..fa9a519307 100644 --- a/p2p/base/connection.h +++ b/p2p/base/connection.h @@ -24,8 +24,8 @@ #include "p2p/base/transport_description.h" #include "rtc_base/async_packet_socket.h" #include "rtc_base/message_handler.h" +#include "rtc_base/network.h" #include "rtc_base/rate_tracker.h" -#include "rtc_base/third_party/sigslot/sigslot.h" namespace cricket { @@ -81,18 +81,19 @@ class Connection : public CandidatePairInterface, // A unique ID assigned when the connection is created. uint32_t id() { return id_; } - // The local port where this connection sends and receives packets. - Port* port() { return port_; } - const Port* port() const { return port_; } - // Implementation of virtual methods in CandidatePairInterface. // Returns the description of the local port const Candidate& local_candidate() const override; // Returns the description of the remote port to which we communicate. const Candidate& remote_candidate() const override; + // Return local network for this connection. + virtual const rtc::Network* network() const; + // Return generation for this connection. + virtual int generation() const; + // Returns the pair priority. - uint64_t priority() const; + virtual uint64_t priority() const; enum WriteState { STATE_WRITABLE = 0, // we have received ping responses recently @@ -297,6 +298,10 @@ class Connection : public CandidatePairInterface, // Check if we sent |val| pings without receving a response. bool TooManyOutstandingPings(const absl::optional& val) const; + // An accessor for unit tests. + Port* PortForTest() { return port_; } + const Port* PortForTest() const { return port_; } + protected: enum { MSG_DELETE = 0, MSG_FIRST_AVAILABLE }; @@ -330,6 +335,10 @@ class Connection : public CandidatePairInterface, void OnMessage(rtc::Message* pmsg) override; + // The local port where this connection sends and receives packets. + Port* port() { return port_; } + const Port* port() const { return port_; } + uint32_t id_; Port* port_; size_t local_candidate_index_; @@ -407,6 +416,7 @@ class Connection : public CandidatePairInterface, friend class Port; friend class ConnectionRequest; + friend class P2PTransportChannel; }; // ProxyConnection defers all the interesting work to the port. diff --git a/p2p/base/p2p_transport_channel.cc b/p2p/base/p2p_transport_channel.cc index 093c35d7ab..e26b06549b 100644 --- a/p2p/base/p2p_transport_channel.cc +++ b/p2p/base/p2p_transport_channel.cc @@ -132,6 +132,8 @@ P2PTransportChannel::P2PTransportChannel( [this] { return GetState(); }, [this] { return GetIceRole(); }, [this](const Connection* connection) { + // TODO(webrtc:10647/jonaso): Figure out a way to remove friendship + // between P2PTransportChannel and Connection. return IsPortPruned(connection->port()) || IsRemoteCandidatePruned(connection->remote_candidate()); }, @@ -355,9 +357,9 @@ IceTransportState P2PTransportChannel::ComputeState() const { return IceTransportState::STATE_FAILED; } - std::set networks; + std::set networks; for (Connection* connection : active_connections) { - rtc::Network* network = connection->port()->Network(); + const rtc::Network* network = connection->network(); if (networks.find(network) == networks.end()) { networks.insert(network); } else { diff --git a/p2p/base/p2p_transport_channel_unittest.cc b/p2p/base/p2p_transport_channel_unittest.cc index 042110bfc6..9cc58accbd 100644 --- a/p2p/base/p2p_transport_channel_unittest.cc +++ b/p2p/base/p2p_transport_channel_unittest.cc @@ -2067,9 +2067,9 @@ TEST_F(P2PTransportChannelTest, TestUsingPooledSessionBeforeDoneGathering) { auto pooled_ports_1 = pooled_session_1->ReadyPorts(); auto pooled_ports_2 = pooled_session_2->ReadyPorts(); EXPECT_THAT(pooled_ports_1, - Contains(ep1_ch1()->selected_connection()->port())); + Contains(ep1_ch1()->selected_connection()->PortForTest())); EXPECT_THAT(pooled_ports_2, - Contains(ep2_ch1()->selected_connection()->port())); + Contains(ep2_ch1()->selected_connection()->PortForTest())); } // Test that a connection succeeds when the P2PTransportChannel uses a pooled @@ -2109,9 +2109,9 @@ TEST_F(P2PTransportChannelTest, TestUsingPooledSessionAfterDoneGathering) { auto pooled_ports_1 = pooled_session_1->ReadyPorts(); auto pooled_ports_2 = pooled_session_2->ReadyPorts(); EXPECT_THAT(pooled_ports_1, - Contains(ep1_ch1()->selected_connection()->port())); + Contains(ep1_ch1()->selected_connection()->PortForTest())); EXPECT_THAT(pooled_ports_2, - Contains(ep2_ch1()->selected_connection()->port())); + Contains(ep2_ch1()->selected_connection()->PortForTest())); } // Test that when the "presume_writable_when_fully_relayed" flag is set to @@ -4389,10 +4389,10 @@ TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnRemovedPort) { // Make a fake signal to remove the ports in the p2ptransportchannel. then // change the ICE role and expect it to be updated. - std::vector ports(1, conn->port()); + std::vector ports(1, conn->PortForTest()); ch.allocator_session()->SignalPortsPruned(ch.allocator_session(), ports); ch.SetIceRole(ICEROLE_CONTROLLED); - EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole()); + EXPECT_EQ(ICEROLE_CONTROLLED, conn->PortForTest()->GetIceRole()); } // Test that the ICE role is updated even on ports with inactive networks. @@ -4415,7 +4415,7 @@ TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnPortAfterIceRestart) { ch.SetIceParameters(kIceParams[1]); ch.MaybeStartGathering(); ch.SetIceRole(ICEROLE_CONTROLLED); - EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole()); + EXPECT_EQ(ICEROLE_CONTROLLED, conn->PortForTest()->GetIceRole()); } // Test that after some amount of time without receiving data, the connection