QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2019 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 | |
rch | f1c9d56 | 2019-03-21 07:49:38 -0700 | [diff] [blame] | 5 | #include <algorithm> |
| 6 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7 | #include "net/third_party/quiche/src/quic/core/quic_packet_number.h" |
| 8 | |
| 9 | namespace quic { |
| 10 | |
| 11 | QuicPacketNumber::QuicPacketNumber() |
| 12 | : packet_number_(UninitializedPacketNumber()) {} |
| 13 | |
| 14 | QuicPacketNumber::QuicPacketNumber(uint64_t packet_number) |
| 15 | : packet_number_(packet_number) { |
| 16 | DCHECK_NE(UninitializedPacketNumber(), packet_number) |
| 17 | << "Use default constructor for uninitialized packet number"; |
| 18 | } |
| 19 | |
| 20 | void QuicPacketNumber::Clear() { |
| 21 | packet_number_ = UninitializedPacketNumber(); |
| 22 | } |
| 23 | |
QUICHE team | c264e36 | 2019-03-19 14:21:06 -0700 | [diff] [blame] | 24 | void QuicPacketNumber::UpdateMax(QuicPacketNumber new_value) { |
| 25 | if (!new_value.IsInitialized()) { |
| 26 | return; |
| 27 | } |
| 28 | if (!IsInitialized()) { |
| 29 | packet_number_ = new_value.ToUint64(); |
| 30 | } else { |
| 31 | packet_number_ = std::max(packet_number_, new_value.ToUint64()); |
| 32 | } |
| 33 | } |
| 34 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 35 | uint64_t QuicPacketNumber::Hash() const { |
| 36 | DCHECK(IsInitialized()); |
| 37 | return packet_number_; |
| 38 | } |
| 39 | |
| 40 | uint64_t QuicPacketNumber::ToUint64() const { |
| 41 | DCHECK(IsInitialized()); |
| 42 | return packet_number_; |
| 43 | } |
| 44 | |
| 45 | bool QuicPacketNumber::IsInitialized() const { |
| 46 | return packet_number_ != UninitializedPacketNumber(); |
| 47 | } |
| 48 | |
| 49 | QuicPacketNumber& QuicPacketNumber::operator++() { |
| 50 | #ifndef NDEBUG |
| 51 | DCHECK(IsInitialized()); |
QUICHE team | 577718a | 2019-03-20 09:00:59 -0700 | [diff] [blame] | 52 | DCHECK_LT(ToUint64(), std::numeric_limits<uint64_t>::max() - 1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 53 | #endif |
| 54 | packet_number_++; |
| 55 | return *this; |
| 56 | } |
| 57 | |
| 58 | QuicPacketNumber QuicPacketNumber::operator++(int) { |
| 59 | #ifndef NDEBUG |
| 60 | DCHECK(IsInitialized()); |
QUICHE team | 577718a | 2019-03-20 09:00:59 -0700 | [diff] [blame] | 61 | DCHECK_LT(ToUint64(), std::numeric_limits<uint64_t>::max() - 1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 62 | #endif |
| 63 | QuicPacketNumber previous(*this); |
| 64 | packet_number_++; |
| 65 | return previous; |
| 66 | } |
| 67 | |
| 68 | QuicPacketNumber& QuicPacketNumber::operator--() { |
| 69 | #ifndef NDEBUG |
| 70 | DCHECK(IsInitialized()); |
QUICHE team | 577718a | 2019-03-20 09:00:59 -0700 | [diff] [blame] | 71 | DCHECK_GE(ToUint64(), 1UL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 72 | #endif |
| 73 | packet_number_--; |
| 74 | return *this; |
| 75 | } |
| 76 | |
| 77 | QuicPacketNumber QuicPacketNumber::operator--(int) { |
| 78 | #ifndef NDEBUG |
| 79 | DCHECK(IsInitialized()); |
QUICHE team | 577718a | 2019-03-20 09:00:59 -0700 | [diff] [blame] | 80 | DCHECK_GE(ToUint64(), 1UL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 81 | #endif |
| 82 | QuicPacketNumber previous(*this); |
| 83 | packet_number_--; |
| 84 | return previous; |
| 85 | } |
| 86 | |
| 87 | QuicPacketNumber& QuicPacketNumber::operator+=(uint64_t delta) { |
| 88 | #ifndef NDEBUG |
| 89 | DCHECK(IsInitialized()); |
QUICHE team | 577718a | 2019-03-20 09:00:59 -0700 | [diff] [blame] | 90 | DCHECK_GT(std::numeric_limits<uint64_t>::max() - ToUint64(), delta); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 91 | #endif |
| 92 | packet_number_ += delta; |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | QuicPacketNumber& QuicPacketNumber::operator-=(uint64_t delta) { |
| 97 | #ifndef NDEBUG |
| 98 | DCHECK(IsInitialized()); |
QUICHE team | 577718a | 2019-03-20 09:00:59 -0700 | [diff] [blame] | 99 | DCHECK_GE(ToUint64(), delta); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 100 | #endif |
| 101 | packet_number_ -= delta; |
| 102 | return *this; |
| 103 | } |
| 104 | |
| 105 | std::ostream& operator<<(std::ostream& os, const QuicPacketNumber& p) { |
| 106 | if (p.IsInitialized()) { |
| 107 | os << p.packet_number_; |
| 108 | } else { |
| 109 | os << "uninitialized"; |
| 110 | } |
| 111 | return os; |
| 112 | } |
| 113 | |
| 114 | // static |
| 115 | uint64_t QuicPacketNumber::UninitializedPacketNumber() { |
QUICHE team | 577718a | 2019-03-20 09:00:59 -0700 | [diff] [blame] | 116 | return std::numeric_limits<uint64_t>::max(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } // namespace quic |