henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // P2PTransportChannel wraps up the state management of the connection between |
| 12 | // two P2P clients. Clients have candidate ports for connecting, and |
| 13 | // connections which are combinations of candidates from each end (Alice and |
| 14 | // Bob each have candidates, one candidate from Alice and one candidate from |
| 15 | // Bob are used to make a connection, repeat to make many connections). |
| 16 | // |
| 17 | // When all of the available connections become invalid (non-writable), we |
| 18 | // kick off a process of determining more candidates and more connections. |
| 19 | // |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #ifndef P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
| 21 | #define P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 22 | |
| 23 | #include <map> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 24 | #include <memory> |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 25 | #include <set> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 28 | |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 29 | #include "api/candidate.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "p2p/base/candidatepairinterface.h" |
| 31 | #include "p2p/base/icetransportinternal.h" |
| 32 | #include "p2p/base/portallocator.h" |
| 33 | #include "p2p/base/portinterface.h" |
| 34 | #include "rtc_base/asyncpacketsocket.h" |
| 35 | #include "rtc_base/constructormagic.h" |
| 36 | #include "rtc_base/random.h" |
| 37 | #include "rtc_base/sigslot.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 38 | |
| 39 | namespace cricket { |
| 40 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 41 | // Enum for UMA metrics, used to record whether the channel is |
| 42 | // connected/connecting/disconnected when ICE restart happens. |
| 43 | enum class IceRestartState { CONNECTING, CONNECTED, DISCONNECTED, MAX_VALUE }; |
| 44 | |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 45 | extern const int WEAK_PING_INTERVAL; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 46 | extern const int STRONG_PING_INTERVAL; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 47 | extern const int WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL; |
| 48 | extern const int STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL; |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 49 | static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3; |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame] | 50 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 51 | // Adds the port on which the candidate originated. |
| 52 | class RemoteCandidate : public Candidate { |
| 53 | public: |
| 54 | RemoteCandidate(const Candidate& c, PortInterface* origin_port) |
| 55 | : Candidate(c), origin_port_(origin_port) {} |
| 56 | |
| 57 | PortInterface* origin_port() { return origin_port_; } |
| 58 | |
| 59 | private: |
| 60 | PortInterface* origin_port_; |
| 61 | }; |
| 62 | |
| 63 | // P2PTransportChannel manages the candidates and connection process to keep |
| 64 | // two P2P clients connected to each other. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 65 | class P2PTransportChannel : public IceTransportInternal, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 66 | public rtc::MessageHandler { |
| 67 | public: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 68 | P2PTransportChannel(const std::string& transport_name, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 69 | int component, |
mikescarlett | b9dd7c5 | 2016-02-19 20:43:45 -0800 | [diff] [blame] | 70 | PortAllocator* allocator); |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 71 | ~P2PTransportChannel() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 72 | |
| 73 | // From TransportChannelImpl: |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 74 | IceTransportState GetState() const override; |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 75 | const std::string& transport_name() const override; |
| 76 | int component() const override; |
| 77 | bool writable() const override; |
| 78 | bool receiving() const override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 79 | void SetIceRole(IceRole role) override; |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 80 | IceRole GetIceRole() const override; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 81 | void SetIceTiebreaker(uint64_t tiebreaker) override; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 82 | void SetIceParameters(const IceParameters& ice_params) override; |
| 83 | void SetRemoteIceParameters(const IceParameters& ice_params) override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 84 | void SetRemoteIceMode(IceMode mode) override; |
deadbeef | 886815b | 2016-06-29 15:21:04 -0700 | [diff] [blame] | 85 | // TODO(deadbeef): Deprecated. Remove when Chromium's |
| 86 | // IceTransportChannel does not depend on this. |
| 87 | void Connect() {} |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 88 | void MaybeStartGathering() override; |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 89 | IceGatheringState gathering_state() const override; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 90 | void AddRemoteCandidate(const Candidate& candidate) override; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 91 | void RemoveRemoteCandidate(const Candidate& candidate) override; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 92 | // Sets the parameters in IceConfig. We do not set them blindly. Instead, we |
| 93 | // only update the parameter if it is considered set in |config|. For example, |
| 94 | // a negative value of receiving_timeout will be considered "not set" and we |
| 95 | // will not use it to update the respective parameter in |config_|. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 96 | // TODO(deadbeef): Use rtc::Optional instead of negative values. |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 97 | void SetIceConfig(const IceConfig& config) override; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 98 | const IceConfig& config() const; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 99 | void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 100 | |
| 101 | // From TransportChannel: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 102 | int SendPacket(const char* data, |
| 103 | size_t len, |
| 104 | const rtc::PacketOptions& options, |
| 105 | int flags) override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 106 | int SetOption(rtc::Socket::Option opt, int value) override; |
| 107 | bool GetOption(rtc::Socket::Option opt, int* value) override; |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 108 | int GetError() override; |
Henrik Boström | f3ecdb9 | 2015-09-08 12:11:54 +0200 | [diff] [blame] | 109 | bool GetStats(std::vector<ConnectionInfo>* stats) override; |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 110 | rtc::Optional<int> GetRttEstimate() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 111 | |
Honghai Zhang | 8cd7f22 | 2016-06-23 13:44:34 -0700 | [diff] [blame] | 112 | // TODO(honghaiz): Remove this method once the reference of it in |
| 113 | // Chromoting is removed. |
| 114 | const Connection* best_connection() const { return selected_connection_; } |
| 115 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 116 | const Connection* selected_connection() const { return selected_connection_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 117 | void set_incoming_only(bool value) { incoming_only_ = value; } |
| 118 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 119 | // Note: These are only for testing purpose. |
| 120 | // |ports_| and |pruned_ports| should not be changed from outside. |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 121 | const std::vector<PortInterface*>& ports() { return ports_; } |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 122 | const std::vector<PortInterface*>& pruned_ports() { return pruned_ports_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 123 | |
| 124 | IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 125 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 126 | void PruneAllPorts(); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 127 | int receiving_timeout() const { return config_.receiving_timeout; } |
| 128 | int check_receiving_interval() const { return check_receiving_interval_; } |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 129 | |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame^] | 130 | rtc::Optional<rtc::NetworkRoute> network_route() const override; |
| 131 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 132 | // Helper method used only in unittest. |
| 133 | rtc::DiffServCodePoint DefaultDscpValue() const; |
| 134 | |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 135 | // Public for unit tests. |
| 136 | Connection* FindNextPingableConnection(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 137 | void MarkConnectionPinged(Connection* conn); |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 138 | |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 139 | // Public for unit tests. |
| 140 | const std::vector<Connection*>& connections() const { return connections_; } |
| 141 | |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 142 | // Public for unit tests. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 143 | PortAllocatorSession* allocator_session() { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 144 | return allocator_sessions_.back().get(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 145 | } |
| 146 | |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 147 | // Public for unit tests. |
| 148 | const std::vector<RemoteCandidate>& remote_candidates() const { |
| 149 | return remote_candidates_; |
| 150 | } |
| 151 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 152 | std::string ToString() const { |
| 153 | const char RECEIVING_ABBREV[2] = {'_', 'R'}; |
| 154 | const char WRITABLE_ABBREV[2] = {'_', 'W'}; |
| 155 | std::stringstream ss; |
| 156 | ss << "Channel[" << transport_name_ << "|" << component_ << "|" |
| 157 | << RECEIVING_ABBREV[receiving_] << WRITABLE_ABBREV[writable_] << "]"; |
| 158 | return ss.str(); |
| 159 | } |
| 160 | |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 161 | private: |
johan | 0fd22ef | 2016-09-29 01:19:20 -0700 | [diff] [blame] | 162 | rtc::Thread* thread() const { return network_thread_; } |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 163 | bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); } |
| 164 | |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 165 | // A transport channel is weak if the current best connection is either |
| 166 | // not receiving or not writable, or if there is no best connection at all. |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 167 | bool weak() const; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 168 | |
| 169 | int weak_ping_interval() const { |
| 170 | if (config_.ice_check_min_interval && |
| 171 | weak_ping_interval_ < *config_.ice_check_min_interval) { |
| 172 | return *config_.ice_check_min_interval; |
| 173 | } |
| 174 | return weak_ping_interval_; |
| 175 | } |
| 176 | |
| 177 | int strong_ping_interval() const { |
| 178 | if (config_.ice_check_min_interval && |
| 179 | STRONG_PING_INTERVAL < *config_.ice_check_min_interval) { |
| 180 | return *config_.ice_check_min_interval; |
| 181 | } |
| 182 | return STRONG_PING_INTERVAL; |
| 183 | } |
| 184 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 185 | // Returns true if it's possible to send packets on |connection|. |
| 186 | bool ReadyToSend(Connection* connection) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 187 | void UpdateConnectionStates(); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 188 | void RequestSortAndStateUpdate(); |
| 189 | // Start pinging if we haven't already started, and we now have a connection |
| 190 | // that's pingable. |
| 191 | void MaybeStartPinging(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 192 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 193 | // The methods below return a positive value if |a| is preferable to |b|, |
| 194 | // a negative value if |b| is preferable, and 0 if they're equally preferable. |
| 195 | // If |receiving_unchanged_threshold| is set, then when |b| is receiving and |
| 196 | // |a| is not, returns a negative value only if |b| has been in receiving |
| 197 | // state and |a| has been in not receiving state since |
| 198 | // |receiving_unchanged_threshold| and sets |
| 199 | // |missed_receiving_unchanged_threshold| to true otherwise. |
| 200 | int CompareConnectionStates( |
| 201 | const cricket::Connection* a, |
| 202 | const cricket::Connection* b, |
| 203 | rtc::Optional<int64_t> receiving_unchanged_threshold, |
| 204 | bool* missed_receiving_unchanged_threshold) const; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 205 | int CompareConnectionCandidates(const cricket::Connection* a, |
| 206 | const cricket::Connection* b) const; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 207 | // Compares two connections based on the connection states |
| 208 | // (writable/receiving/connected), nomination states, last data received time, |
| 209 | // and static preferences. Does not include latency. Used by both sorting |
| 210 | // and ShouldSwitchSelectedConnection(). |
| 211 | // Returns a positive value if |a| is better than |b|. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 212 | int CompareConnections(const cricket::Connection* a, |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 213 | const cricket::Connection* b, |
| 214 | rtc::Optional<int64_t> receiving_unchanged_threshold, |
| 215 | bool* missed_receiving_unchanged_threshold) const; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 216 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 217 | bool PresumedWritable(const cricket::Connection* conn) const; |
| 218 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 219 | void SortConnectionsAndUpdateState(); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 220 | void SwitchSelectedConnection(Connection* conn); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 221 | void UpdateState(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 222 | void HandleAllTimedOut(); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 223 | void MaybeStopPortAllocatorSessions(); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 224 | IceTransportState ComputeState() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 225 | |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 226 | Connection* GetBestConnectionOnNetwork(rtc::Network* network) const; |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 227 | bool CreateConnections(const Candidate& remote_candidate, |
| 228 | PortInterface* origin_port); |
| 229 | bool CreateConnection(PortInterface* port, |
| 230 | const Candidate& remote_candidate, |
| 231 | PortInterface* origin_port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 232 | bool FindConnection(cricket::Connection* connection) const; |
| 233 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 234 | uint32_t GetRemoteCandidateGeneration(const Candidate& candidate); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 235 | bool IsDuplicateRemoteCandidate(const Candidate& candidate); |
| 236 | void RememberRemoteCandidate(const Candidate& remote_candidate, |
| 237 | PortInterface* origin_port); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 238 | bool IsPingable(const Connection* conn, int64_t now) const; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 239 | // Whether a writable connection is past its ping interval and needs to be |
| 240 | // pinged again. |
| 241 | bool WritableConnectionPastPingInterval(const Connection* conn, |
| 242 | int64_t now) const; |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 243 | int CalculateActiveWritablePingInterval(const Connection* conn, |
| 244 | int64_t now) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 245 | void PingConnection(Connection* conn); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 246 | void AddAllocatorSession(std::unique_ptr<PortAllocatorSession> session); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 247 | void AddConnection(Connection* connection); |
| 248 | |
| 249 | void OnPortReady(PortAllocatorSession *session, PortInterface* port); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 250 | void OnPortsPruned(PortAllocatorSession* session, |
| 251 | const std::vector<PortInterface*>& ports); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 252 | void OnCandidatesReady(PortAllocatorSession *session, |
| 253 | const std::vector<Candidate>& candidates); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 254 | void OnCandidatesRemoved(PortAllocatorSession* session, |
| 255 | const std::vector<Candidate>& candidates); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 256 | void OnCandidatesAllocationDone(PortAllocatorSession* session); |
| 257 | void OnUnknownAddress(PortInterface* port, |
| 258 | const rtc::SocketAddress& addr, |
| 259 | ProtocolType proto, |
| 260 | IceMessage* stun_msg, |
| 261 | const std::string& remote_username, |
| 262 | bool port_muxed); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 263 | |
| 264 | // When a port is destroyed, remove it from both lists |ports_| |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 265 | // and |pruned_ports_|. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 266 | void OnPortDestroyed(PortInterface* port); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 267 | // When pruning a port, move it from |ports_| to |pruned_ports_|. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 268 | // Returns true if the port is found and removed from |ports_|. |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 269 | bool PrunePort(PortInterface* port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 270 | void OnRoleConflict(PortInterface* port); |
| 271 | |
| 272 | void OnConnectionStateChange(Connection* connection); |
| 273 | void OnReadPacket(Connection *connection, const char *data, size_t len, |
| 274 | const rtc::PacketTime& packet_time); |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 275 | void OnSentPacket(const rtc::SentPacket& sent_packet); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 276 | void OnReadyToSend(Connection* connection); |
| 277 | void OnConnectionDestroyed(Connection *connection); |
| 278 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 279 | void OnNominated(Connection* conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 280 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 281 | void OnMessage(rtc::Message* pmsg) override; |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 282 | void OnCheckAndPing(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 283 | void OnRegatherOnFailedNetworks(); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 284 | void OnRegatherOnAllNetworks(); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 285 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 286 | uint32_t GetNominationAttr(Connection* conn) const; |
| 287 | bool GetUseCandidateAttr(Connection* conn, NominationMode mode) const; |
| 288 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 289 | // Returns true if we should switch to the new connection. |
| 290 | // sets |missed_receiving_unchanged_threshold| to true if either |
| 291 | // the selected connection or the new connection missed its |
| 292 | // receiving-unchanged-threshold. |
| 293 | bool ShouldSwitchSelectedConnection( |
| 294 | Connection* new_connection, |
| 295 | bool* missed_receiving_unchanged_threshold) const; |
| 296 | // Returns true if the new_connection is selected for transmission. |
| 297 | bool MaybeSwitchSelectedConnection(Connection* new_connection, |
| 298 | const std::string& reason); |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 299 | // Gets the best connection for each network. |
| 300 | std::map<rtc::Network*, Connection*> GetBestConnectionByNetwork() const; |
| 301 | std::vector<Connection*> GetBestWritableConnectionPerNetwork() const; |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 302 | void PruneConnections(); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 303 | bool IsBackupConnection(const Connection* conn) const; |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 304 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 305 | Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 306 | // Between |conn1| and |conn2|, this function returns the one which should |
| 307 | // be pinged first. |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 308 | Connection* MorePingable(Connection* conn1, Connection* conn2); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 309 | // Select the connection which is Relay/Relay. If both of them are, |
| 310 | // UDP relay protocol takes precedence. |
| 311 | Connection* MostLikelyToWork(Connection* conn1, Connection* conn2); |
| 312 | // Compare the last_ping_sent time and return the one least recently pinged. |
| 313 | Connection* LeastRecentlyPinged(Connection* conn1, Connection* conn2); |
| 314 | |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 315 | // Returns the latest remote ICE parameters or nullptr if there are no remote |
| 316 | // ICE parameters yet. |
| 317 | IceParameters* remote_ice() { |
| 318 | return remote_ice_parameters_.empty() ? nullptr |
| 319 | : &remote_ice_parameters_.back(); |
| 320 | } |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 321 | // Returns the remote IceParameters and generation that match |ufrag| |
| 322 | // if found, and returns nullptr otherwise. |
| 323 | const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag, |
| 324 | uint32_t* generation); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 325 | // Returns the index of the latest remote ICE parameters, or 0 if no remote |
| 326 | // ICE parameters have been received. |
| 327 | uint32_t remote_ice_generation() { |
| 328 | return remote_ice_parameters_.empty() |
| 329 | ? 0 |
| 330 | : static_cast<uint32_t>(remote_ice_parameters_.size() - 1); |
| 331 | } |
| 332 | |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 333 | // Samples a delay from the uniform distribution defined by the |
| 334 | // regather_on_all_networks_interval ICE configuration pair. |
| 335 | int SampleRegatherAllNetworksInterval(); |
| 336 | |
| 337 | // Indicates if the given local port has been pruned. |
| 338 | bool IsPortPruned(const Port* port) const; |
| 339 | |
| 340 | // Indicates if the given remote candidate has been pruned. |
| 341 | bool IsRemoteCandidatePruned(const Candidate& cand) const; |
| 342 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 343 | // Sets the writable state, signaling if necessary. |
| 344 | void set_writable(bool writable); |
| 345 | // Sets the receiving state, signaling if necessary. |
| 346 | void set_receiving(bool receiving); |
| 347 | |
| 348 | std::string transport_name_; |
| 349 | int component_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 350 | PortAllocator* allocator_; |
johan | 0fd22ef | 2016-09-29 01:19:20 -0700 | [diff] [blame] | 351 | rtc::Thread* network_thread_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 352 | bool incoming_only_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 353 | int error_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 354 | std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_; |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 355 | // |ports_| contains ports that are used to form new connections when |
| 356 | // new remote candidates are added. |
| 357 | std::vector<PortInterface*> ports_; |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 358 | // |pruned_ports_| contains ports that have been removed from |ports_| and |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 359 | // are not being used to form new connections, but that aren't yet destroyed. |
| 360 | // They may have existing connections, and they still fire signals such as |
| 361 | // SignalUnknownAddress. |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 362 | std::vector<PortInterface*> pruned_ports_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 363 | |
| 364 | // |connections_| is a sorted list with the first one always be the |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 365 | // |selected_connection_| when it's not nullptr. The combination of |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 366 | // |pinged_connections_| and |unpinged_connections_| has the same |
| 367 | // connections as |connections_|. These 2 sets maintain whether a |
| 368 | // connection should be pinged next or not. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 369 | std::vector<Connection *> connections_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 370 | std::set<Connection*> pinged_connections_; |
| 371 | std::set<Connection*> unpinged_connections_; |
| 372 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 373 | Connection* selected_connection_ = nullptr; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 374 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 375 | std::vector<RemoteCandidate> remote_candidates_; |
| 376 | bool sort_dirty_; // indicates whether another sort is needed right now |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 377 | bool had_connection_ = false; // if connections_ has ever been nonempty |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 378 | typedef std::map<rtc::Socket::Option, int> OptionMap; |
| 379 | OptionMap options_; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 380 | IceParameters ice_parameters_; |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 381 | std::vector<IceParameters> remote_ice_parameters_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 382 | IceMode remote_ice_mode_; |
| 383 | IceRole ice_role_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 384 | uint64_t tiebreaker_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 385 | IceGatheringState gathering_state_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 386 | |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 387 | // Used to generate random intervals for regather_all_networks_interval_range. |
| 388 | webrtc::Random rand_; |
| 389 | |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 390 | int check_receiving_interval_; |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 391 | int64_t last_ping_sent_ms_ = 0; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 392 | int weak_ping_interval_ = WEAK_PING_INTERVAL; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 393 | IceTransportState state_ = IceTransportState::STATE_INIT; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 394 | IceConfig config_; |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 395 | int last_sent_packet_id_ = -1; // -1 indicates no packet was sent before. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 396 | bool started_pinging_ = false; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 397 | // The value put in the "nomination" attribute for the next nominated |
| 398 | // connection. A zero-value indicates the connection will not be nominated. |
| 399 | uint32_t nomination_ = 0; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 400 | bool receiving_ = false; |
| 401 | bool writable_ = false; |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 402 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 403 | webrtc::MetricsObserverInterface* metrics_observer_ = nullptr; |
| 404 | |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame^] | 405 | rtc::Optional<rtc::NetworkRoute> network_route_; |
| 406 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 407 | RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 408 | }; |
| 409 | |
| 410 | } // namespace cricket |
| 411 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 412 | #endif // P2P_BASE_P2PTRANSPORTCHANNEL_H_ |