| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "net/quic/quic_bandwidth.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "net/quic/quic_bug_tracker.h" | |
| 11 #include "net/quic/quic_time.h" | |
| 12 #include "net/quic/quic_types.h" | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 // Highest number that QuicBandwidth can hold. | |
| 17 const int64_t kQuicInfiniteBandwidth = INT64_C(0x7fffffffffffffff); | |
| 18 | |
| 19 // static | |
| 20 QuicBandwidth QuicBandwidth::Zero() { | |
| 21 return QuicBandwidth(0); | |
| 22 } | |
| 23 | |
| 24 // static | |
| 25 QuicBandwidth QuicBandwidth::FromBitsPerSecond(int64_t bits_per_second) { | |
| 26 return QuicBandwidth(bits_per_second); | |
| 27 } | |
| 28 | |
| 29 // static | |
| 30 QuicBandwidth QuicBandwidth::FromKBitsPerSecond(int64_t k_bits_per_second) { | |
| 31 DCHECK(k_bits_per_second < kQuicInfiniteBandwidth / 1000); | |
| 32 return QuicBandwidth(k_bits_per_second * 1000); | |
| 33 } | |
| 34 | |
| 35 // static | |
| 36 QuicBandwidth QuicBandwidth::FromBytesPerSecond(int64_t bytes_per_second) { | |
| 37 DCHECK(bytes_per_second < kQuicInfiniteBandwidth / 8); | |
| 38 return QuicBandwidth(bytes_per_second * 8); | |
| 39 } | |
| 40 | |
| 41 // static | |
| 42 QuicBandwidth QuicBandwidth::FromKBytesPerSecond(int64_t k_bytes_per_second) { | |
| 43 DCHECK(k_bytes_per_second < kQuicInfiniteBandwidth / 8000); | |
| 44 return QuicBandwidth(k_bytes_per_second * 8000); | |
| 45 } | |
| 46 | |
| 47 // static | |
| 48 QuicBandwidth QuicBandwidth::FromBytesAndTimeDelta(QuicByteCount bytes, | |
| 49 QuicTime::Delta delta) { | |
| 50 DCHECK_LT(bytes, static_cast<uint64_t>(kQuicInfiniteBandwidth / | |
| 51 (8 * kNumMicrosPerSecond))); | |
| 52 int64_t bytes_per_second = | |
| 53 (bytes * kNumMicrosPerSecond) / delta.ToMicroseconds(); | |
| 54 return QuicBandwidth(bytes_per_second * 8); | |
| 55 } | |
| 56 | |
| 57 QuicBandwidth::QuicBandwidth(int64_t bits_per_second) | |
| 58 : bits_per_second_(bits_per_second) { | |
| 59 if (bits_per_second < 0) { | |
| 60 QUIC_BUG << "Can't set negative bandwidth " << bits_per_second; | |
| 61 bits_per_second_ = 0; | |
| 62 return; | |
| 63 } | |
| 64 bits_per_second_ = bits_per_second; | |
| 65 } | |
| 66 | |
| 67 int64_t QuicBandwidth::ToBitsPerSecond() const { | |
| 68 return bits_per_second_; | |
| 69 } | |
| 70 | |
| 71 int64_t QuicBandwidth::ToKBitsPerSecond() const { | |
| 72 return bits_per_second_ / 1000; | |
| 73 } | |
| 74 | |
| 75 int64_t QuicBandwidth::ToBytesPerSecond() const { | |
| 76 return bits_per_second_ / 8; | |
| 77 } | |
| 78 | |
| 79 int64_t QuicBandwidth::ToKBytesPerSecond() const { | |
| 80 return bits_per_second_ / 8000; | |
| 81 } | |
| 82 | |
| 83 QuicByteCount QuicBandwidth::ToBytesPerPeriod( | |
| 84 QuicTime::Delta time_period) const { | |
| 85 return ToBytesPerSecond() * time_period.ToMicroseconds() / | |
| 86 kNumMicrosPerSecond; | |
| 87 } | |
| 88 | |
| 89 int64_t QuicBandwidth::ToKBytesPerPeriod(QuicTime::Delta time_period) const { | |
| 90 return ToKBytesPerSecond() * time_period.ToMicroseconds() / | |
| 91 kNumMicrosPerSecond; | |
| 92 } | |
| 93 | |
| 94 bool QuicBandwidth::IsZero() const { | |
| 95 return (bits_per_second_ == 0); | |
| 96 } | |
| 97 | |
| 98 QuicTime::Delta QuicBandwidth::TransferTime(QuicByteCount bytes) const { | |
| 99 if (bits_per_second_ == 0) { | |
| 100 return QuicTime::Delta::Zero(); | |
| 101 } | |
| 102 return QuicTime::Delta::FromMicroseconds(bytes * 8 * kNumMicrosPerSecond / | |
| 103 bits_per_second_); | |
| 104 } | |
| 105 | |
| 106 } // namespace net | |
| OLD | NEW |