OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/p2p/base/p2ptransportchannel.h" | 11 #include "webrtc/p2p/base/p2ptransportchannel.h" |
12 | 12 |
13 #include <set> | 13 #include <set> |
14 #include "webrtc/p2p/base/common.h" | 14 #include "webrtc/p2p/base/common.h" |
15 #include "webrtc/p2p/base/relayport.h" // For RELAY_PORT_TYPE. | 15 #include "webrtc/p2p/base/relayport.h" // For RELAY_PORT_TYPE. |
16 #include "webrtc/p2p/base/stunport.h" // For STUN_PORT_TYPE. | 16 #include "webrtc/p2p/base/stunport.h" // For STUN_PORT_TYPE. |
17 #include "webrtc/base/common.h" | 17 #include "webrtc/base/common.h" |
18 #include "webrtc/base/crc32.h" | 18 #include "webrtc/base/crc32.h" |
19 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
20 #include "webrtc/base/stringencode.h" | 20 #include "webrtc/base/stringencode.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // messages for queuing up work for ourselves | 24 // messages for queuing up work for ourselves |
25 enum { | 25 enum { MSG_SORT = 1, MSG_CHECK_AND_PING }; |
pthatcher1
2015/09/17 05:58:42
Why did you combine these? I think it made sense
honghaiz3
2015/09/17 19:47:56
There are two reasons.
1. When you do ping, you a
| |
26 MSG_SORT = 1, | |
27 MSG_PING, | |
28 MSG_CHECK_RECEIVING | |
29 }; | |
30 | 26 |
31 // When the socket is unwritable, we will use 10 Kbps (ignoring IP+UDP headers) | 27 // When the socket is unwritable, we will use 10 Kbps (ignoring IP+UDP headers) |
32 // for pinging. When the socket is writable, we will use only 1 Kbps because | 28 // for pinging. When the socket is writable, we will use only 1 Kbps because |
33 // we don't want to degrade the quality on a modem. These numbers should work | 29 // we don't want to degrade the quality on a modem. These numbers should work |
34 // well on a 28.8K modem, which is the slowest connection on which the voice | 30 // well on a 28.8K modem, which is the slowest connection on which the voice |
35 // quality is reasonable at all. | 31 // quality is reasonable at all. |
36 static const uint32 PING_PACKET_SIZE = 60 * 8; | 32 static const uint32 PING_PACKET_SIZE = 60 * 8; |
37 static const uint32 WRITABLE_DELAY = 1000 * PING_PACKET_SIZE / 1000; // 480ms | 33 static const uint32 LONG_PING_DELAY = 1000 * PING_PACKET_SIZE / 1000; // 480ms |
38 static const uint32 UNWRITABLE_DELAY = 1000 * PING_PACKET_SIZE / 10000; // 50ms | 34 static const uint32 SHORT_PING_DELAY = 1000 * PING_PACKET_SIZE / 10000; // 50ms |
pthatcher1
2015/09/17 05:58:41
I think a good name for these would be WEAK_CONNEC
honghaiz3
2015/09/17 19:47:55
Done.
| |
39 | 35 |
40 // If there is a current writable connection, then we will also try hard to | 36 // If there is a current writable connection, then we will also try hard to |
41 // make sure it is pinged at this rate. | 37 // make sure it is pinged at this rate. |
42 static const uint32 MAX_CURRENT_WRITABLE_DELAY = 900; // 2*WRITABLE_DELAY - bit | 38 // 2 * LONG_PING_DELAY - bit |
39 static const uint32 MAX_CURRENT_WRITABLE_DELAY = 900; | |
43 | 40 |
44 static const int MIN_CHECK_RECEIVING_DELAY = 50; // ms | 41 static const int MIN_CHECK_RECEIVING_DELAY = 50; // ms |
45 | 42 |
46 // The minimum improvement in RTT that justifies a switch. | 43 // The minimum improvement in RTT that justifies a switch. |
47 static const double kMinImprovement = 10; | 44 static const double kMinImprovement = 10; |
48 | 45 |
49 cricket::PortInterface::CandidateOrigin GetOrigin(cricket::PortInterface* port, | 46 cricket::PortInterface::CandidateOrigin GetOrigin(cricket::PortInterface* port, |
50 cricket::PortInterface* origin_port) { | 47 cricket::PortInterface* origin_port) { |
51 if (!origin_port) | 48 if (!origin_port) |
52 return cricket::PortInterface::ORIGIN_MESSAGE; | 49 return cricket::PortInterface::ORIGIN_MESSAGE; |
(...skipping 12 matching lines...) Expand all Loading... | |
65 if (a->priority() < b->priority()) | 62 if (a->priority() < b->priority()) |
66 return -1; | 63 return -1; |
67 | 64 |
68 // If we're still tied at this point, prefer a younger generation. | 65 // If we're still tied at this point, prefer a younger generation. |
69 return (a->remote_candidate().generation() + a->port()->generation()) - | 66 return (a->remote_candidate().generation() + a->port()->generation()) - |
70 (b->remote_candidate().generation() + b->port()->generation()); | 67 (b->remote_candidate().generation() + b->port()->generation()); |
71 } | 68 } |
72 | 69 |
73 // Compare two connections based on their connected state, writability and | 70 // Compare two connections based on their connected state, writability and |
74 // static preferences. | 71 // static preferences. |
75 int CompareConnections(cricket::Connection *a, cricket::Connection *b) { | 72 int CompareConnectionStates(cricket::Connection* a, cricket::Connection* b) { |
73 // When the current best connection is not receiving but a backup connection | |
74 // is receiving, the backup connection has a higher priority regardless | |
75 // its writable state. | |
pthatcher1
2015/09/17 05:58:42
It doesn't need to just be "backup". We could sim
honghaiz3
2015/09/17 19:47:55
Done.
| |
76 if (a->receiving() && !b->receiving()) | |
77 return 1; | |
78 if (!a->receiving() && b->receiving()) | |
79 return -1; | |
80 | |
76 // Sort based on write-state. Better states have lower values. | 81 // Sort based on write-state. Better states have lower values. |
77 if (a->write_state() < b->write_state()) | 82 if (a->write_state() < b->write_state()) |
78 return 1; | 83 return 1; |
79 if (a->write_state() > b->write_state()) | 84 if (a->write_state() > b->write_state()) |
80 return -1; | 85 return -1; |
pthatcher1
2015/09/17 05:58:42
So what's better? receiving-but-non-writable or w
| |
81 | 86 |
82 // WARNING: Some complexity here about TCP reconnecting. | 87 // WARNING: Some complexity here about TCP reconnecting. |
83 // When a TCP connection fails because of a TCP socket disconnecting, the | 88 // When a TCP connection fails because of a TCP socket disconnecting, the |
84 // active side of the connection will attempt to reconnect for 5 seconds while | 89 // active side of the connection will attempt to reconnect for 5 seconds while |
85 // pretending to be writable (the connection is not set to the unwritable | 90 // pretending to be writable (the connection is not set to the unwritable |
86 // state). On the passive side, the connection also remains writable even | 91 // state). On the passive side, the connection also remains writable even |
87 // though it is disconnected, and a new connection is created when the active | 92 // though it is disconnected, and a new connection is created when the active |
88 // side connects. At that point, there are two TCP connections on the passive | 93 // side connects. At that point, there are two TCP connections on the passive |
89 // side: 1. the old, disconnected one that is pretending to be writable, and | 94 // side: 1. the old, disconnected one that is pretending to be writable, and |
90 // 2. the new, connected one that is maybe not yet writable. For purposes of | 95 // 2. the new, connected one that is maybe not yet writable. For purposes of |
(...skipping 12 matching lines...) Expand all Loading... | |
103 // the new connection, when it becomes writable, should have higher priority. | 108 // the new connection, when it becomes writable, should have higher priority. |
104 if (a->write_state() == cricket::Connection::STATE_WRITABLE && | 109 if (a->write_state() == cricket::Connection::STATE_WRITABLE && |
105 b->write_state() == cricket::Connection::STATE_WRITABLE) { | 110 b->write_state() == cricket::Connection::STATE_WRITABLE) { |
106 if (a->connected() && !b->connected()) { | 111 if (a->connected() && !b->connected()) { |
107 return 1; | 112 return 1; |
108 } | 113 } |
109 if (!a->connected() && b->connected()) { | 114 if (!a->connected() && b->connected()) { |
110 return -1; | 115 return -1; |
111 } | 116 } |
112 } | 117 } |
118 return 0; | |
119 } | |
113 | 120 |
121 int CompareConnections(cricket::Connection* a, cricket::Connection* b) { | |
122 int state_cmp = CompareConnectionStates(a, b); | |
123 if (state_cmp != 0) { | |
124 return state_cmp; | |
125 } | |
114 // Compare the candidate information. | 126 // Compare the candidate information. |
115 return CompareConnectionCandidates(a, b); | 127 return CompareConnectionCandidates(a, b); |
116 } | 128 } |
117 | 129 |
118 // Wraps the comparison connection into a less than operator that puts higher | 130 // Wraps the comparison connection into a less than operator that puts higher |
119 // priority writable connections first. | 131 // priority writable connections first. |
120 class ConnectionCompare { | 132 class ConnectionCompare { |
121 public: | 133 public: |
122 bool operator()(const cricket::Connection *ca, | 134 bool operator()(const cricket::Connection *ca, |
123 const cricket::Connection *cb) { | 135 const cricket::Connection *cb) { |
(...skipping 18 matching lines...) Expand all Loading... | |
142 // efficiency by being used bidirectionally, as opposed to two separate | 154 // efficiency by being used bidirectionally, as opposed to two separate |
143 // unidirectional streams. This test should probably occur before | 155 // unidirectional streams. This test should probably occur before |
144 // comparison of local prefs (assuming combined prefs are the same). We | 156 // comparison of local prefs (assuming combined prefs are the same). We |
145 // need to be careful though, not to bounce back and forth with both sides | 157 // need to be careful though, not to bounce back and forth with both sides |
146 // trying to rendevous with the other. | 158 // trying to rendevous with the other. |
147 } | 159 } |
148 }; | 160 }; |
149 | 161 |
150 // Determines whether we should switch between two connections, based first on | 162 // Determines whether we should switch between two connections, based first on |
151 // static preferences and then (if those are equal) on latency estimates. | 163 // static preferences and then (if those are equal) on latency estimates. |
152 bool ShouldSwitch(cricket::Connection* a_conn, cricket::Connection* b_conn) { | 164 bool ShouldSwitch(cricket::Connection* a_conn, |
165 cricket::Connection* b_conn, | |
166 cricket::IceRole ice_role) { | |
153 if (a_conn == b_conn) | 167 if (a_conn == b_conn) |
154 return false; | 168 return false; |
155 | 169 |
156 if (!a_conn || !b_conn) // don't think the latter should happen | 170 if (!a_conn || !b_conn) // don't think the latter should happen |
157 return true; | 171 return true; |
158 | 172 |
159 int prefs_cmp = CompareConnections(a_conn, b_conn); | 173 // If the RECEIVING/WRITE/CONNECT states are different, we should switch |
160 if (prefs_cmp < 0) | 174 // regardless of the nominate state. Otherwise, do not switch if |a_conn| is |
pthatcher1
2015/09/17 05:58:42
nominate => nominated
honghaiz3
2015/09/17 19:47:56
Done.
| |
161 return true; | 175 // nominated but |b_conn| is not. |
162 if (prefs_cmp > 0) | 176 int state_cmp = CompareConnectionStates(a_conn, b_conn); |
177 if (state_cmp != 0) { | |
178 return state_cmp < 0; | |
179 } | |
180 if (ice_role == cricket::ICEROLE_CONTROLLED && | |
181 a_conn->nominated() > b_conn->nominated()) { | |
182 LOG(LS_VERBOSE) << "Controlled side did not switch due to nominated status"; | |
163 return false; | 183 return false; |
184 } | |
185 | |
186 int prefs_cmp = CompareConnectionCandidates(a_conn, b_conn); | |
187 if (prefs_cmp != 0) | |
188 return prefs_cmp < 0; | |
164 | 189 |
165 return b_conn->rtt() <= a_conn->rtt() + kMinImprovement; | 190 return b_conn->rtt() <= a_conn->rtt() + kMinImprovement; |
166 } | 191 } |
167 | 192 |
168 } // unnamed namespace | 193 } // unnamed namespace |
169 | 194 |
170 namespace cricket { | 195 namespace cricket { |
171 | 196 |
172 P2PTransportChannel::P2PTransportChannel(const std::string& content_name, | 197 P2PTransportChannel::P2PTransportChannel(const std::string& content_name, |
173 int component, | 198 int component, |
174 P2PTransport* transport, | 199 P2PTransport* transport, |
175 PortAllocator *allocator) : | 200 PortAllocator* allocator) |
176 TransportChannelImpl(content_name, component), | 201 : TransportChannelImpl(content_name, component), |
177 transport_(transport), | 202 transport_(transport), |
178 allocator_(allocator), | 203 allocator_(allocator), |
179 worker_thread_(rtc::Thread::Current()), | 204 worker_thread_(rtc::Thread::Current()), |
180 incoming_only_(false), | 205 incoming_only_(false), |
181 waiting_for_signaling_(false), | 206 waiting_for_signaling_(false), |
182 error_(0), | 207 error_(0), |
183 best_connection_(NULL), | 208 best_connection_(nullptr), |
184 pending_best_connection_(NULL), | 209 pending_best_connection_(nullptr), |
185 sort_dirty_(false), | 210 next_connection_to_ping_(nullptr), |
186 was_writable_(false), | 211 sort_dirty_(false), |
187 remote_ice_mode_(ICEMODE_FULL), | 212 was_writable_(false), |
188 ice_role_(ICEROLE_UNKNOWN), | 213 remote_ice_mode_(ICEMODE_FULL), |
189 tiebreaker_(0), | 214 ice_role_(ICEROLE_UNKNOWN), |
190 remote_candidate_generation_(0), | 215 tiebreaker_(0), |
191 check_receiving_delay_(MIN_CHECK_RECEIVING_DELAY * 5), | 216 remote_candidate_generation_(0), |
192 receiving_timeout_(MIN_CHECK_RECEIVING_DELAY * 50) { | 217 check_receiving_delay_(MIN_CHECK_RECEIVING_DELAY * 5), |
193 } | 218 receiving_timeout_(MIN_CHECK_RECEIVING_DELAY * 50), |
219 ping_delay_(SHORT_PING_DELAY), | |
220 last_ping_sent_(0) {} | |
194 | 221 |
195 P2PTransportChannel::~P2PTransportChannel() { | 222 P2PTransportChannel::~P2PTransportChannel() { |
196 ASSERT(worker_thread_ == rtc::Thread::Current()); | 223 ASSERT(worker_thread_ == rtc::Thread::Current()); |
197 | 224 |
198 for (uint32 i = 0; i < allocator_sessions_.size(); ++i) | 225 for (uint32 i = 0; i < allocator_sessions_.size(); ++i) |
199 delete allocator_sessions_[i]; | 226 delete allocator_sessions_[i]; |
200 } | 227 } |
201 | 228 |
202 // Add the allocator session to our list so that we know which sessions | 229 // Add the allocator session to our list so that we know which sessions |
203 // are still active. | 230 // are still active. |
(...skipping 10 matching lines...) Expand all Loading... | |
214 session->SignalCandidatesReady.connect( | 241 session->SignalCandidatesReady.connect( |
215 this, &P2PTransportChannel::OnCandidatesReady); | 242 this, &P2PTransportChannel::OnCandidatesReady); |
216 session->SignalCandidatesAllocationDone.connect( | 243 session->SignalCandidatesAllocationDone.connect( |
217 this, &P2PTransportChannel::OnCandidatesAllocationDone); | 244 this, &P2PTransportChannel::OnCandidatesAllocationDone); |
218 session->StartGettingPorts(); | 245 session->StartGettingPorts(); |
219 } | 246 } |
220 | 247 |
221 void P2PTransportChannel::AddConnection(Connection* connection) { | 248 void P2PTransportChannel::AddConnection(Connection* connection) { |
222 connections_.push_back(connection); | 249 connections_.push_back(connection); |
223 connection->set_remote_ice_mode(remote_ice_mode_); | 250 connection->set_remote_ice_mode(remote_ice_mode_); |
251 connection->set_receiving_timeout(receiving_timeout_); | |
224 connection->SignalReadPacket.connect( | 252 connection->SignalReadPacket.connect( |
225 this, &P2PTransportChannel::OnReadPacket); | 253 this, &P2PTransportChannel::OnReadPacket); |
226 connection->SignalReadyToSend.connect( | 254 connection->SignalReadyToSend.connect( |
227 this, &P2PTransportChannel::OnReadyToSend); | 255 this, &P2PTransportChannel::OnReadyToSend); |
228 connection->SignalStateChange.connect( | 256 connection->SignalStateChange.connect( |
229 this, &P2PTransportChannel::OnConnectionStateChange); | 257 this, &P2PTransportChannel::OnConnectionStateChange); |
230 connection->SignalDestroyed.connect( | 258 connection->SignalDestroyed.connect( |
231 this, &P2PTransportChannel::OnConnectionDestroyed); | 259 this, &P2PTransportChannel::OnConnectionDestroyed); |
232 connection->SignalNominated.connect(this, &P2PTransportChannel::OnNominated); | 260 connection->SignalNominated.connect(this, &P2PTransportChannel::OnNominated); |
233 } | 261 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 | 363 |
336 void P2PTransportChannel::SetReceivingTimeout(int receiving_timeout_ms) { | 364 void P2PTransportChannel::SetReceivingTimeout(int receiving_timeout_ms) { |
337 if (receiving_timeout_ms < 0) { | 365 if (receiving_timeout_ms < 0) { |
338 return; | 366 return; |
339 } | 367 } |
340 receiving_timeout_ = receiving_timeout_ms; | 368 receiving_timeout_ = receiving_timeout_ms; |
341 check_receiving_delay_ = | 369 check_receiving_delay_ = |
342 std::max(MIN_CHECK_RECEIVING_DELAY, receiving_timeout_ / 10); | 370 std::max(MIN_CHECK_RECEIVING_DELAY, receiving_timeout_ / 10); |
343 LOG(LS_VERBOSE) << "Set ICE receiving timeout to " << receiving_timeout_ | 371 LOG(LS_VERBOSE) << "Set ICE receiving timeout to " << receiving_timeout_ |
344 << " milliseconds"; | 372 << " milliseconds"; |
373 for (Connection* connection : connections_) { | |
374 connection->set_receiving_timeout(receiving_timeout_); | |
375 } | |
345 } | 376 } |
346 | 377 |
347 // Go into the state of processing candidates, and running in general | 378 // Go into the state of processing candidates, and running in general |
348 void P2PTransportChannel::Connect() { | 379 void P2PTransportChannel::Connect() { |
349 ASSERT(worker_thread_ == rtc::Thread::Current()); | 380 ASSERT(worker_thread_ == rtc::Thread::Current()); |
350 if (ice_ufrag_.empty() || ice_pwd_.empty()) { | 381 if (ice_ufrag_.empty() || ice_pwd_.empty()) { |
351 ASSERT(false); | 382 ASSERT(false); |
352 LOG(LS_ERROR) << "P2PTransportChannel::Connect: The ice_ufrag_ and the " | 383 LOG(LS_ERROR) << "P2PTransportChannel::Connect: The ice_ufrag_ and the " |
353 << "ice_pwd_ are not set."; | 384 << "ice_pwd_ are not set."; |
354 return; | 385 return; |
355 } | 386 } |
356 | 387 |
357 // Kick off an allocator session | 388 // Kick off an allocator session |
358 Allocate(); | 389 Allocate(); |
359 | 390 |
360 // Start pinging as the ports come in. | 391 // Start pinging as the ports come in. |
361 thread()->Post(this, MSG_PING); | 392 thread()->Post(this, MSG_CHECK_AND_PING); |
362 | |
363 thread()->PostDelayed( | |
364 check_receiving_delay_, this, MSG_CHECK_RECEIVING); | |
365 } | 393 } |
366 | 394 |
367 // A new port is available, attempt to make connections for it | 395 // A new port is available, attempt to make connections for it |
368 void P2PTransportChannel::OnPortReady(PortAllocatorSession *session, | 396 void P2PTransportChannel::OnPortReady(PortAllocatorSession *session, |
369 PortInterface* port) { | 397 PortInterface* port) { |
370 ASSERT(worker_thread_ == rtc::Thread::Current()); | 398 ASSERT(worker_thread_ == rtc::Thread::Current()); |
371 | 399 |
372 // Set in-effect options on the new port | 400 // Set in-effect options on the new port |
373 for (OptionMap::const_iterator it = options_.begin(); | 401 for (OptionMap::const_iterator it = options_.begin(); |
374 it != options_.end(); | 402 it != options_.end(); |
(...skipping 18 matching lines...) Expand all Loading... | |
393 port->SignalDestroyed.connect(this, &P2PTransportChannel::OnPortDestroyed); | 421 port->SignalDestroyed.connect(this, &P2PTransportChannel::OnPortDestroyed); |
394 port->SignalRoleConflict.connect( | 422 port->SignalRoleConflict.connect( |
395 this, &P2PTransportChannel::OnRoleConflict); | 423 this, &P2PTransportChannel::OnRoleConflict); |
396 | 424 |
397 // Attempt to create a connection from this new port to all of the remote | 425 // Attempt to create a connection from this new port to all of the remote |
398 // candidates that we were given so far. | 426 // candidates that we were given so far. |
399 | 427 |
400 std::vector<RemoteCandidate>::iterator iter; | 428 std::vector<RemoteCandidate>::iterator iter; |
401 for (iter = remote_candidates_.begin(); iter != remote_candidates_.end(); | 429 for (iter = remote_candidates_.begin(); iter != remote_candidates_.end(); |
402 ++iter) { | 430 ++iter) { |
403 CreateConnection(port, *iter, iter->origin_port(), false); | 431 CreateConnection(port, *iter, iter->origin_port()); |
404 } | 432 } |
405 | 433 |
406 SortConnections(); | 434 SortConnections(); |
407 } | 435 } |
408 | 436 |
409 // A new candidate is available, let listeners know | 437 // A new candidate is available, let listeners know |
410 void P2PTransportChannel::OnCandidatesReady( | 438 void P2PTransportChannel::OnCandidatesReady( |
411 PortAllocatorSession *session, const std::vector<Candidate>& candidates) { | 439 PortAllocatorSession *session, const std::vector<Candidate>& candidates) { |
412 ASSERT(worker_thread_ == rtc::Thread::Current()); | 440 ASSERT(worker_thread_ == rtc::Thread::Current()); |
413 for (size_t i = 0; i < candidates.size(); ++i) { | 441 for (size_t i = 0; i < candidates.size(); ++i) { |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
609 // remote candidate with an older generation arrives, drop it. | 637 // remote candidate with an older generation arrives, drop it. |
610 if (generation != 0 && generation < remote_candidate_generation_) { | 638 if (generation != 0 && generation < remote_candidate_generation_) { |
611 LOG(LS_WARNING) << "Dropping a remote candidate because its generation " | 639 LOG(LS_WARNING) << "Dropping a remote candidate because its generation " |
612 << generation | 640 << generation |
613 << " is lower than the current remote generation " | 641 << " is lower than the current remote generation " |
614 << remote_candidate_generation_; | 642 << remote_candidate_generation_; |
615 return; | 643 return; |
616 } | 644 } |
617 | 645 |
618 // Create connections to this remote candidate. | 646 // Create connections to this remote candidate. |
619 CreateConnections(candidate, NULL, false); | 647 CreateConnections(candidate, NULL); |
620 | 648 |
621 // Resort the connections list, which may have new elements. | 649 // Resort the connections list, which may have new elements. |
622 SortConnections(); | 650 SortConnections(); |
623 } | 651 } |
624 | 652 |
625 // Creates connections from all of the ports that we care about to the given | 653 // Creates connections from all of the ports that we care about to the given |
626 // remote candidate. The return value is true if we created a connection from | 654 // remote candidate. The return value is true if we created a connection from |
627 // the origin port. | 655 // the origin port. |
628 bool P2PTransportChannel::CreateConnections(const Candidate& remote_candidate, | 656 bool P2PTransportChannel::CreateConnections(const Candidate& remote_candidate, |
629 PortInterface* origin_port, | 657 PortInterface* origin_port) { |
630 bool readable) { | |
631 ASSERT(worker_thread_ == rtc::Thread::Current()); | 658 ASSERT(worker_thread_ == rtc::Thread::Current()); |
632 | 659 |
633 Candidate new_remote_candidate(remote_candidate); | 660 Candidate new_remote_candidate(remote_candidate); |
634 new_remote_candidate.set_generation( | 661 new_remote_candidate.set_generation( |
635 GetRemoteCandidateGeneration(remote_candidate)); | 662 GetRemoteCandidateGeneration(remote_candidate)); |
636 // ICE candidates don't need to have username and password set, but | 663 // ICE candidates don't need to have username and password set, but |
637 // the code below this (specifically, ConnectionRequest::Prepare in | 664 // the code below this (specifically, ConnectionRequest::Prepare in |
638 // port.cc) uses the remote candidates's username. So, we set it | 665 // port.cc) uses the remote candidates's username. So, we set it |
639 // here. | 666 // here. |
640 if (remote_candidate.username().empty()) { | 667 if (remote_candidate.username().empty()) { |
(...skipping 17 matching lines...) Expand all Loading... | |
658 } | 685 } |
659 | 686 |
660 // Add a new connection for this candidate to every port that allows such a | 687 // Add a new connection for this candidate to every port that allows such a |
661 // connection (i.e., if they have compatible protocols) and that does not | 688 // connection (i.e., if they have compatible protocols) and that does not |
662 // already have a connection to an equivalent candidate. We must be careful | 689 // already have a connection to an equivalent candidate. We must be careful |
663 // to make sure that the origin port is included, even if it was pruned, | 690 // to make sure that the origin port is included, even if it was pruned, |
664 // since that may be the only port that can create this connection. | 691 // since that may be the only port that can create this connection. |
665 bool created = false; | 692 bool created = false; |
666 std::vector<PortInterface *>::reverse_iterator it; | 693 std::vector<PortInterface *>::reverse_iterator it; |
667 for (it = ports_.rbegin(); it != ports_.rend(); ++it) { | 694 for (it = ports_.rbegin(); it != ports_.rend(); ++it) { |
668 if (CreateConnection(*it, new_remote_candidate, origin_port, readable)) { | 695 if (CreateConnection(*it, new_remote_candidate, origin_port)) { |
669 if (*it == origin_port) | 696 if (*it == origin_port) |
670 created = true; | 697 created = true; |
671 } | 698 } |
672 } | 699 } |
673 | 700 |
674 if ((origin_port != NULL) && | 701 if ((origin_port != NULL) && |
675 std::find(ports_.begin(), ports_.end(), origin_port) == ports_.end()) { | 702 std::find(ports_.begin(), ports_.end(), origin_port) == ports_.end()) { |
676 if (CreateConnection( | 703 if (CreateConnection(origin_port, new_remote_candidate, origin_port)) |
677 origin_port, new_remote_candidate, origin_port, readable)) | |
678 created = true; | 704 created = true; |
679 } | 705 } |
680 | 706 |
681 // Remember this remote candidate so that we can add it to future ports. | 707 // Remember this remote candidate so that we can add it to future ports. |
682 RememberRemoteCandidate(new_remote_candidate, origin_port); | 708 RememberRemoteCandidate(new_remote_candidate, origin_port); |
683 | 709 |
684 return created; | 710 return created; |
685 } | 711 } |
686 | 712 |
687 // Setup a connection object for the local and remote candidate combination. | 713 // Setup a connection object for the local and remote candidate combination. |
688 // And then listen to connection object for changes. | 714 // And then listen to connection object for changes. |
689 bool P2PTransportChannel::CreateConnection(PortInterface* port, | 715 bool P2PTransportChannel::CreateConnection(PortInterface* port, |
690 const Candidate& remote_candidate, | 716 const Candidate& remote_candidate, |
691 PortInterface* origin_port, | 717 PortInterface* origin_port) { |
692 bool readable) { | |
693 // Look for an existing connection with this remote address. If one is not | 718 // Look for an existing connection with this remote address. If one is not |
694 // found, then we can create a new connection for this address. | 719 // found, then we can create a new connection for this address. |
695 Connection* connection = port->GetConnection(remote_candidate.address()); | 720 Connection* connection = port->GetConnection(remote_candidate.address()); |
696 if (connection != NULL) { | 721 if (connection != NULL) { |
697 connection->MaybeUpdatePeerReflexiveCandidate(remote_candidate); | 722 connection->MaybeUpdatePeerReflexiveCandidate(remote_candidate); |
698 | 723 |
699 // It is not legal to try to change any of the parameters of an existing | 724 // It is not legal to try to change any of the parameters of an existing |
700 // connection; however, the other side can send a duplicate candidate. | 725 // connection; however, the other side can send a duplicate candidate. |
701 if (!remote_candidate.IsEquivalent(connection->remote_candidate())) { | 726 if (!remote_candidate.IsEquivalent(connection->remote_candidate())) { |
702 LOG(INFO) << "Attempt to change a remote candidate." | 727 LOG(INFO) << "Attempt to change a remote candidate." |
(...skipping 14 matching lines...) Expand all Loading... | |
717 connection = port->CreateConnection(remote_candidate, origin); | 742 connection = port->CreateConnection(remote_candidate, origin); |
718 if (!connection) | 743 if (!connection) |
719 return false; | 744 return false; |
720 | 745 |
721 AddConnection(connection); | 746 AddConnection(connection); |
722 | 747 |
723 LOG_J(LS_INFO, this) << "Created connection with origin=" << origin << ", (" | 748 LOG_J(LS_INFO, this) << "Created connection with origin=" << origin << ", (" |
724 << connections_.size() << " total)"; | 749 << connections_.size() << " total)"; |
725 } | 750 } |
726 | 751 |
727 // If we are readable, it is because we are creating this in response to a | |
728 // ping from the other side. This will cause the state to become readable. | |
729 if (readable) | |
730 connection->ReceivedPing(); | |
731 | |
732 return true; | 752 return true; |
733 } | 753 } |
734 | 754 |
735 bool P2PTransportChannel::FindConnection( | 755 bool P2PTransportChannel::FindConnection( |
736 cricket::Connection* connection) const { | 756 cricket::Connection* connection) const { |
737 std::vector<Connection*>::const_iterator citer = | 757 std::vector<Connection*>::const_iterator citer = |
738 std::find(connections_.begin(), connections_.end(), connection); | 758 std::find(connections_.begin(), connections_.end(), connection); |
739 return citer != connections_.end(); | 759 return citer != connections_.end(); |
740 } | 760 } |
741 | 761 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
846 bool P2PTransportChannel::GetStats(ConnectionInfos *infos) { | 866 bool P2PTransportChannel::GetStats(ConnectionInfos *infos) { |
847 ASSERT(worker_thread_ == rtc::Thread::Current()); | 867 ASSERT(worker_thread_ == rtc::Thread::Current()); |
848 // Gather connection infos. | 868 // Gather connection infos. |
849 infos->clear(); | 869 infos->clear(); |
850 | 870 |
851 std::vector<Connection *>::const_iterator it; | 871 std::vector<Connection *>::const_iterator it; |
852 for (it = connections_.begin(); it != connections_.end(); ++it) { | 872 for (it = connections_.begin(); it != connections_.end(); ++it) { |
853 Connection *connection = *it; | 873 Connection *connection = *it; |
854 ConnectionInfo info; | 874 ConnectionInfo info; |
855 info.best_connection = (best_connection_ == connection); | 875 info.best_connection = (best_connection_ == connection); |
856 info.readable = | 876 info.receiving = connection->receiving(); |
857 (connection->read_state() == Connection::STATE_READABLE); | |
858 info.writable = | 877 info.writable = |
859 (connection->write_state() == Connection::STATE_WRITABLE); | 878 (connection->write_state() == Connection::STATE_WRITABLE); |
860 info.timeout = | 879 info.timeout = |
861 (connection->write_state() == Connection::STATE_WRITE_TIMEOUT); | 880 (connection->write_state() == Connection::STATE_WRITE_TIMEOUT); |
862 info.new_connection = !connection->reported(); | 881 info.new_connection = !connection->reported(); |
863 connection->set_reported(true); | 882 connection->set_reported(true); |
864 info.rtt = connection->rtt(); | 883 info.rtt = connection->rtt(); |
865 info.sent_total_bytes = connection->sent_total_bytes(); | 884 info.sent_total_bytes = connection->sent_total_bytes(); |
866 info.sent_bytes_second = connection->sent_bytes_second(); | 885 info.sent_bytes_second = connection->sent_bytes_second(); |
867 info.sent_discarded_packets = connection->sent_discarded_packets(); | 886 info.sent_discarded_packets = connection->sent_discarded_packets(); |
(...skipping 20 matching lines...) Expand all Loading... | |
888 // Begin allocate (or immediately re-allocate, if MSG_ALLOCATE pending) | 907 // Begin allocate (or immediately re-allocate, if MSG_ALLOCATE pending) |
889 void P2PTransportChannel::Allocate() { | 908 void P2PTransportChannel::Allocate() { |
890 // Time for a new allocator, lets make sure we have a signalling channel | 909 // Time for a new allocator, lets make sure we have a signalling channel |
891 // to communicate candidates through first. | 910 // to communicate candidates through first. |
892 waiting_for_signaling_ = true; | 911 waiting_for_signaling_ = true; |
893 SignalRequestSignaling(this); | 912 SignalRequestSignaling(this); |
894 } | 913 } |
895 | 914 |
896 // Monitor connection states. | 915 // Monitor connection states. |
897 void P2PTransportChannel::UpdateConnectionStates() { | 916 void P2PTransportChannel::UpdateConnectionStates() { |
898 uint32 now = rtc::Time(); | |
899 | |
900 // We need to copy the list of connections since some may delete themselves | 917 // We need to copy the list of connections since some may delete themselves |
901 // when we call UpdateState. | 918 // when we call UpdateState. |
919 uint32 now = rtc::Time(); | |
902 for (uint32 i = 0; i < connections_.size(); ++i) | 920 for (uint32 i = 0; i < connections_.size(); ++i) |
903 connections_[i]->UpdateState(now); | 921 connections_[i]->UpdateState(now); |
904 } | 922 } |
905 | 923 |
906 // Prepare for best candidate sorting. | 924 // Prepare for best candidate sorting. |
907 void P2PTransportChannel::RequestSort() { | 925 void P2PTransportChannel::RequestSort() { |
908 if (!sort_dirty_) { | 926 if (!sort_dirty_) { |
909 worker_thread_->Post(this, MSG_SORT); | 927 worker_thread_->Post(this, MSG_SORT); |
910 sort_dirty_ = true; | 928 sort_dirty_ = true; |
911 } | 929 } |
(...skipping 18 matching lines...) Expand all Loading... | |
930 ConnectionCompare cmp; | 948 ConnectionCompare cmp; |
931 std::stable_sort(connections_.begin(), connections_.end(), cmp); | 949 std::stable_sort(connections_.begin(), connections_.end(), cmp); |
932 LOG(LS_VERBOSE) << "Sorting available connections:"; | 950 LOG(LS_VERBOSE) << "Sorting available connections:"; |
933 for (uint32 i = 0; i < connections_.size(); ++i) { | 951 for (uint32 i = 0; i < connections_.size(); ++i) { |
934 LOG(LS_VERBOSE) << connections_[i]->ToString(); | 952 LOG(LS_VERBOSE) << connections_[i]->ToString(); |
935 } | 953 } |
936 | 954 |
937 Connection* top_connection = | 955 Connection* top_connection = |
938 (connections_.size() > 0) ? connections_[0] : nullptr; | 956 (connections_.size() > 0) ? connections_[0] : nullptr; |
939 | 957 |
940 // If necessary, switch to the new choice. | 958 if (top_connection != best_connection_ && best_connection_->writable() && |
941 // Note that |top_connection| doesn't have to be writable to become the best | 959 !top_connection->writable()) { |
942 // connection although it will have higher priority if it is writable. | 960 // This is a very special case where the best_connection becomes |
943 // The controlled side can switch the best connection only if the current | 961 // not receiving but the top connection is not writable. |
pthatcher1
2015/09/17 05:58:41
It seems like, instead, that we should always go i
honghaiz3
2015/09/17 19:47:56
Yes. That is what is done.
Combining the comments
| |
944 // |best connection_| has not been nominated by the controlling side yet. | 962 // We don't switch but check the writability of the |top_connection|. |
945 if ((ice_role_ == ICEROLE_CONTROLLING || !best_nominated_connection()) && | 963 // If we are in the long ping delay, reschedule it so that it will be |
946 ShouldSwitch(best_connection_, top_connection)) { | 964 // executed much sooner. If we are in the short ping delay, just make |
965 // it the next one to be pinged, so that we do not overflow the channel | |
966 // with ping messages. | |
967 if (ping_delay_ == LONG_PING_DELAY) { | |
968 thread()->Clear(this, MSG_CHECK_AND_PING); | |
969 thread()->Post(this, MSG_CHECK_AND_PING); | |
970 } | |
971 next_connection_to_ping_ = top_connection; | |
pthatcher1
2015/09/17 05:58:42
Shouldn't the faster pinging work fine without sto
honghaiz3
2015/09/17 19:47:56
Yes. We don't need the extra speedup of the ping r
pthatcher1
2015/09/17 22:01:17
So are you going to remove next_connection_to_ping
| |
972 } else if (ShouldSwitch(best_connection_, top_connection, ice_role_)) { | |
973 // If necessary, switch to the new choice. | |
974 // Note that |top_connection| doesn't have to be writable to become the best | |
975 // connection although it will have higher priority if it is writable. | |
947 LOG(LS_INFO) << "Switching best connection: " << top_connection->ToString(); | 976 LOG(LS_INFO) << "Switching best connection: " << top_connection->ToString(); |
948 SwitchBestConnectionTo(top_connection); | 977 SwitchBestConnectionTo(top_connection); |
949 } | 978 } |
950 | 979 |
951 // Controlled side can prune only if the best connection has been nominated. | 980 // Controlled side can prune only if the best connection has been nominated. |
952 // because otherwise it may delete the connection that will be selected by | 981 // because otherwise it may delete the connection that will be selected by |
953 // the controlling side. | 982 // the controlling side. |
954 if (ice_role_ == ICEROLE_CONTROLLING || best_nominated_connection()) { | 983 if (ice_role_ == ICEROLE_CONTROLLING || best_nominated_connection()) { |
955 PruneConnections(); | 984 PruneConnections(); |
956 } | 985 } |
(...skipping 29 matching lines...) Expand all Loading... | |
986 | 1015 |
987 void P2PTransportChannel::PruneConnections() { | 1016 void P2PTransportChannel::PruneConnections() { |
988 // We can prune any connection for which there is a connected, writable | 1017 // We can prune any connection for which there is a connected, writable |
989 // connection on the same network with better or equal priority. We leave | 1018 // connection on the same network with better or equal priority. We leave |
990 // those with better priority just in case they become writable later (at | 1019 // those with better priority just in case they become writable later (at |
991 // which point, we would prune out the current best connection). We leave | 1020 // which point, we would prune out the current best connection). We leave |
992 // connections on other networks because they may not be using the same | 1021 // connections on other networks because they may not be using the same |
993 // resources and they may represent very distinct paths over which we can | 1022 // resources and they may represent very distinct paths over which we can |
994 // switch. If the |primier| connection is not connected, we may be | 1023 // switch. If the |primier| connection is not connected, we may be |
995 // reconnecting a TCP connection and temporarily do not prune connections in | 1024 // reconnecting a TCP connection and temporarily do not prune connections in |
996 // this network. See the big comment in CompareConnections. | 1025 // this network. See the big comment in CompareConnections. If the |primier| |
1026 // connection is not receiving, we do not prune the connections in the network | |
1027 // because either some connection will become receiving or all connections | |
1028 // in the network will time out and be deleted. | |
pthatcher1
2015/09/17 05:58:42
I think this would be more clear as "once on conne
honghaiz3
2015/09/17 19:47:55
Removed the comments as we change back to check wr
| |
997 | 1029 |
998 // Get a list of the networks that we are using. | 1030 // Get a list of the networks that we are using. |
999 std::set<rtc::Network*> networks; | 1031 std::set<rtc::Network*> networks; |
1000 for (const Connection* conn : connections_) { | 1032 for (const Connection* conn : connections_) { |
1001 networks.insert(conn->port()->Network()); | 1033 networks.insert(conn->port()->Network()); |
1002 } | 1034 } |
1003 for (rtc::Network* network : networks) { | 1035 for (rtc::Network* network : networks) { |
1004 Connection* primier = GetBestConnectionOnNetwork(network); | 1036 Connection* primier = GetBestConnectionOnNetwork(network); |
1005 if (!(primier && primier->writable() && primier->connected())) { | 1037 if (!(primier && primier->receiving() && primier->connected())) { |
pthatcher1
2015/09/17 05:58:41
Why don't we want to keep it as writable()? Shoul
honghaiz3
2015/09/17 19:47:56
Yes. especially now that we put writable with high
| |
1006 continue; | 1038 continue; |
1007 } | 1039 } |
1008 | 1040 |
1009 for (Connection* conn : connections_) { | 1041 for (Connection* conn : connections_) { |
1010 if ((conn != primier) && (conn->port()->Network() == network) && | 1042 if ((conn != primier) && (conn->port()->Network() == network) && |
1043 (conn != pending_best_connection_) && | |
1011 (CompareConnectionCandidates(primier, conn) >= 0)) { | 1044 (CompareConnectionCandidates(primier, conn) >= 0)) { |
1012 conn->Prune(); | 1045 conn->Prune(); |
1013 } | 1046 } |
1014 } | 1047 } |
1015 } | 1048 } |
1016 } | 1049 } |
1017 | 1050 |
1018 // Track the best connection, and let listeners know | 1051 // Track the best connection, and let listeners know |
1019 void P2PTransportChannel::SwitchBestConnectionTo(Connection* conn) { | 1052 void P2PTransportChannel::SwitchBestConnectionTo(Connection* conn) { |
1020 // Note: if conn is NULL, the previous best_connection_ has been destroyed, | 1053 // Note: if conn is NULL, the previous best_connection_ has been destroyed, |
(...skipping 11 matching lines...) Expand all Loading... | |
1032 // When it just switched to a best connection, set receiving to true. | 1065 // When it just switched to a best connection, set receiving to true. |
1033 set_receiving(true); | 1066 set_receiving(true); |
1034 } else { | 1067 } else { |
1035 LOG_J(LS_INFO, this) << "No best connection"; | 1068 LOG_J(LS_INFO, this) << "No best connection"; |
1036 } | 1069 } |
1037 } | 1070 } |
1038 | 1071 |
1039 void P2PTransportChannel::UpdateChannelState() { | 1072 void P2PTransportChannel::UpdateChannelState() { |
1040 // The Handle* functions already set the writable state. We'll just double- | 1073 // The Handle* functions already set the writable state. We'll just double- |
1041 // check it here. | 1074 // check it here. |
1042 bool writable = ((best_connection_ != NULL) && | 1075 bool writable = best_connection_ && best_connection_->writable(); |
1043 (best_connection_->write_state() == | |
1044 Connection::STATE_WRITABLE)); | |
1045 ASSERT(writable == this->writable()); | 1076 ASSERT(writable == this->writable()); |
1046 if (writable != this->writable()) | 1077 if (writable != this->writable()) |
1047 LOG(LS_ERROR) << "UpdateChannelState: writable state mismatch"; | 1078 LOG(LS_ERROR) << "UpdateChannelState: writable state mismatch"; |
1048 | 1079 |
1049 bool readable = false; | 1080 bool receiving = best_connection_ && best_connection_->receiving(); |
pthatcher1
2015/09/17 05:58:42
Shouldn't the logic here be the same as the previo
honghaiz3
2015/09/17 19:47:56
Done.
I was thinking that we should use the best_
| |
1050 for (uint32 i = 0; i < connections_.size(); ++i) { | 1081 set_receiving(receiving); |
1051 if (connections_[i]->read_state() == Connection::STATE_READABLE) { | |
1052 readable = true; | |
1053 break; | |
1054 } | |
1055 } | |
1056 set_readable(readable); | |
1057 } | 1082 } |
1058 | 1083 |
1059 // We checked the status of our connections and we had at least one that | 1084 // We checked the status of our connections and we had at least one that |
1060 // was writable, go into the writable state. | 1085 // was writable, go into the writable state. |
1061 void P2PTransportChannel::HandleWritable() { | 1086 void P2PTransportChannel::HandleWritable() { |
1062 ASSERT(worker_thread_ == rtc::Thread::Current()); | 1087 ASSERT(worker_thread_ == rtc::Thread::Current()); |
1063 if (!writable()) { | 1088 if (!writable()) { |
1064 for (uint32 i = 0; i < allocator_sessions_.size(); ++i) { | 1089 for (uint32 i = 0; i < allocator_sessions_.size(); ++i) { |
1065 if (allocator_sessions_[i]->IsGettingPorts()) { | 1090 if (allocator_sessions_[i]->IsGettingPorts()) { |
1066 allocator_sessions_[i]->StopGettingPorts(); | 1091 allocator_sessions_[i]->StopGettingPorts(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1102 | 1127 |
1103 return NULL; | 1128 return NULL; |
1104 } | 1129 } |
1105 | 1130 |
1106 // Handle any queued up requests | 1131 // Handle any queued up requests |
1107 void P2PTransportChannel::OnMessage(rtc::Message *pmsg) { | 1132 void P2PTransportChannel::OnMessage(rtc::Message *pmsg) { |
1108 switch (pmsg->message_id) { | 1133 switch (pmsg->message_id) { |
1109 case MSG_SORT: | 1134 case MSG_SORT: |
1110 OnSort(); | 1135 OnSort(); |
1111 break; | 1136 break; |
1112 case MSG_PING: | 1137 case MSG_CHECK_AND_PING: |
1113 OnPing(); | 1138 OnCheckAndPing(); |
1114 break; | |
1115 case MSG_CHECK_RECEIVING: | |
1116 OnCheckReceiving(); | |
1117 break; | 1139 break; |
1118 default: | 1140 default: |
1119 ASSERT(false); | 1141 ASSERT(false); |
1120 break; | 1142 break; |
1121 } | 1143 } |
1122 } | 1144 } |
1123 | 1145 |
1124 // Handle queued up sort request | 1146 // Handle queued up sort request |
1125 void P2PTransportChannel::OnSort() { | 1147 void P2PTransportChannel::OnSort() { |
1126 // Resort the connections based on the new statistics. | 1148 // Resort the connections based on the new statistics. |
1127 SortConnections(); | 1149 SortConnections(); |
1128 } | 1150 } |
1129 | 1151 |
1130 // Handle queued up ping request | 1152 void P2PTransportChannel::OnCheckAndPing() { |
1131 void P2PTransportChannel::OnPing() { | |
1132 // Make sure the states of the connections are up-to-date (since this affects | |
1133 // which ones are pingable). | |
1134 UpdateConnectionStates(); | 1153 UpdateConnectionStates(); |
1135 | 1154 // When the best connection is not receiving or writable, switch to short ping |
1136 // Find the oldest pingable connection and have it do a ping. | 1155 // delay. |
pthatcher1
2015/09/17 05:58:41
not receiving or writable => not receiving or not
honghaiz3
2015/09/17 19:47:56
Done with neither nor.
| |
1137 Connection* conn = FindNextPingableConnection(); | 1156 ping_delay_ = writable() && receiving() ? LONG_PING_DELAY : SHORT_PING_DELAY; |
pthatcher1
2015/09/17 05:58:41
We might even consider making a method
bool weak
honghaiz3
2015/09/17 19:47:56
Done.
| |
1138 if (conn) | 1157 uint32 now = rtc::Time(); |
1139 PingConnection(conn); | 1158 if (now >= last_ping_sent_ + ping_delay_) { |
pthatcher1
2015/09/17 05:58:41
Might be more readable as:
uint32 time_since_last
honghaiz3
2015/09/17 19:47:55
Similar to the other comments. I try to avoid subt
| |
1140 | 1159 Connection* conn = FindNextPingableConnection(); |
1141 // Post ourselves a message to perform the next ping. | 1160 if (conn) { |
1142 uint32 delay = writable() ? WRITABLE_DELAY : UNWRITABLE_DELAY; | 1161 PingConnection(conn); |
1143 thread()->PostDelayed(delay, this, MSG_PING); | 1162 } |
1144 } | |
1145 | |
1146 void P2PTransportChannel::OnCheckReceiving() { | |
1147 // Check receiving only if the best connection has received data packets | |
1148 // because we want to detect not receiving any packets only after the media | |
1149 // have started flowing. | |
1150 if (best_connection_ && best_connection_->recv_total_bytes() > 0) { | |
1151 bool receiving = rtc::Time() <= | |
1152 best_connection_->last_received() + receiving_timeout_; | |
1153 set_receiving(receiving); | |
1154 } | 1163 } |
1155 | 1164 uint32 check_delay = std::min(ping_delay_, check_receiving_delay_); |
1156 thread()->PostDelayed(check_receiving_delay_, this, MSG_CHECK_RECEIVING); | 1165 thread()->PostDelayed(check_delay, this, MSG_CHECK_AND_PING); |
pthatcher1
2015/09/17 05:58:41
I think this was more clear before with two "loops
honghaiz3
2015/09/17 19:47:55
For the two reasons I mentioned at the top.
If yo
| |
1157 } | 1166 } |
1158 | 1167 |
1159 // Is the connection in a state for us to even consider pinging the other side? | 1168 // Is the connection in a state for us to even consider pinging the other side? |
1160 // We consider a connection pingable even if it's not connected because that's | 1169 // We consider a connection pingable even if it's not connected because that's |
1161 // how a TCP connection is kicked into reconnecting on the active side. | 1170 // how a TCP connection is kicked into reconnecting on the active side. |
1162 bool P2PTransportChannel::IsPingable(Connection* conn) { | 1171 bool P2PTransportChannel::IsPingable(Connection* conn) { |
1163 const Candidate& remote = conn->remote_candidate(); | 1172 const Candidate& remote = conn->remote_candidate(); |
1164 // We should never get this far with an empty remote ufrag. | 1173 // We should never get this far with an empty remote ufrag. |
1165 ASSERT(!remote.username().empty()); | 1174 ASSERT(!remote.username().empty()); |
1166 if (remote.username().empty() || remote.password().empty()) { | 1175 if (remote.username().empty() || remote.password().empty()) { |
1167 // If we don't have an ICE ufrag and pwd, there's no way we can ping. | 1176 // If we don't have an ICE ufrag and pwd, there's no way we can ping. |
1168 return false; | 1177 return false; |
1169 } | 1178 } |
1170 | 1179 |
1171 // An never connected connection cannot be written to at all, so pinging is | 1180 // An never connected connection cannot be written to at all, so pinging is |
1172 // out of the question. However, if it has become WRITABLE, it is in the | 1181 // out of the question. However, if it has become WRITABLE, it is in the |
1173 // reconnecting state so ping is needed. | 1182 // reconnecting state so ping is needed. |
1174 if (!conn->connected() && conn->write_state() != Connection::STATE_WRITABLE) { | 1183 if (!conn->connected() && conn->write_state() != Connection::STATE_WRITABLE) { |
1175 return false; | 1184 return false; |
1176 } | 1185 } |
1177 | 1186 |
pthatcher1
2015/09/17 05:58:41
Might be more readable as:
// If the channel is w
honghaiz3
2015/09/17 19:47:56
Done.
| |
1178 if (writable()) { | 1187 if (writable() && receiving()) { |
1179 // If we are writable, then we only want to ping connections that could be | 1188 // If we are writable and also receiving, then we only want to ping |
1180 // better than this one, i.e., the ones that were not pruned. | 1189 // connections that could be better than this one, i.e., the ones that |
1181 return (conn->write_state() != Connection::STATE_WRITE_TIMEOUT); | 1190 // were not pruned. |
1182 } else { | 1191 return !conn->pruned(); |
1183 // If we are not writable, then we need to try everything that might work. | |
1184 // This includes both connections that do not have write timeout as well as | |
1185 // ones that do not have read timeout. A connection could be readable but | |
1186 // be in write-timeout if we pruned it before. Since the other side is | |
1187 // still pinging it, it very well might still work. | |
1188 return (conn->write_state() != Connection::STATE_WRITE_TIMEOUT) || | |
1189 (conn->read_state() != Connection::STATE_READ_TIMEOUT); | |
1190 } | 1192 } |
1193 // If we are not writable, then we need to try everything that might work. | |
1194 return true; | |
1191 } | 1195 } |
1192 | 1196 |
1193 // Returns the next pingable connection to ping. This will be the oldest | 1197 // Returns the next pingable connection to ping. This will be the oldest |
1194 // pingable connection unless we have a connected, writable connection that is | 1198 // pingable connection unless we have a connected, writable connection that is |
1195 // past the maximum acceptable ping delay. When reconnecting a TCP connection, | 1199 // past the maximum acceptable ping delay. When reconnecting a TCP connection, |
1196 // the best connection is disconnected, although still WRITABLE while | 1200 // the best connection is disconnected, although still WRITABLE while |
1197 // reconnecting. The newly created connection should be selected as the ping | 1201 // reconnecting. The newly created connection should be selected as the ping |
1198 // target to become writable instead. See the big comment in CompareConnections. | 1202 // target to become writable instead. See the big comment in CompareConnections. |
1199 Connection* P2PTransportChannel::FindNextPingableConnection() { | 1203 Connection* P2PTransportChannel::FindNextPingableConnection() { |
1204 if (next_connection_to_ping_ && !next_connection_to_ping_->writable()) { | |
1205 Connection* conn = next_connection_to_ping_; | |
1206 next_connection_to_ping_ = nullptr; | |
1207 return conn; | |
1208 } | |
1200 uint32 now = rtc::Time(); | 1209 uint32 now = rtc::Time(); |
1201 if (best_connection_ && best_connection_->connected() && | 1210 if (best_connection_ && best_connection_->connected() && |
1202 (best_connection_->write_state() == Connection::STATE_WRITABLE) && | 1211 best_connection_->writable() && best_connection_->receiving() && |
1203 (best_connection_->last_ping_sent() + MAX_CURRENT_WRITABLE_DELAY <= | 1212 (best_connection_->last_ping_sent() + MAX_CURRENT_WRITABLE_DELAY <= |
1204 now)) { | 1213 now)) { |
1205 return best_connection_; | 1214 return best_connection_; |
1206 } | 1215 } |
1207 | 1216 |
1208 // First, find "triggered checks". We ping first those connections | 1217 // First, find "triggered checks". We ping first those connections |
1209 // that have received a ping but have not sent a ping since receiving | 1218 // that have received a ping but have not sent a ping since receiving |
1210 // it (last_received_ping > last_sent_ping). But we shouldn't do | 1219 // it (last_received_ping > last_sent_ping). But we shouldn't do |
1211 // triggered checks if the connection is already writable. | 1220 // triggered checks if the connection is already writable. |
1212 Connection* oldest_needing_triggered_check = nullptr; | 1221 Connection* oldest_needing_triggered_check = nullptr; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1253 bool use_candidate = false; | 1262 bool use_candidate = false; |
1254 if (remote_ice_mode_ == ICEMODE_FULL && ice_role_ == ICEROLE_CONTROLLING) { | 1263 if (remote_ice_mode_ == ICEMODE_FULL && ice_role_ == ICEROLE_CONTROLLING) { |
1255 use_candidate = (conn == best_connection_) || | 1264 use_candidate = (conn == best_connection_) || |
1256 (best_connection_ == NULL) || | 1265 (best_connection_ == NULL) || |
1257 (!best_connection_->writable()) || | 1266 (!best_connection_->writable()) || |
1258 (conn->priority() > best_connection_->priority()); | 1267 (conn->priority() > best_connection_->priority()); |
1259 } else if (remote_ice_mode_ == ICEMODE_LITE && conn == best_connection_) { | 1268 } else if (remote_ice_mode_ == ICEMODE_LITE && conn == best_connection_) { |
1260 use_candidate = best_connection_->writable(); | 1269 use_candidate = best_connection_->writable(); |
1261 } | 1270 } |
1262 conn->set_use_candidate_attr(use_candidate); | 1271 conn->set_use_candidate_attr(use_candidate); |
1263 conn->Ping(rtc::Time()); | 1272 last_ping_sent_ = rtc::Time(); |
1273 conn->Ping(last_ping_sent_); | |
1264 } | 1274 } |
1265 | 1275 |
1266 // When a connection's state changes, we need to figure out who to use as | 1276 // When a connection's state changes, we need to figure out who to use as |
1267 // the best connection again. It could have become usable, or become unusable. | 1277 // the best connection again. It could have become usable, or become unusable. |
1268 void P2PTransportChannel::OnConnectionStateChange(Connection* connection) { | 1278 void P2PTransportChannel::OnConnectionStateChange(Connection* connection) { |
1269 ASSERT(worker_thread_ == rtc::Thread::Current()); | 1279 ASSERT(worker_thread_ == rtc::Thread::Current()); |
1270 | 1280 |
1271 // Update the best connection if the state change is from pending best | 1281 // Update the best connection if the state change is from pending best |
1272 // connection and role is controlled. | 1282 // connection and role is controlled. |
1273 if (ice_role_ == ICEROLE_CONTROLLED) { | 1283 if (ice_role_ == ICEROLE_CONTROLLED) { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1355 } | 1365 } |
1356 } | 1366 } |
1357 | 1367 |
1358 void P2PTransportChannel::OnReadyToSend(Connection* connection) { | 1368 void P2PTransportChannel::OnReadyToSend(Connection* connection) { |
1359 if (connection == best_connection_ && writable()) { | 1369 if (connection == best_connection_ && writable()) { |
1360 SignalReadyToSend(this); | 1370 SignalReadyToSend(this); |
1361 } | 1371 } |
1362 } | 1372 } |
1363 | 1373 |
1364 } // namespace cricket | 1374 } // namespace cricket |
OLD | NEW |