Index: webrtc/p2p/base/port.cc |
diff --git a/webrtc/p2p/base/port.cc b/webrtc/p2p/base/port.cc |
index b6173ebf45768be331c589631f81f9aee8ccbc80..a2dbb9cfa22ed1fe365ece2aa7e1911741f43394 100644 |
--- a/webrtc/p2p/base/port.cc |
+++ b/webrtc/p2p/base/port.cc |
@@ -198,6 +198,7 @@ void Port::Construct() { |
network_->SignalTypeChanged.connect(this, &Port::OnNetworkTypeChanged); |
network_cost_ = network_->GetCost(); |
+ thread_->PostDelayed(RTC_FROM_HERE, timeout_delay_, this, MSG_CHECK_DEAD); |
LOG_J(LS_INFO, this) << "Port created with network cost " << network_cost_; |
} |
@@ -644,13 +645,28 @@ void Port::SendBindingErrorResponse(StunMessage* request, |
<< " to " << addr.ToSensitiveString(); |
} |
+void Port::StartBeingUsed() { |
+ is_used_ = true; |
+} |
+ |
+void Port::StopBeingUsed() { |
+ is_used_ = false; |
+ thread_->Post(RTC_FROM_HERE, this, MSG_CHECK_DEAD); |
+} |
+ |
void Port::OnMessage(rtc::Message *pmsg) { |
- ASSERT(pmsg->message_id == MSG_DEAD); |
+ ASSERT(pmsg->message_id == MSG_CHECK_DEAD); |
if (dead()) { |
Destroy(); |
} |
} |
+bool Port::dead() const { |
+ return !is_used_ && connections_.empty() && |
+ rtc::TimeMillis() - last_time_all_connections_removed_ >= |
+ timeout_delay_; |
+} |
+ |
void Port::OnNetworkTypeChanged(const rtc::Network* network) { |
ASSERT(network == network_); |
@@ -699,12 +715,14 @@ void Port::OnConnectionDestroyed(Connection* conn) { |
connections_.erase(iter); |
HandleConnectionDestroyed(conn); |
- // On the controlled side, ports time out after all connections fail. |
+ // Ports time out after all connections fail if it is not used by any channel. |
// Note: If a new connection is added after this message is posted, but it |
- // fails and is removed before kPortTimeoutDelay, then this message will |
- // still cause the Port to be destroyed. |
- if (dead()) { |
- thread_->PostDelayed(RTC_FROM_HERE, timeout_delay_, this, MSG_DEAD); |
+ // fails and is removed before kPortTimeoutDelay, then the port will not be |
+ // destroyed until kPortTimeoutDelay milliseconds after the new connection |
+ // is removed. |
+ if (connections_.empty()) { |
+ last_time_all_connections_removed_ = rtc::TimeMillis(); |
+ thread_->PostDelayed(RTC_FROM_HERE, timeout_delay_, this, MSG_CHECK_DEAD); |
} |
} |