Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / net / quic / quic_unacked_packet_map.h
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.
4
5 #ifndef NET_QUIC_QUIC_UNACKED_PACKET_MAP_H_
6 #define NET_QUIC_QUIC_UNACKED_PACKET_MAP_H_
7
8 #include "net/base/linked_hash_map.h"
9 #include "net/quic/quic_protocol.h"
10
11 namespace net {
12
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 {
17  public:
18   struct NET_EXPORT_PRIVATE TransmissionInfo {
19     // Used by STL when assinging into a map.
20     TransmissionInfo();
21
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);
27
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);
34
35     RetransmittableFrames* retransmittable_frames;
36     QuicSequenceNumberLength sequence_number_length;
37     // Zero when the packet is serialized, non-zero once it's sent.
38     QuicTime sent_time;
39     // Zero when the packet is serialized, non-zero once it's sent.
40     QuicByteCount bytes_sent;
41     size_t nack_count;
42     // Stores the sequence numbers of all transmissions of this packet.
43     // Can never be null.
44     SequenceNumberSet* all_transmissions;
45     // Pending packets have not been abandoned or lost.
46     bool pending;
47   };
48
49   QuicUnackedPacketMap();
50   ~QuicUnackedPacketMap();
51
52   // Adds |serialized_packet| to the map.  Does not mark it pending.
53   void AddPacket(const SerializedPacket& serialized_packet);
54
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);
61
62   // Returns true if the packet |sequence_number| is unacked.
63   bool IsUnacked(QuicPacketSequenceNumber sequence_number) const;
64
65   // Returns true if the packet |sequence_number| is pending.
66   bool IsPending(QuicPacketSequenceNumber sequence_number) const;
67
68   // Sets the nack count to the max of the current nack count and |min_nacks|.
69   void NackPacket(QuicPacketSequenceNumber sequence_number,
70                   size_t min_nacks);
71
72   // Marks |sequence_number| as no longer pending.
73   void SetNotPending(QuicPacketSequenceNumber sequence_number);
74
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;
81
82   // Returns true if there are any unacked packets.
83   bool HasUnackedPackets() const;
84
85   // Returns true if there are any unacked packets which have retransmittable
86   // frames.
87   bool HasUnackedRetransmittableFrames() const;
88
89   // Returns the number of unacked packets which have retransmittable frames.
90   size_t GetNumRetransmittablePackets() const;
91
92   // Returns the largest sequence number that has been sent.
93   QuicPacketSequenceNumber largest_sent_packet() const {
94     return largest_sent_packet_;
95   }
96
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;
100
101   // Returns the set of sequence numbers of all unacked packets.
102   // Test only.
103   SequenceNumberSet GetUnackedPackets() const;
104
105   // Sets a packet pending, with the sent time |sent_time|.
106   void SetPending(QuicPacketSequenceNumber sequence_number,
107                   QuicTime sent_time,
108                   QuicByteCount bytes_sent);
109
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);
113
114   typedef linked_hash_map<QuicPacketSequenceNumber,
115                           TransmissionInfo> UnackedPacketMap;
116
117   typedef UnackedPacketMap::const_iterator const_iterator;
118
119   const_iterator begin() const { return unacked_packets_.begin(); }
120   const_iterator end() const { return unacked_packets_.end(); }
121
122   // Returns true if there are unacked packets that are pending.
123   bool HasPendingPackets() const;
124
125   // Returns the TransmissionInfo associated with |sequence_number|, which
126   // must be unacked.
127   const TransmissionInfo& GetTransmissionInfo(
128       QuicPacketSequenceNumber sequence_number) const;
129
130   // Returns the time that the last unacked packet was sent.
131   QuicTime GetLastPacketSentTime() const;
132
133   // Returns the time that the first pending packet was sent.
134   QuicTime GetFirstPendingPacketSentTime() const;
135
136   // Returns the number of unacked packets.
137   size_t GetNumUnackedPackets() const;
138
139   // Returns true if there are multiple packet pending.
140   bool HasMultiplePendingPackets() const;
141
142   // Returns true if there are any pending crypto packets.
143   bool HasPendingCryptoPackets() const;
144
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);
149
150   // Neuters the specified packet.  Deletes any retransmittable
151   // frames, and sets all_transmissions to only include itself.
152   void NeuterPacket(QuicPacketSequenceNumber sequence_number);
153
154  private:
155   QuicPacketSequenceNumber largest_sent_packet_;
156
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
164   // set to NULL.
165   UnackedPacketMap unacked_packets_;
166
167   size_t bytes_in_flight_;
168   // Number of outstanding crypto handshake packets.
169   size_t pending_crypto_packet_count_;
170
171   DISALLOW_COPY_AND_ASSIGN(QuicUnackedPacketMap);
172 };
173
174 }  // namespace net
175
176 #endif  // NET_QUIC_QUIC_UNACKED_PACKET_MAP_H_