make Connection::port() protected

This patch makes Connection::port() protected
and add explicit methods for the use cases instead
- network()     - port()->Network()
- generation()  - port()->generation()

This is done to easier mock a Connection.

BUG=webrtc:10647

Change-Id: I5b35477ed9f81d57cd871072874262d0a8af2d4c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/160784
Commit-Queue: Jonas Oreland <jonaso@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29929}
This commit is contained in:
Jonas Oreland 2019-11-27 11:02:45 +01:00 committed by Commit Bot
parent b1ccae253e
commit 98e745b302
6 changed files with 46 additions and 25 deletions

View File

@ -33,7 +33,7 @@ static constexpr int a_and_b_equal = 0;
bool LocalCandidateUsesPreferredNetwork(
const cricket::Connection* conn,
absl::optional<rtc::AdapterType> 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<rtc::Network*, const Connection*>
std::map<const rtc::Network*, const Connection*>
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<rtc::Network*, const Connection*> best_connection_by_network;
std::map<const rtc::Network*, const Connection*> 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<const Connection*> 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.

View File

@ -100,7 +100,8 @@ class BasicIceController : public IceControllerInterface {
int CalculateActiveWritablePingInterval(const Connection* conn,
int64_t now) const;
std::map<rtc::Network*, const Connection*> GetBestConnectionByNetwork() const;
std::map<const rtc::Network*, const Connection*> GetBestConnectionByNetwork()
const;
std::vector<const Connection*> GetBestWritableConnectionPerNetwork() const;
bool ReadyToSend(const Connection* connection) const;

View File

@ -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

View File

@ -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<int>& 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.

View File

@ -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<rtc::Network*> networks;
std::set<const rtc::Network*> 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 {

View File

@ -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<PortInterface*> ports(1, conn->port());
std::vector<PortInterface*> 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