1 // Copyright 2014 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.
5 #ifndef NET_QUIC_QUIC_UNACKED_PACKET_MAP_H_
6 #define NET_QUIC_QUIC_UNACKED_PACKET_MAP_H_
8 #include "net/base/linked_hash_map.h"
9 #include "net/quic/quic_protocol.h"
13 // Class which tracks unacked packets, including those packets which are
14 // currently pending, and the relationship between packets which
15 // contain the same data (via retransmissions)
16 class NET_EXPORT_PRIVATE QuicUnackedPacketMap
{
18 struct NET_EXPORT_PRIVATE TransmissionInfo
{
19 // Used by STL when assinging into a map.
22 // Constructs a Transmission with a new all_tranmissions set
23 // containing |sequence_number|.
24 TransmissionInfo(RetransmittableFrames
* retransmittable_frames
,
25 QuicPacketSequenceNumber sequence_number
,
26 QuicSequenceNumberLength sequence_number_length
);
28 // Constructs a Transmission with the specified |all_tranmissions| set
29 // and inserts |sequence_number| into it.
30 TransmissionInfo(RetransmittableFrames
* retransmittable_frames
,
31 QuicPacketSequenceNumber sequence_number
,
32 QuicSequenceNumberLength sequence_number_length
,
33 SequenceNumberSet
* all_transmissions
);
35 RetransmittableFrames
* retransmittable_frames
;
36 QuicSequenceNumberLength sequence_number_length
;
37 // Zero when the packet is serialized, non-zero once it's sent.
39 // Zero when the packet is serialized, non-zero once it's sent.
40 QuicByteCount bytes_sent
;
42 // Stores the sequence numbers of all transmissions of this packet.
44 SequenceNumberSet
* all_transmissions
;
45 // Pending packets have not been abandoned or lost.
49 QuicUnackedPacketMap();
50 ~QuicUnackedPacketMap();
52 // Adds |serialized_packet| to the map. Does not mark it pending.
53 void AddPacket(const SerializedPacket
& serialized_packet
);
55 // Called when a packet is retransmitted with a new sequence number.
56 // |old_sequence_number| will remain unacked, but will have no
57 // retransmittable data associated with it. |new_sequence_number| will
58 // be both unacked and associated with retransmittable data.
59 void OnRetransmittedPacket(QuicPacketSequenceNumber old_sequence_number
,
60 QuicPacketSequenceNumber new_sequence_number
);
62 // Returns true if the packet |sequence_number| is unacked.
63 bool IsUnacked(QuicPacketSequenceNumber sequence_number
) const;
65 // Returns true if the packet |sequence_number| is pending.
66 bool IsPending(QuicPacketSequenceNumber sequence_number
) const;
68 // Sets the nack count to the max of the current nack count and |min_nacks|.
69 void NackPacket(QuicPacketSequenceNumber sequence_number
,
72 // Marks |sequence_number| as no longer pending.
73 void SetNotPending(QuicPacketSequenceNumber sequence_number
);
75 // Returns true if the unacked packet |sequence_number| has retransmittable
76 // frames. This will return false if the packet has been acked, if a
77 // previous transmission of this packet was ACK'd, or if this packet has been
78 // retransmitted as with different sequence number, or if the packet never
79 // had any retransmittable packets in the first place.
80 bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number
) const;
82 // Returns true if there are any unacked packets.
83 bool HasUnackedPackets() const;
85 // Returns true if there are any unacked packets which have retransmittable
87 bool HasUnackedRetransmittableFrames() const;
89 // Returns the number of unacked packets which have retransmittable frames.
90 size_t GetNumRetransmittablePackets() const;
92 // Returns the largest sequence number that has been sent.
93 QuicPacketSequenceNumber
largest_sent_packet() const {
94 return largest_sent_packet_
;
97 // Returns the smallest sequence number of a serialized packet which has not
98 // been acked by the peer. If there are no unacked packets, returns 0.
99 QuicPacketSequenceNumber
GetLeastUnackedSentPacket() const;
101 // Returns the set of sequence numbers of all unacked packets.
103 SequenceNumberSet
GetUnackedPackets() const;
105 // Sets a packet pending, with the sent time |sent_time|.
106 void SetPending(QuicPacketSequenceNumber sequence_number
,
108 QuicByteCount bytes_sent
);
110 // Clears up to |num_to_clear| previous transmissions in order to make room
111 // in the ack frame for new acks.
112 void ClearPreviousRetransmissions(size_t num_to_clear
);
114 typedef linked_hash_map
<QuicPacketSequenceNumber
,
115 TransmissionInfo
> UnackedPacketMap
;
117 typedef UnackedPacketMap::const_iterator const_iterator
;
119 const_iterator
begin() const { return unacked_packets_
.begin(); }
120 const_iterator
end() const { return unacked_packets_
.end(); }
122 // Returns true if there are unacked packets that are pending.
123 bool HasPendingPackets() const;
125 // Returns the TransmissionInfo associated with |sequence_number|, which
127 const TransmissionInfo
& GetTransmissionInfo(
128 QuicPacketSequenceNumber sequence_number
) const;
130 // Returns the time that the last unacked packet was sent.
131 QuicTime
GetLastPacketSentTime() const;
133 // Returns the time that the first pending packet was sent.
134 QuicTime
GetFirstPendingPacketSentTime() const;
136 // Returns the number of unacked packets.
137 size_t GetNumUnackedPackets() const;
139 // Returns true if there are multiple packet pending.
140 bool HasMultiplePendingPackets() const;
142 // Returns true if there are any pending crypto packets.
143 bool HasPendingCryptoPackets() const;
145 // Removes entries from the unacked packet map, and deletes
146 // the retransmittable frames associated with the packet.
147 // Does not remove any previous or subsequent transmissions of this packet.
148 void RemovePacket(QuicPacketSequenceNumber sequence_number
);
150 // Neuters the specified packet. Deletes any retransmittable
151 // frames, and sets all_transmissions to only include itself.
152 void NeuterPacket(QuicPacketSequenceNumber sequence_number
);
155 QuicPacketSequenceNumber largest_sent_packet_
;
157 // Newly serialized retransmittable and fec packets are added to this map,
158 // which contains owning pointers to any contained frames. If a packet is
159 // retransmitted, this map will contain entries for both the old and the new
160 // packet. The old packet's retransmittable frames entry will be NULL, while
161 // the new packet's entry will contain the frames to retransmit.
162 // If the old packet is acked before the new packet, then the old entry will
163 // be removed from the map and the new entry's retransmittable frames will be
165 UnackedPacketMap unacked_packets_
;
167 size_t bytes_in_flight_
;
168 // Number of outstanding crypto handshake packets.
169 size_t pending_crypto_packet_count_
;
171 DISALLOW_COPY_AND_ASSIGN(QuicUnackedPacketMap
);
176 #endif // NET_QUIC_QUIC_UNACKED_PACKET_MAP_H_