blob: 7ffa4300893141a2e61ff74ba40ab8c966704a50 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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//
20#ifndef WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_
21#define WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_
22
23#include <map>
kwiberg3ec46792016-04-27 07:22:53 -070024#include <memory>
guoweis36f01372016-03-02 18:02:40 -080025#include <set>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026#include <string>
27#include <vector>
kwiberg4485ffb2016-04-26 08:14:39 -070028
29#include "webrtc/base/constructormagic.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000030#include "webrtc/p2p/base/candidate.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070031#include "webrtc/p2p/base/candidatepairinterface.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032#include "webrtc/p2p/base/p2ptransport.h"
33#include "webrtc/p2p/base/portallocator.h"
34#include "webrtc/p2p/base/portinterface.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035#include "webrtc/p2p/base/transportchannelimpl.h"
36#include "webrtc/base/asyncpacketsocket.h"
37#include "webrtc/base/sigslot.h"
38
39namespace cricket {
40
Honghai Zhang049fbb12016-03-07 11:13:07 -080041extern const int WEAK_PING_INTERVAL;
zhihuang435264a2016-06-21 11:28:38 -070042extern const int STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL;
43extern const int STABLE_WRITABLE_CONNECTION_PING_INTERVAL;
honghaiz524ecc22016-05-25 12:48:31 -070044static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3;
guoweisb0bb77f2015-10-26 15:10:01 -070045
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046// Adds the port on which the candidate originated.
47class RemoteCandidate : public Candidate {
48 public:
49 RemoteCandidate(const Candidate& c, PortInterface* origin_port)
50 : Candidate(c), origin_port_(origin_port) {}
51
52 PortInterface* origin_port() { return origin_port_; }
53
54 private:
55 PortInterface* origin_port_;
56};
57
58// P2PTransportChannel manages the candidates and connection process to keep
59// two P2P clients connected to each other.
60class P2PTransportChannel : public TransportChannelImpl,
61 public rtc::MessageHandler {
62 public:
deadbeefcbecd352015-09-23 11:50:27 -070063 P2PTransportChannel(const std::string& transport_name,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064 int component,
mikescarlettb9dd7c52016-02-19 20:43:45 -080065 PortAllocator* allocator);
66 // TODO(mikescarlett): Deprecated. Remove when Chromium's
67 // IceTransportChannel does not depend on this.
68 P2PTransportChannel(const std::string& transport_name,
69 int component,
guidou74db7772016-02-18 01:57:49 -080070 P2PTransport* transport,
deadbeefcbecd352015-09-23 11:50:27 -070071 PortAllocator* allocator);
72 virtual ~P2PTransportChannel();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073
74 // From TransportChannelImpl:
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020075 TransportChannelState GetState() const override;
76 void SetIceRole(IceRole role) override;
77 IceRole GetIceRole() const override { return ice_role_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +020078 void SetIceTiebreaker(uint64_t tiebreaker) override;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -070079 void SetIceParameters(const IceParameters& ice_params) override;
80 void SetRemoteIceParameters(const IceParameters& ice_params) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020081 void SetRemoteIceMode(IceMode mode) override;
deadbeef886815b2016-06-29 15:21:04 -070082 // TODO(deadbeef): Deprecated. Remove when Chromium's
83 // IceTransportChannel does not depend on this.
84 void Connect() {}
deadbeefcbecd352015-09-23 11:50:27 -070085 void MaybeStartGathering() override;
86 IceGatheringState gathering_state() const override {
87 return gathering_state_;
88 }
89 void AddRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -070090 void RemoveRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang049fbb12016-03-07 11:13:07 -080091 // Sets the parameters in IceConfig. We do not set them blindly. Instead, we
92 // only update the parameter if it is considered set in |config|. For example,
93 // a negative value of receiving_timeout will be considered "not set" and we
94 // will not use it to update the respective parameter in |config_|.
deadbeef14f97f52016-06-22 17:14:15 -070095 // TODO(deadbeef): Use rtc::Optional instead of negative values.
honghaiz1f429e32015-09-28 07:57:34 -070096 void SetIceConfig(const IceConfig& config) override;
guoweis36f01372016-03-02 18:02:40 -080097 const IceConfig& config() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000098
99 // From TransportChannel:
deadbeefcbecd352015-09-23 11:50:27 -0700100 int SendPacket(const char* data,
101 size_t len,
102 const rtc::PacketOptions& options,
103 int flags) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200104 int SetOption(rtc::Socket::Option opt, int value) override;
105 bool GetOption(rtc::Socket::Option opt, int* value) override;
106 int GetError() override { return error_; }
107 bool GetStats(std::vector<ConnectionInfo>* stats) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108
Honghai Zhang8cd7f222016-06-23 13:44:34 -0700109 // TODO(honghaiz): Remove this method once the reference of it in
110 // Chromoting is removed.
111 const Connection* best_connection() const { return selected_connection_; }
112
Honghai Zhang572b0942016-06-23 12:26:57 -0700113 const Connection* selected_connection() const { return selected_connection_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 void set_incoming_only(bool value) { incoming_only_ = value; }
115
Honghai Zhanga74363c2016-07-28 18:06:15 -0700116 // Note: These are only for testing purpose.
117 // |ports_| and |pruned_ports| should not be changed from outside.
Peter Thatcher1fe120a2015-06-10 11:33:17 -0700118 const std::vector<PortInterface*>& ports() { return ports_; }
Honghai Zhanga74363c2016-07-28 18:06:15 -0700119 const std::vector<PortInterface*>& pruned_ports() { return pruned_ports_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120
121 IceMode remote_ice_mode() const { return remote_ice_mode_; }
122
123 // DTLS methods.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200124 bool IsDtlsActive() const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125
126 // Default implementation.
deadbeefcbecd352015-09-23 11:50:27 -0700127 bool GetSslRole(rtc::SSLRole* role) const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128
deadbeefcbecd352015-09-23 11:50:27 -0700129 bool SetSslRole(rtc::SSLRole role) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000130
131 // Set up the ciphers to use for DTLS-SRTP.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800132 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133 return false;
134 }
135
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000136 // Find out which DTLS-SRTP cipher was negotiated.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800137 bool GetSrtpCryptoSuite(int* cipher) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000139 // Find out which DTLS cipher was negotiated.
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -0700140 bool GetSslCipherSuite(int* cipher) override { return false; }
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000141
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200142 // Returns null because the channel is not encrypted by default.
143 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override {
144 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145 }
146
jbauch555604a2016-04-26 03:13:22 -0700147 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate()
kwibergb4d01c42016-04-06 05:15:06 -0700148 const override {
149 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000150 }
151
152 // Allows key material to be extracted for external encryption.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200153 bool ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200154 const uint8_t* context,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200155 size_t context_len,
156 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200157 uint8_t* result,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200158 size_t result_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000159 return false;
160 }
161
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200162 bool SetLocalCertificate(
163 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000164 return false;
165 }
166
167 // Set DTLS Remote fingerprint. Must be after local identity set.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200168 bool SetRemoteFingerprint(const std::string& digest_alg,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200169 const uint8_t* digest,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200170 size_t digest_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000171 return false;
172 }
173
Honghai Zhanga74363c2016-07-28 18:06:15 -0700174 void PruneAllPorts();
Honghai Zhang049fbb12016-03-07 11:13:07 -0800175 int receiving_timeout() const { return config_.receiving_timeout; }
176 int check_receiving_interval() const { return check_receiving_interval_; }
Peter Thatcher54360512015-07-08 11:08:35 -0700177
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000178 // Helper method used only in unittest.
179 rtc::DiffServCodePoint DefaultDscpValue() const;
180
Peter Thatcher7351f462015-04-02 16:39:16 -0700181 // Public for unit tests.
182 Connection* FindNextPingableConnection();
guoweis36f01372016-03-02 18:02:40 -0800183 void MarkConnectionPinged(Connection* conn);
Peter Thatcher7351f462015-04-02 16:39:16 -0700184
honghaiz77d0d6e2015-10-27 11:34:45 -0700185 // Public for unit tests.
186 const std::vector<Connection*>& connections() const { return connections_; }
187
honghaiz9b669572015-11-04 12:07:44 -0800188 // Public for unit tests.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189 PortAllocatorSession* allocator_session() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700190 return allocator_sessions_.back().get();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191 }
192
honghaiz112fe432015-12-30 13:32:47 -0800193 // Public for unit tests.
194 const std::vector<RemoteCandidate>& remote_candidates() const {
195 return remote_candidates_;
196 }
197
honghaiz9b669572015-11-04 12:07:44 -0800198 private:
johan0fd22ef2016-09-29 01:19:20 -0700199 rtc::Thread* thread() const { return network_thread_; }
honghaiz9b669572015-11-04 12:07:44 -0800200 bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); }
201
honghaiza58ea782015-09-24 08:13:36 -0700202 // A transport channel is weak if the current best connection is either
203 // not receiving or not writable, or if there is no best connection at all.
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700204 bool weak() const;
Honghai Zhange05bcc22016-08-16 18:19:14 -0700205 // Returns true if it's possible to send packets on |connection|.
206 bool ReadyToSend(Connection* connection) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207 void UpdateConnectionStates();
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700208 void RequestSortAndStateUpdate();
209 // Start pinging if we haven't already started, and we now have a connection
210 // that's pingable.
211 void MaybeStartPinging();
deadbeef14f97f52016-06-22 17:14:15 -0700212
honghaiz9ad0db52016-07-14 19:30:28 -0700213 // The methods below return a positive value if |a| is preferable to |b|,
214 // a negative value if |b| is preferable, and 0 if they're equally preferable.
215 // If |receiving_unchanged_threshold| is set, then when |b| is receiving and
216 // |a| is not, returns a negative value only if |b| has been in receiving
217 // state and |a| has been in not receiving state since
218 // |receiving_unchanged_threshold| and sets
219 // |missed_receiving_unchanged_threshold| to true otherwise.
220 int CompareConnectionStates(
221 const cricket::Connection* a,
222 const cricket::Connection* b,
223 rtc::Optional<int64_t> receiving_unchanged_threshold,
224 bool* missed_receiving_unchanged_threshold) const;
deadbeef14f97f52016-06-22 17:14:15 -0700225 int CompareConnectionCandidates(const cricket::Connection* a,
226 const cricket::Connection* b) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700227 // Compares two connections based on the connection states
228 // (writable/receiving/connected), nomination states, last data received time,
229 // and static preferences. Does not include latency. Used by both sorting
230 // and ShouldSwitchSelectedConnection().
231 // Returns a positive value if |a| is better than |b|.
deadbeef14f97f52016-06-22 17:14:15 -0700232 int CompareConnections(const cricket::Connection* a,
honghaiz9ad0db52016-07-14 19:30:28 -0700233 const cricket::Connection* b,
234 rtc::Optional<int64_t> receiving_unchanged_threshold,
235 bool* missed_receiving_unchanged_threshold) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700236
deadbeef14f97f52016-06-22 17:14:15 -0700237 bool PresumedWritable(const cricket::Connection* conn) const;
238
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700239 void SortConnectionsAndUpdateState();
Honghai Zhang572b0942016-06-23 12:26:57 -0700240 void SwitchSelectedConnection(Connection* conn);
Honghai Zhang381b4212015-12-04 12:24:03 -0800241 void UpdateState();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000242 void HandleAllTimedOut();
honghaiz9b669572015-11-04 12:07:44 -0800243 void MaybeStopPortAllocatorSessions();
Honghai Zhang381b4212015-12-04 12:24:03 -0800244 TransportChannelState ComputeState() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000246 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700247 bool CreateConnections(const Candidate& remote_candidate,
248 PortInterface* origin_port);
249 bool CreateConnection(PortInterface* port,
250 const Candidate& remote_candidate,
251 PortInterface* origin_port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000252 bool FindConnection(cricket::Connection* connection) const;
253
Peter Boström0c4e06b2015-10-07 12:23:21 +0200254 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000255 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
256 void RememberRemoteCandidate(const Candidate& remote_candidate,
257 PortInterface* origin_port);
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700258 bool IsPingable(const Connection* conn, int64_t now) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700259 bool IsSelectedConnectionPingable(int64_t now);
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700260 int CalculateActiveWritablePingInterval(const Connection* conn,
261 int64_t now) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262 void PingConnection(Connection* conn);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700263 void AddAllocatorSession(std::unique_ptr<PortAllocatorSession> session);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000264 void AddConnection(Connection* connection);
265
266 void OnPortReady(PortAllocatorSession *session, PortInterface* port);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700267 void OnPortsPruned(PortAllocatorSession* session,
268 const std::vector<PortInterface*>& ports);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000269 void OnCandidatesReady(PortAllocatorSession *session,
270 const std::vector<Candidate>& candidates);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700271 void OnCandidatesRemoved(PortAllocatorSession* session,
272 const std::vector<Candidate>& candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000273 void OnCandidatesAllocationDone(PortAllocatorSession* session);
274 void OnUnknownAddress(PortInterface* port,
275 const rtc::SocketAddress& addr,
276 ProtocolType proto,
277 IceMessage* stun_msg,
278 const std::string& remote_username,
279 bool port_muxed);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700280
281 // When a port is destroyed, remove it from both lists |ports_|
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700282 // and |pruned_ports_|.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000283 void OnPortDestroyed(PortInterface* port);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700284 // When pruning a port, move it from |ports_| to |pruned_ports_|.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700285 // Returns true if the port is found and removed from |ports_|.
Honghai Zhanga74363c2016-07-28 18:06:15 -0700286 bool PrunePort(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000287 void OnRoleConflict(PortInterface* port);
288
289 void OnConnectionStateChange(Connection* connection);
290 void OnReadPacket(Connection *connection, const char *data, size_t len,
291 const rtc::PacketTime& packet_time);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100292 void OnSentPacket(const rtc::SentPacket& sent_packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000293 void OnReadyToSend(Connection* connection);
294 void OnConnectionDestroyed(Connection *connection);
295
honghaiz5a3acd82015-08-20 15:53:17 -0700296 void OnNominated(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000297
deadbeefcbecd352015-09-23 11:50:27 -0700298 void OnMessage(rtc::Message* pmsg) override;
honghaiza58ea782015-09-24 08:13:36 -0700299 void OnCheckAndPing();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700300 void OnRegatherOnFailedNetworks();
Peter Thatcher54360512015-07-08 11:08:35 -0700301
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700302 uint32_t GetNominationAttr(Connection* conn) const;
303 bool GetUseCandidateAttr(Connection* conn, NominationMode mode) const;
304
honghaiz9ad0db52016-07-14 19:30:28 -0700305 // Returns true if we should switch to the new connection.
306 // sets |missed_receiving_unchanged_threshold| to true if either
307 // the selected connection or the new connection missed its
308 // receiving-unchanged-threshold.
309 bool ShouldSwitchSelectedConnection(
310 Connection* new_connection,
311 bool* missed_receiving_unchanged_threshold) const;
312 // Returns true if the new_connection is selected for transmission.
313 bool MaybeSwitchSelectedConnection(Connection* new_connection,
314 const std::string& reason);
Honghai Zhang572b0942016-06-23 12:26:57 -0700315
honghaiz5a3acd82015-08-20 15:53:17 -0700316 void PruneConnections();
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700317 bool IsBackupConnection(const Connection* conn) const;
honghaiz5a3acd82015-08-20 15:53:17 -0700318
honghaiz34b11eb2016-03-16 08:55:44 -0700319 Connection* FindConnectionToPing(int64_t now);
320 Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now);
guoweis36f01372016-03-02 18:02:40 -0800321 // Between |conn1| and |conn2|, this function returns the one which should
322 // be pinged first.
323 Connection* SelectMostPingableConnection(Connection* conn1,
324 Connection* conn2);
325 // Select the connection which is Relay/Relay. If both of them are,
326 // UDP relay protocol takes precedence.
327 Connection* MostLikelyToWork(Connection* conn1, Connection* conn2);
328 // Compare the last_ping_sent time and return the one least recently pinged.
329 Connection* LeastRecentlyPinged(Connection* conn1, Connection* conn2);
330
honghaiza54a0802015-12-16 18:37:23 -0800331 // Returns the latest remote ICE parameters or nullptr if there are no remote
332 // ICE parameters yet.
333 IceParameters* remote_ice() {
334 return remote_ice_parameters_.empty() ? nullptr
335 : &remote_ice_parameters_.back();
336 }
honghaiz112fe432015-12-30 13:32:47 -0800337 // Returns the remote IceParameters and generation that match |ufrag|
338 // if found, and returns nullptr otherwise.
339 const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag,
340 uint32_t* generation);
honghaiza54a0802015-12-16 18:37:23 -0800341 // Returns the index of the latest remote ICE parameters, or 0 if no remote
342 // ICE parameters have been received.
343 uint32_t remote_ice_generation() {
344 return remote_ice_parameters_.empty()
345 ? 0
346 : static_cast<uint32_t>(remote_ice_parameters_.size() - 1);
347 }
348
deadbeefcbecd352015-09-23 11:50:27 -0700349 PortAllocator* allocator_;
johan0fd22ef2016-09-29 01:19:20 -0700350 rtc::Thread* network_thread_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000351 bool incoming_only_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000352 int error_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700353 std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_;
deadbeefdfc42442016-06-21 14:19:48 -0700354 // |ports_| contains ports that are used to form new connections when
355 // new remote candidates are added.
356 std::vector<PortInterface*> ports_;
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700357 // |pruned_ports_| contains ports that have been removed from |ports_| and
deadbeefdfc42442016-06-21 14:19:48 -0700358 // are not being used to form new connections, but that aren't yet destroyed.
359 // They may have existing connections, and they still fire signals such as
360 // SignalUnknownAddress.
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700361 std::vector<PortInterface*> pruned_ports_;
guoweis36f01372016-03-02 18:02:40 -0800362
363 // |connections_| is a sorted list with the first one always be the
Honghai Zhang572b0942016-06-23 12:26:57 -0700364 // |selected_connection_| when it's not nullptr. The combination of
guoweis36f01372016-03-02 18:02:40 -0800365 // |pinged_connections_| and |unpinged_connections_| has the same
366 // connections as |connections_|. These 2 sets maintain whether a
367 // connection should be pinged next or not.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000368 std::vector<Connection *> connections_;
guoweis36f01372016-03-02 18:02:40 -0800369 std::set<Connection*> pinged_connections_;
370 std::set<Connection*> unpinged_connections_;
371
Honghai Zhang572b0942016-06-23 12:26:57 -0700372 Connection* selected_connection_ = nullptr;
guoweis36f01372016-03-02 18:02:40 -0800373
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000374 std::vector<RemoteCandidate> remote_candidates_;
375 bool sort_dirty_; // indicates whether another sort is needed right now
deadbeefcbecd352015-09-23 11:50:27 -0700376 bool had_connection_ = false; // if connections_ has ever been nonempty
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000377 typedef std::map<rtc::Socket::Option, int> OptionMap;
378 OptionMap options_;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700379 IceParameters ice_parameters_;
honghaiza54a0802015-12-16 18:37:23 -0800380 std::vector<IceParameters> remote_ice_parameters_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000381 IceMode remote_ice_mode_;
382 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200383 uint64_t tiebreaker_;
deadbeefcbecd352015-09-23 11:50:27 -0700384 IceGatheringState gathering_state_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000385
Honghai Zhang049fbb12016-03-07 11:13:07 -0800386 int check_receiving_interval_;
honghaiz34b11eb2016-03-16 08:55:44 -0700387 int64_t last_ping_sent_ms_ = 0;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800388 int weak_ping_interval_ = WEAK_PING_INTERVAL;
Honghai Zhang381b4212015-12-04 12:24:03 -0800389 TransportChannelState state_ = TransportChannelState::STATE_INIT;
guoweis36f01372016-03-02 18:02:40 -0800390 IceConfig config_;
Honghai Zhang52dce732016-03-31 12:37:31 -0700391 int last_sent_packet_id_ = -1; // -1 indicates no packet was sent before.
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700392 bool started_pinging_ = false;
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700393 // The value put in the "nomination" attribute for the next nominated
394 // connection. A zero-value indicates the connection will not be nominated.
395 uint32_t nomination_ = 0;
Peter Thatcher54360512015-07-08 11:08:35 -0700396
henrikg3c089d72015-09-16 05:37:44 -0700397 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000398};
399
400} // namespace cricket
401
402#endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_