1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "services/network/p2p/socket_throttler.h" 6 7 #include <utility> 8 9 #include "third_party/webrtc/rtc_base/data_rate_limiter.h" 10 #include "third_party/webrtc/rtc_base/time_utils.h" 11 12 namespace network { 13 14 namespace { 15 16 const int kMaxIceMessageBandwidth = 256 * 1024; 17 18 } // namespace 19 P2PMessageThrottler()20P2PMessageThrottler::P2PMessageThrottler() 21 : rate_limiter_(new rtc::DataRateLimiter(kMaxIceMessageBandwidth, 1.0)) {} 22 ~P2PMessageThrottler()23P2PMessageThrottler::~P2PMessageThrottler() {} 24 SetSendIceBandwidth(int bandwidth_kbps)25void P2PMessageThrottler::SetSendIceBandwidth(int bandwidth_kbps) { 26 rate_limiter_.reset(new rtc::DataRateLimiter(bandwidth_kbps, 1.0)); 27 } 28 DropNextPacket(size_t packet_len)29bool P2PMessageThrottler::DropNextPacket(size_t packet_len) { 30 double now = rtc::TimeNanos() / static_cast<double>(rtc::kNumNanosecsPerSec); 31 if (!rate_limiter_->CanUse(packet_len, now)) { 32 // Exceeding the send rate, this packet should be dropped. 33 return true; 34 } 35 36 rate_limiter_->Use(packet_len, now); 37 return false; 38 } 39 40 } // namespace network 41