| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // QuicBandwidth represents a bandwidth, stored in bits per second resolution. | 5 // QuicBandwidth represents a bandwidth, stored in bits per second resolution. |
| 6 | 6 |
| 7 #ifndef NET_QUIC_CORE_QUIC_BANDWIDTH_H_ | 7 #ifndef NET_QUIC_CORE_QUIC_BANDWIDTH_H_ |
| 8 #define NET_QUIC_CORE_QUIC_BANDWIDTH_H_ | 8 #define NET_QUIC_CORE_QUIC_BANDWIDTH_H_ |
| 9 | 9 |
| 10 #include <cmath> | 10 #include <cmath> |
| 11 #include <cstdint> | 11 #include <cstdint> |
| 12 #include <limits> |
| 12 #include <ostream> | 13 #include <ostream> |
| 13 | 14 |
| 14 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "net/quic/core/quic_constants.h" |
| 15 #include "net/quic/core/quic_time.h" | 17 #include "net/quic/core/quic_time.h" |
| 16 #include "net/quic/core/quic_types.h" | 18 #include "net/quic/core/quic_types.h" |
| 17 #include "net/quic/platform/api/quic_export.h" | 19 #include "net/quic/platform/api/quic_export.h" |
| 18 | 20 |
| 19 namespace net { | 21 namespace net { |
| 20 | 22 |
| 21 class QUIC_EXPORT_PRIVATE QuicBandwidth { | 23 class QUIC_EXPORT_PRIVATE QuicBandwidth { |
| 22 public: | 24 public: |
| 23 // Creates a new QuicBandwidth with an internal value of 0. | 25 // Creates a new QuicBandwidth with an internal value of 0. |
| 24 static QuicBandwidth Zero(); | 26 static constexpr QuicBandwidth Zero() { return QuicBandwidth(0); } |
| 25 | 27 |
| 26 // Creates a new QuicBandwidth with an internal value of INT64_MAX. | 28 // Creates a new QuicBandwidth with an internal value of INT64_MAX. |
| 27 static QuicBandwidth Infinite(); | 29 static constexpr QuicBandwidth Infinite() { |
| 30 return QuicBandwidth(std::numeric_limits<int64_t>::max()); |
| 31 } |
| 28 | 32 |
| 29 // Create a new QuicBandwidth holding the bits per second. | 33 // Create a new QuicBandwidth holding the bits per second. |
| 30 static QuicBandwidth FromBitsPerSecond(int64_t bits_per_second); | 34 static constexpr QuicBandwidth FromBitsPerSecond(int64_t bits_per_second) { |
| 35 return QuicBandwidth(bits_per_second); |
| 36 } |
| 31 | 37 |
| 32 // Create a new QuicBandwidth holding the kilo bits per second. | 38 // Create a new QuicBandwidth holding the kilo bits per second. |
| 33 static QuicBandwidth FromKBitsPerSecond(int64_t k_bits_per_second); | 39 static constexpr QuicBandwidth FromKBitsPerSecond(int64_t k_bits_per_second) { |
| 40 return QuicBandwidth(k_bits_per_second * 1000); |
| 41 } |
| 34 | 42 |
| 35 // Create a new QuicBandwidth holding the bytes per second. | 43 // Create a new QuicBandwidth holding the bytes per second. |
| 36 static QuicBandwidth FromBytesPerSecond(int64_t bytes_per_second); | 44 static constexpr QuicBandwidth FromBytesPerSecond(int64_t bytes_per_second) { |
| 45 return QuicBandwidth(bytes_per_second * 8); |
| 46 } |
| 37 | 47 |
| 38 // Create a new QuicBandwidth holding the kilo bytes per second. | 48 // Create a new QuicBandwidth holding the kilo bytes per second. |
| 39 static QuicBandwidth FromKBytesPerSecond(int64_t k_bytes_per_second); | 49 static constexpr QuicBandwidth FromKBytesPerSecond( |
| 50 int64_t k_bytes_per_second) { |
| 51 return QuicBandwidth(k_bytes_per_second * 8000); |
| 52 } |
| 40 | 53 |
| 41 // Create a new QuicBandwidth based on the bytes per the elapsed delta. | 54 // Create a new QuicBandwidth based on the bytes per the elapsed delta. |
| 42 static QuicBandwidth FromBytesAndTimeDelta(QuicByteCount bytes, | 55 static inline QuicBandwidth FromBytesAndTimeDelta(QuicByteCount bytes, |
| 43 QuicTime::Delta delta); | 56 QuicTime::Delta delta) { |
| 57 return QuicBandwidth((bytes * kNumMicrosPerSecond) / |
| 58 delta.ToMicroseconds() * 8); |
| 59 } |
| 44 | 60 |
| 45 int64_t ToBitsPerSecond() const; | 61 inline int64_t ToBitsPerSecond() const { return bits_per_second_; } |
| 46 | 62 |
| 47 int64_t ToKBitsPerSecond() const; | 63 inline int64_t ToKBitsPerSecond() const { return bits_per_second_ / 1000; } |
| 48 | 64 |
| 49 int64_t ToBytesPerSecond() const; | 65 inline int64_t ToBytesPerSecond() const { return bits_per_second_ / 8; } |
| 50 | 66 |
| 51 int64_t ToKBytesPerSecond() const; | 67 inline int64_t ToKBytesPerSecond() const { return bits_per_second_ / 8000; } |
| 52 | 68 |
| 53 QuicByteCount ToBytesPerPeriod(QuicTime::Delta time_period) const; | 69 inline QuicByteCount ToBytesPerPeriod(QuicTime::Delta time_period) const { |
| 70 return ToBytesPerSecond() * time_period.ToMicroseconds() / |
| 71 kNumMicrosPerSecond; |
| 72 } |
| 54 | 73 |
| 55 int64_t ToKBytesPerPeriod(QuicTime::Delta time_period) const; | 74 inline int64_t ToKBytesPerPeriod(QuicTime::Delta time_period) const { |
| 75 return ToKBytesPerSecond() * time_period.ToMicroseconds() / |
| 76 kNumMicrosPerSecond; |
| 77 } |
| 56 | 78 |
| 57 bool IsZero() const; | 79 inline bool IsZero() const { return bits_per_second_ == 0; } |
| 58 | 80 |
| 59 QuicTime::Delta TransferTime(QuicByteCount bytes) const; | 81 inline QuicTime::Delta TransferTime(QuicByteCount bytes) const { |
| 82 if (bits_per_second_ == 0) { |
| 83 return QuicTime::Delta::Zero(); |
| 84 } |
| 85 return QuicTime::Delta::FromMicroseconds(bytes * 8 * kNumMicrosPerSecond / |
| 86 bits_per_second_); |
| 87 } |
| 60 | 88 |
| 61 std::string ToDebugValue() const; | 89 std::string ToDebugValue() const; |
| 62 | 90 |
| 63 private: | 91 private: |
| 64 explicit QuicBandwidth(int64_t bits_per_second); | 92 explicit constexpr QuicBandwidth(int64_t bits_per_second) |
| 93 : bits_per_second_(bits_per_second >= 0 ? bits_per_second : 0) {} |
| 94 |
| 65 int64_t bits_per_second_; | 95 int64_t bits_per_second_; |
| 66 | 96 |
| 67 friend QuicBandwidth operator+(QuicBandwidth lhs, QuicBandwidth rhs); | 97 friend QuicBandwidth operator+(QuicBandwidth lhs, QuicBandwidth rhs); |
| 68 friend QuicBandwidth operator-(QuicBandwidth lhs, QuicBandwidth rhs); | 98 friend QuicBandwidth operator-(QuicBandwidth lhs, QuicBandwidth rhs); |
| 69 friend QuicBandwidth operator*(QuicBandwidth lhs, float factor); | 99 friend QuicBandwidth operator*(QuicBandwidth lhs, float factor); |
| 70 }; | 100 }; |
| 71 | 101 |
| 72 // Non-member relational operators for QuicBandwidth. | 102 // Non-member relational operators for QuicBandwidth. |
| 73 inline bool operator==(QuicBandwidth lhs, QuicBandwidth rhs) { | 103 inline bool operator==(QuicBandwidth lhs, QuicBandwidth rhs) { |
| 74 return lhs.ToBitsPerSecond() == rhs.ToBitsPerSecond(); | 104 return lhs.ToBitsPerSecond() == rhs.ToBitsPerSecond(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 142 |
| 113 // Override stream output operator for gtest. | 143 // Override stream output operator for gtest. |
| 114 inline std::ostream& operator<<(std::ostream& output, | 144 inline std::ostream& operator<<(std::ostream& output, |
| 115 const QuicBandwidth bandwidth) { | 145 const QuicBandwidth bandwidth) { |
| 116 output << bandwidth.ToDebugValue(); | 146 output << bandwidth.ToDebugValue(); |
| 117 return output; | 147 return output; |
| 118 } | 148 } |
| 119 | 149 |
| 120 } // namespace net | 150 } // namespace net |
| 121 #endif // NET_QUIC_CORE_QUIC_BANDWIDTH_H_ | 151 #endif // NET_QUIC_CORE_QUIC_BANDWIDTH_H_ |
| OLD | NEW |