Stop sending stun binding requests after certain amount of time.

Also stop it if the request timed out.

It is going to be complicated to keep this and make it sync with the connection bind request as they may be on two different ports.

BUG=

Review URL: https://codereview.webrtc.org/1465843004

Cr-Commit-Position: refs/heads/master@{#10899}
This commit is contained in:
honghaiz 2015-12-04 08:57:26 -08:00 committed by Commit bot
parent 9e1b992f74
commit 45b0efd378

View File

@ -24,15 +24,19 @@ namespace cricket {
// TODO: Move these to a common place (used in relayport too) // TODO: Move these to a common place (used in relayport too)
const int KEEPALIVE_DELAY = 10 * 1000; // 10 seconds - sort timeouts const int KEEPALIVE_DELAY = 10 * 1000; // 10 seconds - sort timeouts
const int RETRY_DELAY = 50; // 50ms, from ICE spec
const int RETRY_TIMEOUT = 50 * 1000; // ICE says 50 secs const int RETRY_TIMEOUT = 50 * 1000; // ICE says 50 secs
// Stop sending STUN binding requests after this amount of time
// (in milliseconds) because the connection binding requests should keep
// the NAT binding alive.
const int KEEP_ALIVE_TIMEOUT = 2 * 60 * 1000; // 2 minutes
// Handles a binding request sent to the STUN server. // Handles a binding request sent to the STUN server.
class StunBindingRequest : public StunRequest { class StunBindingRequest : public StunRequest {
public: public:
StunBindingRequest(UDPPort* port, bool keep_alive, StunBindingRequest(UDPPort* port,
const rtc::SocketAddress& addr) const rtc::SocketAddress& addr,
: port_(port), keep_alive_(keep_alive), server_addr_(addr) { uint32_t deadline)
: port_(port), server_addr_(addr), deadline_(deadline) {
start_time_ = rtc::Time(); start_time_ = rtc::Time();
} }
@ -59,10 +63,10 @@ class StunBindingRequest : public StunRequest {
} }
// We will do a keep-alive regardless of whether this request succeeds. // We will do a keep-alive regardless of whether this request succeeds.
// This should have almost no impact on network usage. // It will be stopped after |deadline_| mostly to conserve the battery life.
if (keep_alive_) { if (rtc::Time() <= deadline_) {
port_->requests_.SendDelayed( port_->requests_.SendDelayed(
new StunBindingRequest(port_, true, server_addr_), new StunBindingRequest(port_, server_addr_, deadline_),
port_->stun_keepalive_delay()); port_->stun_keepalive_delay());
} }
} }
@ -80,10 +84,10 @@ class StunBindingRequest : public StunRequest {
port_->OnStunBindingOrResolveRequestFailed(server_addr_); port_->OnStunBindingOrResolveRequestFailed(server_addr_);
if (keep_alive_ uint32_t now = rtc::Time();
&& (rtc::TimeSince(start_time_) <= RETRY_TIMEOUT)) { if (now <= deadline_ && rtc::TimeDiff(now, start_time_) <= RETRY_TIMEOUT) {
port_->requests_.SendDelayed( port_->requests_.SendDelayed(
new StunBindingRequest(port_, true, server_addr_), new StunBindingRequest(port_, server_addr_, deadline_),
port_->stun_keepalive_delay()); port_->stun_keepalive_delay());
} }
} }
@ -94,20 +98,13 @@ class StunBindingRequest : public StunRequest {
<< " (" << port_->Network()->name() << ")"; << " (" << port_->Network()->name() << ")";
port_->OnStunBindingOrResolveRequestFailed(server_addr_); port_->OnStunBindingOrResolveRequestFailed(server_addr_);
if (keep_alive_
&& (rtc::TimeSince(start_time_) <= RETRY_TIMEOUT)) {
port_->requests_.SendDelayed(
new StunBindingRequest(port_, true, server_addr_),
RETRY_DELAY);
}
} }
private: private:
UDPPort* port_; UDPPort* port_;
bool keep_alive_;
const rtc::SocketAddress server_addr_; const rtc::SocketAddress server_addr_;
uint32_t start_time_; uint32_t start_time_;
uint32_t deadline_;
}; };
UDPPort::AddressResolver::AddressResolver( UDPPort::AddressResolver::AddressResolver(
@ -351,7 +348,7 @@ void UDPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
void UDPPort::SendStunBindingRequests() { void UDPPort::SendStunBindingRequests() {
// We will keep pinging the stun server to make sure our NAT pin-hole stays // We will keep pinging the stun server to make sure our NAT pin-hole stays
// open during the call. // open until the deadline (specified in SendStunBindingRequest).
ASSERT(requests_.empty()); ASSERT(requests_.empty());
for (ServerAddresses::const_iterator it = server_addresses_.begin(); for (ServerAddresses::const_iterator it = server_addresses_.begin();
@ -397,7 +394,8 @@ void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) {
} else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) { } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) {
// Check if |server_addr_| is compatible with the port's ip. // Check if |server_addr_| is compatible with the port's ip.
if (IsCompatibleAddress(stun_addr)) { if (IsCompatibleAddress(stun_addr)) {
requests_.Send(new StunBindingRequest(this, true, stun_addr)); requests_.Send(new StunBindingRequest(this, stun_addr,
rtc::Time() + KEEP_ALIVE_TIMEOUT));
} else { } else {
// Since we can't send stun messages to the server, we should mark this // Since we can't send stun messages to the server, we should mark this
// port ready. // port ready.