quic_received_packet_manager.h revision 2385ea399aae016c0806a4f9ef3c9cfe3d2a39df
1// Copyright 2013 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// Manages the packet entropy calculation for both sent and received packets
6// for a connection.
7
8#ifndef NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_
9#define NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_
10
11#include "net/quic/quic_framer.h"
12#include "net/quic/quic_protocol.h"
13
14namespace net {
15
16namespace test {
17class QuicReceivedPacketManagerPeer;
18}  // namespace test
19
20// Records all received packets by a connection and tracks their entropy.
21// Also calculates the correct entropy for the framer when it truncates an ack
22// frame being serialized.
23class NET_EXPORT_PRIVATE QuicReceivedPacketManager :
24    public QuicReceivedEntropyHashCalculatorInterface {
25 public:
26  QuicReceivedPacketManager();
27  virtual ~QuicReceivedPacketManager();
28
29  // Updates the internal state concerning which packets have been acked.
30  void RecordPacketReceived(const QuicPacketHeader& header,
31                            QuicTime receipt_time);
32
33  // Checks if we're still waiting for the packet with |sequence_number|.
34  bool IsAwaitingPacket(QuicPacketSequenceNumber sequence_number);
35
36  // Update the |received_info| for an outgoing ack.
37  void UpdateReceivedPacketInfo(ReceivedPacketInfo* received_info,
38                                QuicTime approximate_now);
39
40  // QuicReceivedEntropyHashCalculatorInterface
41  // Called by QuicFramer, when the outgoing ack gets truncated, to recalculate
42  // the received entropy hash for the truncated ack frame.
43  virtual QuicPacketEntropyHash EntropyHash(
44      QuicPacketSequenceNumber sequence_number) const OVERRIDE;
45
46  // These two are called by OnAckFrame.
47  //
48  // Updates internal state based on |incoming_ack.received_info|.
49  void UpdatePacketInformationReceivedByPeer(const QuicAckFrame& incoming_ack);
50  // Updates internal state based on |incoming_ack.sent_info|.
51  void UpdatePacketInformationSentByPeer(const QuicAckFrame& incoming_ack);
52
53  QuicPacketSequenceNumber peer_largest_observed_packet() {
54    return peer_largest_observed_packet_;
55  }
56
57  QuicPacketSequenceNumber least_packet_awaited_by_peer() {
58    return least_packet_awaited_by_peer_;
59  }
60
61  QuicPacketSequenceNumber peer_least_packet_awaiting_ack() {
62    return peer_least_packet_awaiting_ack_;
63  }
64
65 private:
66  friend class test::QuicReceivedPacketManagerPeer;
67
68  typedef std::map<QuicPacketSequenceNumber,
69                   QuicPacketEntropyHash> ReceivedEntropyMap;
70
71  // Record the received entropy hash against |sequence_number|.
72  void RecordPacketEntropyHash(QuicPacketSequenceNumber sequence_number,
73                               QuicPacketEntropyHash entropy_hash);
74
75  // Recalculate the entropy hash and clears old packet entropies,
76  // now that the sender sent us the |entropy_hash| for packets up to,
77  // but not including, |peer_least_unacked|.
78  void RecalculateEntropyHash(QuicPacketSequenceNumber peer_least_unacked,
79                              QuicPacketEntropyHash entropy_hash);
80
81  // Deletes all missing packets before least unacked. The connection won't
82  // process any packets with sequence number before |least_unacked| that it
83  // received after this call. Returns true if there were missing packets before
84  // |least_unacked| unacked, false otherwise.
85  bool DontWaitForPacketsBefore(QuicPacketSequenceNumber least_unacked);
86
87  // TODO(satyamshekhar): Can be optimized using an interval set like data
88  // structure.
89  // Map of received sequence numbers to their corresponding entropy.
90  // Every received packet has an entry, and packets without the entropy bit set
91  // have an entropy value of 0.
92  // TODO(ianswett): When the entropy flag is off, the entropy should not be 0.
93  ReceivedEntropyMap packets_entropy_;
94
95  // Cumulative hash of entropy of all received packets.
96  QuicPacketEntropyHash packets_entropy_hash_;
97
98  // The largest sequence number cleared by RecalculateEntropyHash.
99  // Received entropy cannot be calculated for numbers less than it.
100  QuicPacketSequenceNumber largest_sequence_number_;
101
102
103  // Track some peer state so we can do less bookkeeping.
104  // Largest sequence number that the peer has observed. Mostly received,
105  // missing in case of truncated acks.
106  QuicPacketSequenceNumber peer_largest_observed_packet_;
107  // Least sequence number which the peer is still waiting for.
108  QuicPacketSequenceNumber least_packet_awaited_by_peer_;
109  // Least sequence number of the the packet sent by the peer for which it
110  // hasn't received an ack.
111  QuicPacketSequenceNumber peer_least_packet_awaiting_ack_;
112
113  // Received packet information used to produce acks.
114  ReceivedPacketInfo received_info_;
115
116  // The time we received the largest_observed sequence number, or zero if
117  // no sequence numbers have been received since UpdateReceivedPacketInfo.
118  // Needed for calculating delta_time_largest_observed.
119  QuicTime time_largest_observed_;
120};
121
122}  // namespace net
123
124#endif  // NET_QUIC_QUIC_RECEIVED_PACKET_MANAGER_H_
125