quic_connection.h revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2012 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// The entity that handles framing writes for a Quic client or server.
6// Each QuicSession will have a connection associated with it.
7//
8// On the server side, the Dispatcher handles the raw reads, and hands off
9// packets via ProcessUdpPacket for framing and processing.
10//
11// On the client side, the Connection handles the raw reads, as well as the
12// processing.
13//
14// Note: this class is not thread-safe.
15
16#ifndef NET_QUIC_QUIC_CONNECTION_H_
17#define NET_QUIC_QUIC_CONNECTION_H_
18
19#include <list>
20#include <map>
21#include <queue>
22#include <set>
23#include <vector>
24
25#include "base/hash_tables.h"
26#include "net/base/ip_endpoint.h"
27#include "net/base/linked_hash_map.h"
28#include "net/quic/congestion_control/quic_congestion_manager.h"
29#include "net/quic/quic_blocked_writer_interface.h"
30#include "net/quic/quic_framer.h"
31#include "net/quic/quic_packet_creator.h"
32#include "net/quic/quic_packet_entropy_manager.h"
33#include "net/quic/quic_packet_generator.h"
34#include "net/quic/quic_protocol.h"
35#include "net/quic/quic_stats.h"
36
37namespace net {
38
39class QuicClock;
40class QuicConnection;
41class QuicFecGroup;
42class QuicRandom;
43
44namespace test {
45class QuicConnectionPeer;
46}  // namespace test
47
48class NET_EXPORT_PRIVATE QuicConnectionVisitorInterface {
49 public:
50  virtual ~QuicConnectionVisitorInterface() {}
51
52  // A simple visitor interface for dealing with data frames.  The session
53  // should determine if all frames will be accepted, and return true if so.
54  // If any frames can't be processed or buffered, none of the data should
55  // be used, and the callee should return false.
56  virtual bool OnPacket(const IPEndPoint& self_address,
57                        const IPEndPoint& peer_address,
58                        const QuicPacketHeader& header,
59                        const std::vector<QuicStreamFrame>& frame) = 0;
60
61  // Called when the stream is reset by the peer.
62  virtual void OnRstStream(const QuicRstStreamFrame& frame) = 0;
63
64  // Called when the connection is going away according to the peer.
65  virtual void OnGoAway(const QuicGoAwayFrame& frame) = 0;
66
67  // Called when the connection is closed either locally by the framer, or
68  // remotely by the peer.
69  virtual void ConnectionClose(QuicErrorCode error,
70                               bool from_peer) = 0;
71
72  // Called when packets are acked by the peer.
73  virtual void OnAck(const SequenceNumberSet& acked_packets) = 0;
74
75  // Called when a blocked socket becomes writable.  If all pending bytes for
76  // this visitor are consumed by the connection successfully this should
77  // return true, otherwise it should return false.
78  virtual bool OnCanWrite() = 0;
79};
80
81// Interface which gets callbacks from the QuicConnection at interesting
82// points.  Implementations must not mutate the state of the connection
83// as a result of these callbacks.
84class NET_EXPORT_PRIVATE QuicConnectionDebugVisitorInterface {
85 public:
86  virtual ~QuicConnectionDebugVisitorInterface() {}
87
88  // Called when a packet has been received, but before it is
89  // validated or parsed.
90  virtual void OnPacketReceived(const IPEndPoint& self_address,
91                                const IPEndPoint& peer_address,
92                                const QuicEncryptedPacket& packet) = 0;
93
94  // Called when the protocol version on the received packet doensn't match
95  // current protocol version of the connection.
96  virtual void OnProtocolVersionMismatch(QuicTag version) = 0;
97
98  // Called when the complete header of a packet has been parsed.
99  virtual void OnPacketHeader(const QuicPacketHeader& header) = 0;
100
101  // Called when a StreamFrame has been parsed.
102  virtual void OnStreamFrame(const QuicStreamFrame& frame) = 0;
103
104  // Called when a AckFrame has been parsed.
105  virtual void OnAckFrame(const QuicAckFrame& frame) = 0;
106
107  // Called when a CongestionFeedbackFrame has been parsed.
108  virtual void OnCongestionFeedbackFrame(
109      const QuicCongestionFeedbackFrame& frame) = 0;
110
111  // Called when a RstStreamFrame has been parsed.
112  virtual void OnRstStreamFrame(const QuicRstStreamFrame& frame) = 0;
113
114  // Called when a ConnectionCloseFrame has been parsed.
115  virtual void OnConnectionCloseFrame(
116      const QuicConnectionCloseFrame& frame) = 0;
117
118  // Called when a public reset packet has been received.
119  virtual void OnPublicResetPacket(const QuicPublicResetPacket& packet) = 0;
120
121  // Called when a version negotiation packet has been received.
122  virtual void OnVersionNegotiationPacket(
123      const QuicVersionNegotiationPacket& packet) = 0;
124
125  // Called after a packet has been successfully parsed which results
126  // in the revival of a packet via FEC.
127  virtual void OnRevivedPacket(const QuicPacketHeader& revived_header,
128                               base::StringPiece payload) = 0;
129};
130
131class NET_EXPORT_PRIVATE QuicConnectionHelperInterface {
132 public:
133  virtual ~QuicConnectionHelperInterface() {}
134
135  // Sets the QuicConnection to be used by this helper.  This method
136  // must only be called once.
137  virtual void SetConnection(QuicConnection* connection) = 0;
138
139  // Returns a QuicClock to be used for all time related functions.
140  virtual const QuicClock* GetClock() const = 0;
141
142  // Returns a QuicRandom to be used for all random number related functions.
143  virtual QuicRandom* GetRandomGenerator() = 0;
144
145  // Sends the packet out to the peer, possibly simulating packet
146  // loss if FLAGS_fake_packet_loss_percentage is set.  If the write
147  // succeeded, returns the number of bytes written.  If the write
148  // failed, returns -1 and the error code will be copied to |*error|.
149  virtual int WritePacketToWire(const QuicEncryptedPacket& packet,
150                                int* error) = 0;
151
152  // Returns true if the helper buffers and subsequently rewrites data
153  // when an attempt to write results in the underlying socket becoming
154  // write blocked.
155  virtual bool IsWriteBlockedDataBuffered() = 0;
156
157  // Returns true if |error| represents a write-block error code such
158  // as EAGAIN or ERR_IO_PENDING.
159  virtual bool IsWriteBlocked(int error) = 0;
160
161  // Sets up an alarm to retransmit a packet if we haven't received an ack
162  // in the expected time frame.  Implementations must invoke
163  // OnRetransmissionAlarm when the alarm fires.  Implementations must also
164  // handle the case where |this| is deleted before the alarm fires.  If the
165  // alarm is already set, this call is a no-op.
166  virtual void SetRetransmissionAlarm(QuicTime::Delta delay) = 0;
167
168  // Sets an alarm to send packets at |alarm_time|.  Implementations must
169  // invoke OnCanWrite when the alarm fires.  Implementations must also
170  // handle the case where |this| is deleted before the alarm fires.
171  virtual void SetSendAlarm(QuicTime alarm_time) = 0;
172
173  // Sets an alarm which fires when the connection may have timed out.
174  // Implementations must call CheckForTimeout() and then reregister the alarm
175  // if the connection has not yet timed out.
176  virtual void SetTimeoutAlarm(QuicTime::Delta delay) = 0;
177
178  // Returns true if a send alarm is currently set.
179  virtual bool IsSendAlarmSet() = 0;
180
181  // If a send alarm is currently set, this method unregisters it.  If
182  // no send alarm is set, it does nothing.
183  virtual void UnregisterSendAlarmIfRegistered() = 0;
184
185  // Sets an alarm which fires when an Ack may need to be sent.
186  // Implementations must call SendAck() when the alarm fires.
187  // If the alarm is already registered for a shorter timeout, this call is a
188  // no-op.
189  virtual void SetAckAlarm(QuicTime::Delta delay) = 0;
190
191  // Clears the ack alarm if it was set.  If it was not set, this is a no-op.
192  virtual void ClearAckAlarm() = 0;
193};
194
195class NET_EXPORT_PRIVATE QuicConnection
196    : public QuicFramerVisitorInterface,
197      public QuicBlockedWriterInterface,
198      public QuicPacketGenerator::DelegateInterface {
199 public:
200  enum Force {
201    NO_FORCE,
202    FORCE
203  };
204
205  enum RetransmissionType {
206    INITIAL_ENCRYPTION_ONLY,
207    ALL_PACKETS
208  };
209
210  // Constructs a new QuicConnection for the specified |guid| and |address|.
211  // |helper| will be owned by this connection.
212  QuicConnection(QuicGuid guid,
213                 IPEndPoint address,
214                 QuicConnectionHelperInterface* helper,
215                 bool is_server);
216  virtual ~QuicConnection();
217
218  static void DeleteEnclosedFrame(QuicFrame* frame);
219
220  // Send the data payload to the peer.
221  // Returns a pair with the number of bytes consumed from data, and a boolean
222  // indicating if the fin bit was consumed.  This does not indicate the data
223  // has been sent on the wire: it may have been turned into a packet and queued
224  // if the socket was unexpectedly blocked.
225  QuicConsumedData SendStreamData(QuicStreamId id,
226                                  base::StringPiece data,
227                                  QuicStreamOffset offset,
228                                  bool fin);
229  // Send a stream reset frame to the peer.
230  virtual void SendRstStream(QuicStreamId id,
231                             QuicRstStreamErrorCode error);
232
233  // Sends the connection close packet without affecting the state of the
234  // connection.  This should only be called if the session is actively being
235  // destroyed: otherwise call SendConnectionCloseWithDetails instead.
236  virtual void SendConnectionClosePacket(QuicErrorCode error,
237                                         const std::string& details);
238
239  // Sends a connection close frame to the peer, and closes the connection by
240  // calling CloseConnection(notifying the visitor as it does so).
241  virtual void SendConnectionClose(QuicErrorCode error);
242  virtual void SendConnectionCloseWithDetails(QuicErrorCode error,
243                                              const std::string& details);
244  // Notifies the visitor of the close and marks the connection as disconnected.
245  void CloseConnection(QuicErrorCode error, bool from_peer);
246  virtual void SendGoAway(QuicErrorCode error,
247                          QuicStreamId last_good_stream_id,
248                          const std::string& reason);
249
250  // Returns statistics tracked for this connection.
251  const QuicConnectionStats& GetStats();
252
253  // Processes an incoming UDP packet (consisting of a QuicEncryptedPacket) from
254  // the peer.  If processing this packet permits a packet to be revived from
255  // its FEC group that packet will be revived and processed.
256  virtual void ProcessUdpPacket(const IPEndPoint& self_address,
257                                const IPEndPoint& peer_address,
258                                const QuicEncryptedPacket& packet);
259
260  // QuicBlockedWriterInterface
261  // Called when the underlying connection becomes writable to allow
262  // queued writes to happen.  Returns false if the socket has become blocked.
263  virtual bool OnCanWrite() OVERRIDE;
264
265  // Do any work which logically would be done in OnPacket but can not be
266  // safely done until the packet is validated.  Returns true if the packet
267  // can be handled, false otherwise.
268  bool ProcessValidatedPacket();
269
270  QuicTag version() const { return quic_version_; }
271
272  // From QuicFramerVisitorInterface
273  virtual void OnError(QuicFramer* framer) OVERRIDE;
274  virtual bool OnProtocolVersionMismatch(QuicTag received_version) OVERRIDE;
275  virtual void OnPacket() OVERRIDE;
276  virtual void OnPublicResetPacket(
277      const QuicPublicResetPacket& packet) OVERRIDE;
278  virtual void OnVersionNegotiationPacket(
279      const QuicVersionNegotiationPacket& packet) OVERRIDE;
280  virtual void OnRevivedPacket() OVERRIDE;
281  virtual bool OnPacketHeader(const QuicPacketHeader& header) OVERRIDE;
282  virtual void OnFecProtectedPayload(base::StringPiece payload) OVERRIDE;
283  virtual bool OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE;
284  virtual bool OnAckFrame(const QuicAckFrame& frame) OVERRIDE;
285  virtual bool OnCongestionFeedbackFrame(
286      const QuicCongestionFeedbackFrame& frame) OVERRIDE;
287  virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE;
288  virtual bool OnConnectionCloseFrame(
289      const QuicConnectionCloseFrame& frame) OVERRIDE;
290  virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE;
291  virtual void OnFecData(const QuicFecData& fec) OVERRIDE;
292  virtual void OnPacketComplete() OVERRIDE;
293
294  // QuicPacketGenerator::DelegateInterface
295  virtual bool CanWrite(
296      Retransmission is_retransmission,
297      HasRetransmittableData has_retransmittable_data) OVERRIDE;
298  virtual QuicAckFrame* CreateAckFrame() OVERRIDE;
299  virtual QuicCongestionFeedbackFrame* CreateFeedbackFrame() OVERRIDE;
300  virtual bool OnSerializedPacket(const SerializedPacket& packet) OVERRIDE;
301
302  // Accessors
303  void set_visitor(QuicConnectionVisitorInterface* visitor) {
304    visitor_ = visitor;
305  }
306  void set_debug_visitor(QuicConnectionDebugVisitorInterface* debug_visitor) {
307    debug_visitor_ = debug_visitor;
308  }
309  const IPEndPoint& self_address() const { return self_address_; }
310  const IPEndPoint& peer_address() const { return peer_address_; }
311  QuicGuid guid() const { return guid_; }
312  const QuicClock* clock() const { return clock_; }
313  QuicRandom* random_generator() const { return random_generator_; }
314
315  // Updates the internal state concerning which packets have been acked.
316  void RecordPacketReceived(const QuicPacketHeader& header);
317
318  // Called by a RetransmissionAlarm when the timer goes off.  If the peer
319  // appears to be sending truncated acks, this returns false to indicate
320  // failure, otherwise it calls MaybeRetransmitPacket and returns true.
321  bool MaybeRetransmitPacketForRTO(QuicPacketSequenceNumber sequence_number);
322
323  // Called to retransmit a packet, in the case a packet was sufficiently
324  // nacked by the peer, or not acked within the time out window.
325  void RetransmitPacket(QuicPacketSequenceNumber sequence_number);
326
327  QuicPacketCreator::Options* options() { return packet_creator_.options(); }
328
329  bool connected() { return connected_; }
330
331  size_t NumFecGroups() const { return group_map_.size(); }
332
333  // Testing only.
334  size_t NumQueuedPackets() const { return queued_packets_.size(); }
335
336  // Returns true if the connection has queued packets or frames.
337  bool HasQueuedData() const;
338
339  // Sets (or resets) the idle state connection timeout. Also, checks and times
340  // out the connection if network timer has expired for |timeout|.
341  void SetIdleNetworkTimeout(QuicTime::Delta timeout);
342  // Sets (or resets) the total time delta the connection can be alive for.
343  // Also, checks and times out the connection if timer has expired for
344  // |timeout|. Used to limit the time a connection can be alive before crypto
345  // handshake finishes.
346  void SetOverallConnectionTimeout(QuicTime::Delta timeout);
347
348  // If the connection has timed out, this will close the connection and return
349  // true.  Otherwise, it will return false and will reset the timeout alarm.
350  bool CheckForTimeout();
351
352  // Returns true of the next packet to be sent should be "lost" by
353  // not actually writing it to the wire.
354  bool ShouldSimulateLostPacket();
355
356  // Sets up a packet with an QuicAckFrame and sends it out.
357  void SendAck();
358
359  // Called when an RTO fires.  Returns the time when this alarm
360  // should next fire, or 0 if no retransmission alarm should be set.
361  QuicTime OnRetransmissionTimeout();
362
363  // Retransmits unacked packets which were sent with initial encryption, if
364  // |initial_encryption_only| is true, otherwise retransmits all unacked
365  // packets. Used when the negotiated protocol version is different than what
366  // was initially assumed and when the visitor wants to re-transmit packets
367  // with initial encryption when the initial encrypter changes.
368  void RetransmitUnackedPackets(RetransmissionType retransmission_type);
369
370  // Changes the encrypter used for level |level| to |encrypter|. The function
371  // takes ownership of |encrypter|.
372  void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter);
373  const QuicEncrypter* encrypter(EncryptionLevel level) const;
374
375  // SetDefaultEncryptionLevel sets the encryption level that will be applied
376  // to new packets.
377  void SetDefaultEncryptionLevel(EncryptionLevel level);
378
379  // SetDecrypter sets the primary decrypter, replacing any that already exists,
380  // and takes ownership. If an alternative decrypter is in place then the
381  // function DCHECKs. This is intended for cases where one knows that future
382  // packets will be using the new decrypter and the previous decrypter is now
383  // obsolete.
384  void SetDecrypter(QuicDecrypter* decrypter);
385
386  // SetAlternativeDecrypter sets a decrypter that may be used to decrypt
387  // future packets and takes ownership of it. If |latch_once_used| is true,
388  // then the first time that the decrypter is successful it will replace the
389  // primary decrypter. Otherwise both decrypters will remain active and the
390  // primary decrypter will be the one last used.
391  void SetAlternativeDecrypter(QuicDecrypter* decrypter,
392                               bool latch_once_used);
393
394  const QuicDecrypter* decrypter() const;
395  const QuicDecrypter* alternative_decrypter() const;
396
397 protected:
398  // Deletes all missing packets before least unacked. The connection won't
399  // process any packets with sequence number before |least_unacked| that it
400  // received after this call. Returns true if there were missing packets before
401  // |least_unacked| unacked, false otherwise.
402  bool DontWaitForPacketsBefore(QuicPacketSequenceNumber least_unacked);
403
404  // Send a packet to the peer using encryption |level|. If |sequence_number|
405  // is present in the |retransmission_map_|, then contents of this packet will
406  // be retransmitted with a new sequence number if it's not acked by the peer.
407  // Deletes |packet| via WritePacket call or transfers ownership to
408  // QueuedPacket, ultimately deleted via WritePacket. Also, it updates the
409  // entropy map corresponding to |sequence_number| using |entropy_hash|.
410  // TODO(wtc): none of the callers check the return value.
411  virtual bool SendOrQueuePacket(EncryptionLevel level,
412                                 QuicPacketSequenceNumber sequence_number,
413                                 QuicPacket* packet,
414                                 QuicPacketEntropyHash entropy_hash,
415                                 HasRetransmittableData retransmittable);
416
417  // Writes the given packet to socket, encrypted with |level|, with the help
418  // of helper. Returns true on successful write, false otherwise. However,
419  // behavior is undefined if connection is not established or broken. In any
420  // circumstances, a return value of true implies that |packet| has been
421  // deleted and should not be accessed. If |sequence_number| is present in
422  // |retransmission_map_| it also sets up retransmission of the given packet
423  // in case of successful write. If |force| is FORCE, then the packet will be
424  // sent immediately and the send scheduler will not be consulted.
425  bool WritePacket(EncryptionLevel level,
426                   QuicPacketSequenceNumber sequence_number,
427                   QuicPacket* packet,
428                   HasRetransmittableData retransmittable,
429                   Force force);
430
431  // Make sure an ack we got from our peer is sane.
432  bool ValidateAckFrame(const QuicAckFrame& incoming_ack);
433
434  QuicConnectionHelperInterface* helper() { return helper_.get(); }
435
436 protected:
437  QuicFramer framer_;
438
439 private:
440  friend class test::QuicConnectionPeer;
441
442  // Packets which have not been written to the wire.
443  // Owns the QuicPacket* packet.
444  struct QueuedPacket {
445    QueuedPacket(QuicPacketSequenceNumber sequence_number,
446                 QuicPacket* packet,
447                 EncryptionLevel level,
448                 HasRetransmittableData retransmittable)
449        : sequence_number(sequence_number),
450          packet(packet),
451          encryption_level(level),
452          retransmittable(retransmittable) {
453    }
454
455    QuicPacketSequenceNumber sequence_number;
456    QuicPacket* packet;
457    const EncryptionLevel encryption_level;
458    HasRetransmittableData retransmittable;
459  };
460
461  struct RetransmissionInfo {
462    explicit RetransmissionInfo(QuicPacketSequenceNumber sequence_number)
463        : sequence_number(sequence_number),
464          number_nacks(0),
465          number_retransmissions(0) {
466    }
467
468    QuicPacketSequenceNumber sequence_number;
469    size_t number_nacks;
470    size_t number_retransmissions;
471  };
472
473  struct RetransmissionTime {
474    RetransmissionTime(QuicPacketSequenceNumber sequence_number,
475                       const QuicTime& scheduled_time,
476                       bool for_fec)
477        : sequence_number(sequence_number),
478          scheduled_time(scheduled_time),
479          for_fec(for_fec) { }
480
481    QuicPacketSequenceNumber sequence_number;
482    QuicTime scheduled_time;
483    bool for_fec;
484  };
485
486  class RetransmissionTimeComparator {
487   public:
488    bool operator()(const RetransmissionTime& lhs,
489                    const RetransmissionTime& rhs) const {
490      DCHECK(lhs.scheduled_time.IsInitialized() &&
491             rhs.scheduled_time.IsInitialized());
492      return lhs.scheduled_time > rhs.scheduled_time;
493    }
494  };
495
496  typedef std::list<QueuedPacket> QueuedPacketList;
497  typedef linked_hash_map<QuicPacketSequenceNumber,
498                          RetransmittableFrames*> UnackedPacketMap;
499  typedef std::map<QuicFecGroupNumber, QuicFecGroup*> FecGroupMap;
500  typedef base::hash_map<QuicPacketSequenceNumber,
501                         RetransmissionInfo> RetransmissionMap;
502  typedef std::priority_queue<RetransmissionTime,
503                              std::vector<RetransmissionTime>,
504                              RetransmissionTimeComparator>
505      RetransmissionTimeouts;
506
507  // Selects and updates the version of the protocol being used by selecting a
508  // version from |available_versions| which is also supported. Returns true if
509  // such a version exists, false otherwise.
510  bool SelectMutualVersion(const QuicTagVector& available_versions);
511  // Sends a version negotiation packet to the peer.
512  void SendVersionNegotiationPacket();
513
514  void SetupRetransmission(QuicPacketSequenceNumber sequence_number);
515  bool IsRetransmission(QuicPacketSequenceNumber sequence_number);
516
517  void SetupAbandonFecTimer(QuicPacketSequenceNumber sequence_number);
518
519  // Drop packet corresponding to |sequence_number| by deleting entries from
520  // |unacked_packets_| and |retransmission_map_|, if present. We need to drop
521  // all packets with encryption level NONE after the default level has been set
522  // to FORWARD_SECURE.
523  void DropPacket(QuicPacketSequenceNumber sequence_number);
524
525  // Writes as many queued packets as possible.  The connection must not be
526  // blocked when this is called.
527  bool WriteQueuedPackets();
528
529  // If a packet can be revived from the current FEC group, then
530  // revive and process the packet.
531  void MaybeProcessRevivedPacket();
532
533  void HandleAckForSentPackets(const QuicAckFrame& incoming_ack,
534                               SequenceNumberSet* acked_packets);
535  void HandleAckForSentFecPackets(const QuicAckFrame& incoming_ack,
536                                  SequenceNumberSet* acked_packets);
537
538  // These two are called by OnAckFrame.
539  //
540  // Updates internal state based on incoming_ack.received_info
541  void UpdatePacketInformationReceivedByPeer(
542      const QuicAckFrame& incoming_ack);
543  // Updates internal state based on incoming_ack.sent_info
544  void UpdatePacketInformationSentByPeer(const QuicAckFrame& incoming_ack);
545
546  void UpdateOutgoingAck();
547
548  void MaybeSendAckInResponseToPacket();
549
550  void MaybeAbandonFecPacket(QuicPacketSequenceNumber sequence_number);
551
552  // Get the FEC group associate with the last processed packet or NULL, if the
553  // group has already been deleted.
554  QuicFecGroup* GetFecGroup();
555
556  // Closes any FEC groups protecting packets before |sequence_number|.
557  void CloseFecGroupsBefore(QuicPacketSequenceNumber sequence_number);
558
559  scoped_ptr<QuicConnectionHelperInterface> helper_;
560  EncryptionLevel encryption_level_;
561  const QuicClock* clock_;
562  QuicRandom* random_generator_;
563
564  const QuicGuid guid_;
565  // Address on the last successfully processed packet received from the
566  // client.
567  IPEndPoint self_address_;
568  IPEndPoint peer_address_;
569
570  bool last_packet_revived_;  // True if the last packet was revived from FEC.
571  size_t last_size_;  // Size of the last received packet.
572  QuicPacketHeader last_header_;
573  std::vector<QuicStreamFrame> last_stream_frames_;
574
575  QuicAckFrame outgoing_ack_;
576  QuicCongestionFeedbackFrame outgoing_congestion_feedback_;
577
578  // Track some peer state so we can do less bookkeeping
579  // Largest sequence sent by the peer which had an ack frame (latest ack info).
580  QuicPacketSequenceNumber largest_seen_packet_with_ack_;
581  // Largest sequence number that the peer has observed. Mostly received,
582  // missing in case of truncated acks.
583  QuicPacketSequenceNumber peer_largest_observed_packet_;
584  // Least sequence number which the peer is still waiting for.
585  QuicPacketSequenceNumber least_packet_awaited_by_peer_;
586  // Least sequence number of the the packet sent by the peer for which it
587  // hasn't received an ack.
588  QuicPacketSequenceNumber peer_least_packet_awaiting_ack_;
589
590  // When new packets are created which may be retransmitted, they are added
591  // to this map, which contains owning pointers to the contained frames.
592  UnackedPacketMap unacked_packets_;
593
594  // Pending fec packets that have not been acked yet. These packets need to be
595  // cleared out of the cgst_window after a timeout since FEC packets are never
596  // retransmitted.
597  // Ask: What should be the timeout for these packets?
598  UnackedPacketMap unacked_fec_packets_;
599
600  // Heap of packets that we might need to retransmit, and the time at
601  // which we should retransmit them. Every time a packet is sent it is added
602  // to this heap which is O(log(number of pending packets to be retransmitted))
603  // which might be costly. This should be optimized to O(1) by maintaining a
604  // priority queue of lists of packets to be retransmitted, where list x
605  // contains all packets that have been retransmitted x times.
606  RetransmissionTimeouts retransmission_timeouts_;
607
608  // Map from sequence number to the retransmission info.
609  RetransmissionMap retransmission_map_;
610
611  // True while OnRetransmissionTimeout is running to prevent
612  // SetRetransmissionAlarm from being called erroneously.
613  bool handling_retransmission_timeout_;
614
615  // When packets could not be sent because the socket was not writable,
616  // they are added to this list.  All corresponding frames are in
617  // unacked_packets_ if they are to be retransmitted.
618  QueuedPacketList queued_packets_;
619
620  // True when the socket becomes unwritable.
621  bool write_blocked_;
622
623  FecGroupMap group_map_;
624
625  QuicPacketEntropyManager entropy_manager_;
626
627  QuicConnectionVisitorInterface* visitor_;
628  QuicConnectionDebugVisitorInterface* debug_visitor_;
629  QuicPacketCreator packet_creator_;
630  QuicPacketGenerator packet_generator_;
631
632  // Network idle time before we kill of this connection.
633  QuicTime::Delta idle_network_timeout_;
634  // Overall connection timeout.
635  QuicTime::Delta overall_connection_timeout_;
636  // Connection creation time.
637  QuicTime creation_time_;
638
639  // Statistics for this session.
640  QuicConnectionStats stats_;
641
642  // The time that we got a packet for this connection.
643  QuicTime time_of_last_received_packet_;
644
645  // The time that we last sent a packet for this connection.
646  QuicTime time_of_last_sent_packet_;
647
648  // Member holding the time we received the largest_observed sequence number.
649  // Needed for calculating delta_time_largest_observed.
650  QuicTime time_largest_observed_;
651
652  // Congestion manager which controls the rate the connection sends packets
653  // as well as collecting and generating congestion feedback.
654  QuicCongestionManager congestion_manager_;
655
656  // The state of connection in version negotiation finite state machine.
657  QuicVersionNegotiationState version_negotiation_state_;
658
659  // The version of the protocol this connection is using.
660  QuicTag quic_version_;
661
662  size_t max_packets_per_retransmission_alarm_;
663
664  // Tracks if the connection was created by the server.
665  bool is_server_;
666
667  // True by default.  False if we've received or sent an explicit connection
668  // close.
669  bool connected_;
670
671  // True if the last ack received from the peer may have been truncated.  False
672  // otherwise.
673  bool received_truncated_ack_;
674  bool send_ack_in_response_to_packet_;
675
676  // Set to true if the udp packet headers have a new self or peer address.
677  // This is checked later on validating a data or version negotiation packet.
678  bool address_migrating_;
679
680  DISALLOW_COPY_AND_ASSIGN(QuicConnection);
681};
682
683}  // namespace net
684
685#endif  // NET_QUIC_QUIC_CONNECTION_H_
686