blob: 692b09843bf7dbf470cc41a3972bd7a8f8fe03cf [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
honghaiza54a0802015-12-16 18:37:23 -080046struct IceParameters {
47 std::string ufrag;
48 std::string pwd;
49 IceParameters(const std::string& ice_ufrag, const std::string& ice_pwd)
50 : ufrag(ice_ufrag), pwd(ice_pwd) {}
51
52 bool operator==(const IceParameters& other) {
53 return ufrag == other.ufrag && pwd == other.pwd;
54 }
55 bool operator!=(const IceParameters& other) { return !(*this == other); }
56};
57
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000058// Adds the port on which the candidate originated.
59class RemoteCandidate : public Candidate {
60 public:
61 RemoteCandidate(const Candidate& c, PortInterface* origin_port)
62 : Candidate(c), origin_port_(origin_port) {}
63
64 PortInterface* origin_port() { return origin_port_; }
65
66 private:
67 PortInterface* origin_port_;
68};
69
70// P2PTransportChannel manages the candidates and connection process to keep
71// two P2P clients connected to each other.
72class P2PTransportChannel : public TransportChannelImpl,
73 public rtc::MessageHandler {
74 public:
deadbeefcbecd352015-09-23 11:50:27 -070075 P2PTransportChannel(const std::string& transport_name,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076 int component,
mikescarlettb9dd7c52016-02-19 20:43:45 -080077 PortAllocator* allocator);
78 // TODO(mikescarlett): Deprecated. Remove when Chromium's
79 // IceTransportChannel does not depend on this.
80 P2PTransportChannel(const std::string& transport_name,
81 int component,
guidou74db7772016-02-18 01:57:49 -080082 P2PTransport* transport,
deadbeefcbecd352015-09-23 11:50:27 -070083 PortAllocator* allocator);
84 virtual ~P2PTransportChannel();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000085
86 // From TransportChannelImpl:
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020087 TransportChannelState GetState() const override;
88 void SetIceRole(IceRole role) override;
89 IceRole GetIceRole() const override { return ice_role_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +020090 void SetIceTiebreaker(uint64_t tiebreaker) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +020091 void SetIceCredentials(const std::string& ice_ufrag,
92 const std::string& ice_pwd) override;
93 void SetRemoteIceCredentials(const std::string& ice_ufrag,
94 const std::string& ice_pwd) override;
95 void SetRemoteIceMode(IceMode mode) override;
deadbeef886815b2016-06-29 15:21:04 -070096 // TODO(deadbeef): Deprecated. Remove when Chromium's
97 // IceTransportChannel does not depend on this.
98 void Connect() {}
deadbeefcbecd352015-09-23 11:50:27 -070099 void MaybeStartGathering() override;
100 IceGatheringState gathering_state() const override {
101 return gathering_state_;
102 }
103 void AddRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700104 void RemoveRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800105 // Sets the parameters in IceConfig. We do not set them blindly. Instead, we
106 // only update the parameter if it is considered set in |config|. For example,
107 // a negative value of receiving_timeout will be considered "not set" and we
108 // will not use it to update the respective parameter in |config_|.
deadbeef14f97f52016-06-22 17:14:15 -0700109 // TODO(deadbeef): Use rtc::Optional instead of negative values.
honghaiz1f429e32015-09-28 07:57:34 -0700110 void SetIceConfig(const IceConfig& config) override;
guoweis36f01372016-03-02 18:02:40 -0800111 const IceConfig& config() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112
113 // From TransportChannel:
deadbeefcbecd352015-09-23 11:50:27 -0700114 int SendPacket(const char* data,
115 size_t len,
116 const rtc::PacketOptions& options,
117 int flags) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200118 int SetOption(rtc::Socket::Option opt, int value) override;
119 bool GetOption(rtc::Socket::Option opt, int* value) override;
120 int GetError() override { return error_; }
121 bool GetStats(std::vector<ConnectionInfo>* stats) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122
Honghai Zhang8cd7f222016-06-23 13:44:34 -0700123 // TODO(honghaiz): Remove this method once the reference of it in
124 // Chromoting is removed.
125 const Connection* best_connection() const { return selected_connection_; }
126
Honghai Zhang572b0942016-06-23 12:26:57 -0700127 const Connection* selected_connection() const { return selected_connection_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128 void set_incoming_only(bool value) { incoming_only_ = value; }
129
130 // Note: This is only for testing purpose.
131 // |ports_| should not be changed from outside.
Peter Thatcher1fe120a2015-06-10 11:33:17 -0700132 const std::vector<PortInterface*>& ports() { return ports_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133
134 IceMode remote_ice_mode() const { return remote_ice_mode_; }
135
136 // DTLS methods.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200137 bool IsDtlsActive() const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138
139 // Default implementation.
deadbeefcbecd352015-09-23 11:50:27 -0700140 bool GetSslRole(rtc::SSLRole* role) const override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000141
deadbeefcbecd352015-09-23 11:50:27 -0700142 bool SetSslRole(rtc::SSLRole role) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143
144 // Set up the ciphers to use for DTLS-SRTP.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800145 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000146 return false;
147 }
148
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000149 // Find out which DTLS-SRTP cipher was negotiated.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800150 bool GetSrtpCryptoSuite(int* cipher) override { return false; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000152 // Find out which DTLS cipher was negotiated.
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -0700153 bool GetSslCipherSuite(int* cipher) override { return false; }
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000154
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200155 // Returns null because the channel is not encrypted by default.
156 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override {
157 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000158 }
159
jbauch555604a2016-04-26 03:13:22 -0700160 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate()
kwibergb4d01c42016-04-06 05:15:06 -0700161 const override {
162 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 }
164
165 // Allows key material to be extracted for external encryption.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200166 bool ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200167 const uint8_t* context,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200168 size_t context_len,
169 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200170 uint8_t* result,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200171 size_t result_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000172 return false;
173 }
174
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200175 bool SetLocalCertificate(
176 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177 return false;
178 }
179
180 // Set DTLS Remote fingerprint. Must be after local identity set.
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200181 bool SetRemoteFingerprint(const std::string& digest_alg,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200182 const uint8_t* digest,
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200183 size_t digest_len) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184 return false;
185 }
186
Honghai Zhang049fbb12016-03-07 11:13:07 -0800187 int receiving_timeout() const { return config_.receiving_timeout; }
188 int check_receiving_interval() const { return check_receiving_interval_; }
Peter Thatcher54360512015-07-08 11:08:35 -0700189
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190 // Helper method used only in unittest.
191 rtc::DiffServCodePoint DefaultDscpValue() const;
192
Peter Thatcher7351f462015-04-02 16:39:16 -0700193 // Public for unit tests.
194 Connection* FindNextPingableConnection();
guoweis36f01372016-03-02 18:02:40 -0800195 void MarkConnectionPinged(Connection* conn);
Peter Thatcher7351f462015-04-02 16:39:16 -0700196
honghaiz77d0d6e2015-10-27 11:34:45 -0700197 // Public for unit tests.
198 const std::vector<Connection*>& connections() const { return connections_; }
199
honghaiz9b669572015-11-04 12:07:44 -0800200 // Public for unit tests.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 PortAllocatorSession* allocator_session() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700202 return allocator_sessions_.back().get();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000203 }
204
honghaiz112fe432015-12-30 13:32:47 -0800205 // Public for unit tests.
206 const std::vector<RemoteCandidate>& remote_candidates() const {
207 return remote_candidates_;
208 }
209
honghaiz9b669572015-11-04 12:07:44 -0800210 private:
honghaiz9ad0db52016-07-14 19:30:28 -0700211 rtc::Thread* thread() const { return worker_thread_; }
honghaiz9b669572015-11-04 12:07:44 -0800212 bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); }
213
honghaiza58ea782015-09-24 08:13:36 -0700214 // A transport channel is weak if the current best connection is either
215 // not receiving or not writable, or if there is no best connection at all.
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700216 bool weak() const;
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700217 // Returns true if it's possible to send packets on this channel.
218 bool ReadyToSend() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000219 void UpdateConnectionStates();
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700220 void RequestSortAndStateUpdate();
221 // Start pinging if we haven't already started, and we now have a connection
222 // that's pingable.
223 void MaybeStartPinging();
deadbeef14f97f52016-06-22 17:14:15 -0700224
honghaiz9ad0db52016-07-14 19:30:28 -0700225 // The methods below return a positive value if |a| is preferable to |b|,
226 // a negative value if |b| is preferable, and 0 if they're equally preferable.
227 // If |receiving_unchanged_threshold| is set, then when |b| is receiving and
228 // |a| is not, returns a negative value only if |b| has been in receiving
229 // state and |a| has been in not receiving state since
230 // |receiving_unchanged_threshold| and sets
231 // |missed_receiving_unchanged_threshold| to true otherwise.
232 int CompareConnectionStates(
233 const cricket::Connection* a,
234 const cricket::Connection* b,
235 rtc::Optional<int64_t> receiving_unchanged_threshold,
236 bool* missed_receiving_unchanged_threshold) const;
deadbeef14f97f52016-06-22 17:14:15 -0700237 int CompareConnectionCandidates(const cricket::Connection* a,
238 const cricket::Connection* b) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700239 // Compares two connections based on the connection states
240 // (writable/receiving/connected), nomination states, last data received time,
241 // and static preferences. Does not include latency. Used by both sorting
242 // and ShouldSwitchSelectedConnection().
243 // Returns a positive value if |a| is better than |b|.
deadbeef14f97f52016-06-22 17:14:15 -0700244 int CompareConnections(const cricket::Connection* a,
honghaiz9ad0db52016-07-14 19:30:28 -0700245 const cricket::Connection* b,
246 rtc::Optional<int64_t> receiving_unchanged_threshold,
247 bool* missed_receiving_unchanged_threshold) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700248
deadbeef14f97f52016-06-22 17:14:15 -0700249 bool PresumedWritable(const cricket::Connection* conn) const;
250
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700251 void SortConnectionsAndUpdateState();
Honghai Zhang572b0942016-06-23 12:26:57 -0700252 void SwitchSelectedConnection(Connection* conn);
Honghai Zhang381b4212015-12-04 12:24:03 -0800253 void UpdateState();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000254 void HandleAllTimedOut();
honghaiz9b669572015-11-04 12:07:44 -0800255 void MaybeStopPortAllocatorSessions();
Honghai Zhang381b4212015-12-04 12:24:03 -0800256 TransportChannelState ComputeState() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000257
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000258 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700259 bool CreateConnections(const Candidate& remote_candidate,
260 PortInterface* origin_port);
261 bool CreateConnection(PortInterface* port,
262 const Candidate& remote_candidate,
263 PortInterface* origin_port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000264 bool FindConnection(cricket::Connection* connection) const;
265
Peter Boström0c4e06b2015-10-07 12:23:21 +0200266 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000267 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
268 void RememberRemoteCandidate(const Candidate& remote_candidate,
269 PortInterface* origin_port);
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700270 bool IsPingable(const Connection* conn, int64_t now) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700271 bool IsSelectedConnectionPingable(int64_t now);
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700272 int CalculateActiveWritablePingInterval(const Connection* conn,
273 int64_t now) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000274 void PingConnection(Connection* conn);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700275 void AddAllocatorSession(std::unique_ptr<PortAllocatorSession> session);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000276 void AddConnection(Connection* connection);
277
278 void OnPortReady(PortAllocatorSession *session, PortInterface* port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700279 // TODO(honghaiz): Merge the two methods OnPortsRemoved and OnPortPruned but
280 // still log the reason of removing.
281 void OnPortsRemoved(PortAllocatorSession* session,
282 const std::vector<PortInterface*>& ports);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700283 void OnPortPruned(PortAllocatorSession* session, PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000284 void OnCandidatesReady(PortAllocatorSession *session,
285 const std::vector<Candidate>& candidates);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700286 void OnCandidatesRemoved(PortAllocatorSession* session,
287 const std::vector<Candidate>& candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000288 void OnCandidatesAllocationDone(PortAllocatorSession* session);
289 void OnUnknownAddress(PortInterface* port,
290 const rtc::SocketAddress& addr,
291 ProtocolType proto,
292 IceMessage* stun_msg,
293 const std::string& remote_username,
294 bool port_muxed);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700295
296 // When a port is destroyed, remove it from both lists |ports_|
297 // and |removed_ports_|.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000298 void OnPortDestroyed(PortInterface* port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700299 // When removing a port, move it from |ports_| to |removed_ports_|.
300 // Returns true if the port is found and removed from |ports_|.
301 bool RemovePort(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000302 void OnRoleConflict(PortInterface* port);
303
304 void OnConnectionStateChange(Connection* connection);
305 void OnReadPacket(Connection *connection, const char *data, size_t len,
306 const rtc::PacketTime& packet_time);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100307 void OnSentPacket(const rtc::SentPacket& sent_packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000308 void OnReadyToSend(Connection* connection);
309 void OnConnectionDestroyed(Connection *connection);
310
honghaiz5a3acd82015-08-20 15:53:17 -0700311 void OnNominated(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000312
deadbeefcbecd352015-09-23 11:50:27 -0700313 void OnMessage(rtc::Message* pmsg) override;
honghaiza58ea782015-09-24 08:13:36 -0700314 void OnCheckAndPing();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700315 void OnRegatherOnFailedNetworks();
Peter Thatcher54360512015-07-08 11:08:35 -0700316
honghaiz9ad0db52016-07-14 19:30:28 -0700317 // Returns true if we should switch to the new connection.
318 // sets |missed_receiving_unchanged_threshold| to true if either
319 // the selected connection or the new connection missed its
320 // receiving-unchanged-threshold.
321 bool ShouldSwitchSelectedConnection(
322 Connection* new_connection,
323 bool* missed_receiving_unchanged_threshold) const;
324 // Returns true if the new_connection is selected for transmission.
325 bool MaybeSwitchSelectedConnection(Connection* new_connection,
326 const std::string& reason);
Honghai Zhang572b0942016-06-23 12:26:57 -0700327
honghaiz5a3acd82015-08-20 15:53:17 -0700328 void PruneConnections();
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700329 bool IsBackupConnection(const Connection* conn) const;
honghaiz5a3acd82015-08-20 15:53:17 -0700330
honghaiz34b11eb2016-03-16 08:55:44 -0700331 Connection* FindConnectionToPing(int64_t now);
332 Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now);
guoweis36f01372016-03-02 18:02:40 -0800333 // Between |conn1| and |conn2|, this function returns the one which should
334 // be pinged first.
335 Connection* SelectMostPingableConnection(Connection* conn1,
336 Connection* conn2);
337 // Select the connection which is Relay/Relay. If both of them are,
338 // UDP relay protocol takes precedence.
339 Connection* MostLikelyToWork(Connection* conn1, Connection* conn2);
340 // Compare the last_ping_sent time and return the one least recently pinged.
341 Connection* LeastRecentlyPinged(Connection* conn1, Connection* conn2);
342
honghaiza54a0802015-12-16 18:37:23 -0800343 // Returns the latest remote ICE parameters or nullptr if there are no remote
344 // ICE parameters yet.
345 IceParameters* remote_ice() {
346 return remote_ice_parameters_.empty() ? nullptr
347 : &remote_ice_parameters_.back();
348 }
honghaiz112fe432015-12-30 13:32:47 -0800349 // Returns the remote IceParameters and generation that match |ufrag|
350 // if found, and returns nullptr otherwise.
351 const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag,
352 uint32_t* generation);
honghaiza54a0802015-12-16 18:37:23 -0800353 // Returns the index of the latest remote ICE parameters, or 0 if no remote
354 // ICE parameters have been received.
355 uint32_t remote_ice_generation() {
356 return remote_ice_parameters_.empty()
357 ? 0
358 : static_cast<uint32_t>(remote_ice_parameters_.size() - 1);
359 }
360
deadbeefcbecd352015-09-23 11:50:27 -0700361 PortAllocator* allocator_;
362 rtc::Thread* worker_thread_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000363 bool incoming_only_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000364 int error_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700365 std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_;
deadbeefdfc42442016-06-21 14:19:48 -0700366 // |ports_| contains ports that are used to form new connections when
367 // new remote candidates are added.
368 std::vector<PortInterface*> ports_;
369 // |removed_ports_| contains ports that have been removed from |ports_| and
370 // are not being used to form new connections, but that aren't yet destroyed.
371 // They may have existing connections, and they still fire signals such as
372 // SignalUnknownAddress.
373 std::vector<PortInterface*> removed_ports_;
guoweis36f01372016-03-02 18:02:40 -0800374
375 // |connections_| is a sorted list with the first one always be the
Honghai Zhang572b0942016-06-23 12:26:57 -0700376 // |selected_connection_| when it's not nullptr. The combination of
guoweis36f01372016-03-02 18:02:40 -0800377 // |pinged_connections_| and |unpinged_connections_| has the same
378 // connections as |connections_|. These 2 sets maintain whether a
379 // connection should be pinged next or not.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000380 std::vector<Connection *> connections_;
guoweis36f01372016-03-02 18:02:40 -0800381 std::set<Connection*> pinged_connections_;
382 std::set<Connection*> unpinged_connections_;
383
Honghai Zhang572b0942016-06-23 12:26:57 -0700384 Connection* selected_connection_ = nullptr;
guoweis36f01372016-03-02 18:02:40 -0800385
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000386 std::vector<RemoteCandidate> remote_candidates_;
387 bool sort_dirty_; // indicates whether another sort is needed right now
deadbeefcbecd352015-09-23 11:50:27 -0700388 bool had_connection_ = false; // if connections_ has ever been nonempty
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000389 typedef std::map<rtc::Socket::Option, int> OptionMap;
390 OptionMap options_;
391 std::string ice_ufrag_;
392 std::string ice_pwd_;
honghaiza54a0802015-12-16 18:37:23 -0800393 std::vector<IceParameters> remote_ice_parameters_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394 IceMode remote_ice_mode_;
395 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200396 uint64_t tiebreaker_;
deadbeefcbecd352015-09-23 11:50:27 -0700397 IceGatheringState gathering_state_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000398
Honghai Zhang049fbb12016-03-07 11:13:07 -0800399 int check_receiving_interval_;
honghaiz34b11eb2016-03-16 08:55:44 -0700400 int64_t last_ping_sent_ms_ = 0;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800401 int weak_ping_interval_ = WEAK_PING_INTERVAL;
Honghai Zhang381b4212015-12-04 12:24:03 -0800402 TransportChannelState state_ = TransportChannelState::STATE_INIT;
guoweis36f01372016-03-02 18:02:40 -0800403 IceConfig config_;
Honghai Zhang52dce732016-03-31 12:37:31 -0700404 int last_sent_packet_id_ = -1; // -1 indicates no packet was sent before.
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700405 bool started_pinging_ = false;
Peter Thatcher54360512015-07-08 11:08:35 -0700406
henrikg3c089d72015-09-16 05:37:44 -0700407 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000408};
409
410} // namespace cricket
411
412#endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_