blob: b3009ce5c7aaa0e61e9d579a3a4252b9e44caf2b [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
rchf1c9d562019-03-21 07:49:38 -07005#include <algorithm>
6
QUICHE teama6ef0a62019-03-07 20:34:33 -05007#include "net/third_party/quiche/src/quic/core/quic_packet_number.h"
8
9namespace quic {
10
11QuicPacketNumber::QuicPacketNumber()
12 : packet_number_(UninitializedPacketNumber()) {}
13
14QuicPacketNumber::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
20void QuicPacketNumber::Clear() {
21 packet_number_ = UninitializedPacketNumber();
22}
23
QUICHE teamc264e362019-03-19 14:21:06 -070024void 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 teama6ef0a62019-03-07 20:34:33 -050035uint64_t QuicPacketNumber::Hash() const {
36 DCHECK(IsInitialized());
37 return packet_number_;
38}
39
40uint64_t QuicPacketNumber::ToUint64() const {
41 DCHECK(IsInitialized());
42 return packet_number_;
43}
44
45bool QuicPacketNumber::IsInitialized() const {
46 return packet_number_ != UninitializedPacketNumber();
47}
48
49QuicPacketNumber& QuicPacketNumber::operator++() {
50#ifndef NDEBUG
51 DCHECK(IsInitialized());
QUICHE team577718a2019-03-20 09:00:59 -070052 DCHECK_LT(ToUint64(), std::numeric_limits<uint64_t>::max() - 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -050053#endif
54 packet_number_++;
55 return *this;
56}
57
58QuicPacketNumber QuicPacketNumber::operator++(int) {
59#ifndef NDEBUG
60 DCHECK(IsInitialized());
QUICHE team577718a2019-03-20 09:00:59 -070061 DCHECK_LT(ToUint64(), std::numeric_limits<uint64_t>::max() - 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -050062#endif
63 QuicPacketNumber previous(*this);
64 packet_number_++;
65 return previous;
66}
67
68QuicPacketNumber& QuicPacketNumber::operator--() {
69#ifndef NDEBUG
70 DCHECK(IsInitialized());
QUICHE team577718a2019-03-20 09:00:59 -070071 DCHECK_GE(ToUint64(), 1UL);
QUICHE teama6ef0a62019-03-07 20:34:33 -050072#endif
73 packet_number_--;
74 return *this;
75}
76
77QuicPacketNumber QuicPacketNumber::operator--(int) {
78#ifndef NDEBUG
79 DCHECK(IsInitialized());
QUICHE team577718a2019-03-20 09:00:59 -070080 DCHECK_GE(ToUint64(), 1UL);
QUICHE teama6ef0a62019-03-07 20:34:33 -050081#endif
82 QuicPacketNumber previous(*this);
83 packet_number_--;
84 return previous;
85}
86
87QuicPacketNumber& QuicPacketNumber::operator+=(uint64_t delta) {
88#ifndef NDEBUG
89 DCHECK(IsInitialized());
QUICHE team577718a2019-03-20 09:00:59 -070090 DCHECK_GT(std::numeric_limits<uint64_t>::max() - ToUint64(), delta);
QUICHE teama6ef0a62019-03-07 20:34:33 -050091#endif
92 packet_number_ += delta;
93 return *this;
94}
95
96QuicPacketNumber& QuicPacketNumber::operator-=(uint64_t delta) {
97#ifndef NDEBUG
98 DCHECK(IsInitialized());
QUICHE team577718a2019-03-20 09:00:59 -070099 DCHECK_GE(ToUint64(), delta);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500100#endif
101 packet_number_ -= delta;
102 return *this;
103}
104
105std::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
115uint64_t QuicPacketNumber::UninitializedPacketNumber() {
QUICHE team577718a2019-03-20 09:00:59 -0700116 return std::numeric_limits<uint64_t>::max();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500117}
118
119} // namespace quic