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.
5 // The entity that handles framing writes for a Quic client or server.
6 // Each QuicSession will have a connection associated with it.
8 // On the server side, the Dispatcher handles the raw reads, and hands off
9 // packets via ProcessUdpPacket for framing and processing.
11 // On the client side, the Connection handles the raw reads, as well as the
14 // Note: this class is not thread-safe.
16 #ifndef NET_QUIC_QUIC_CONNECTION_H_
17 #define NET_QUIC_QUIC_CONNECTION_H_
27 #include "base/logging.h"
28 #include "net/base/iovec.h"
29 #include "net/base/ip_endpoint.h"
30 #include "net/quic/iovector.h"
31 #include "net/quic/quic_ack_notifier.h"
32 #include "net/quic/quic_ack_notifier_manager.h"
33 #include "net/quic/quic_alarm.h"
34 #include "net/quic/quic_blocked_writer_interface.h"
35 #include "net/quic/quic_connection_stats.h"
36 #include "net/quic/quic_packet_creator.h"
37 #include "net/quic/quic_packet_generator.h"
38 #include "net/quic/quic_packet_writer.h"
39 #include "net/quic/quic_protocol.h"
40 #include "net/quic/quic_received_packet_manager.h"
41 #include "net/quic/quic_sent_entropy_manager.h"
42 #include "net/quic/quic_sent_packet_manager.h"
43 #include "net/quic/quic_time.h"
44 #include "net/quic/quic_types.h"
57 class PacketSavingConnection
;
58 class QuicConnectionPeer
;
61 // Class that receives callbacks from the connection when frames are received
62 // and when other interesting events happen.
63 class NET_EXPORT_PRIVATE QuicConnectionVisitorInterface
{
65 virtual ~QuicConnectionVisitorInterface() {}
67 // A simple visitor interface for dealing with data frames.
68 virtual void OnStreamFrames(const std::vector
<QuicStreamFrame
>& frames
) = 0;
70 // The session should process all WINDOW_UPDATE frames, adjusting both stream
71 // and connection level flow control windows.
72 virtual void OnWindowUpdateFrames(
73 const std::vector
<QuicWindowUpdateFrame
>& frames
) = 0;
75 // BLOCKED frames tell us that the peer believes it is flow control blocked on
76 // a specified stream. If the session at this end disagrees, something has
77 // gone wrong with our flow control accounting.
78 virtual void OnBlockedFrames(const std::vector
<QuicBlockedFrame
>& frames
) = 0;
80 // Called when the stream is reset by the peer.
81 virtual void OnRstStream(const QuicRstStreamFrame
& frame
) = 0;
83 // Called when the connection is going away according to the peer.
84 virtual void OnGoAway(const QuicGoAwayFrame
& frame
) = 0;
86 // Called when the connection is closed either locally by the framer, or
87 // remotely by the peer.
88 virtual void OnConnectionClosed(QuicErrorCode error
, bool from_peer
) = 0;
90 // Called when the connection failed to write because the socket was blocked.
91 virtual void OnWriteBlocked() = 0;
93 // Called once a specific QUIC version is agreed by both endpoints.
94 virtual void OnSuccessfulVersionNegotiation(const QuicVersion
& version
) = 0;
96 // Called when a blocked socket becomes writable.
97 virtual void OnCanWrite() = 0;
99 // Called when the connection experiences a change in congestion window.
100 virtual void OnCongestionWindowChange(QuicTime now
) = 0;
102 // Called to ask if the visitor wants to schedule write resumption as it both
103 // has pending data to write, and is able to write (e.g. based on flow control
105 // Writes may be pending because they were write-blocked, congestion-throttled
106 // or yielded to other connections.
107 virtual bool WillingAndAbleToWrite() const = 0;
109 // Called to ask if any handshake messages are pending in this visitor.
110 virtual bool HasPendingHandshake() const = 0;
112 // Called to ask if any streams are open in this visitor, excluding the
113 // reserved crypto and headers stream.
114 virtual bool HasOpenDataStreams() const = 0;
117 // Interface which gets callbacks from the QuicConnection at interesting
118 // points. Implementations must not mutate the state of the connection
119 // as a result of these callbacks.
120 class NET_EXPORT_PRIVATE QuicConnectionDebugVisitor
121 : public QuicPacketGenerator::DebugDelegate
,
122 public QuicSentPacketManager::DebugDelegate
{
124 ~QuicConnectionDebugVisitor() override
{}
126 // Called when a packet has been sent.
127 virtual void OnPacketSent(const SerializedPacket
& serialized_packet
,
128 QuicPacketSequenceNumber original_sequence_number
,
129 EncryptionLevel level
,
130 TransmissionType transmission_type
,
131 const QuicEncryptedPacket
& packet
,
132 QuicTime sent_time
) {}
134 // Called when a packet has been received, but before it is
135 // validated or parsed.
136 virtual void OnPacketReceived(const IPEndPoint
& self_address
,
137 const IPEndPoint
& peer_address
,
138 const QuicEncryptedPacket
& packet
) {}
140 // Called when a packet is received with a connection id that does not
141 // match the ID of this connection.
142 virtual void OnIncorrectConnectionId(
143 QuicConnectionId connection_id
) {}
145 // Called when an undecryptable packet has been received.
146 virtual void OnUndecryptablePacket() {}
148 // Called when a duplicate packet has been received.
149 virtual void OnDuplicatePacket(QuicPacketSequenceNumber sequence_number
) {}
151 // Called when the protocol version on the received packet doensn't match
152 // current protocol version of the connection.
153 virtual void OnProtocolVersionMismatch(QuicVersion version
) {}
155 // Called when the complete header of a packet has been parsed.
156 virtual void OnPacketHeader(const QuicPacketHeader
& header
) {}
158 // Called when a StreamFrame has been parsed.
159 virtual void OnStreamFrame(const QuicStreamFrame
& frame
) {}
161 // Called when a AckFrame has been parsed.
162 virtual void OnAckFrame(const QuicAckFrame
& frame
) {}
164 // Called when a CongestionFeedbackFrame has been parsed.
165 virtual void OnCongestionFeedbackFrame(
166 const QuicCongestionFeedbackFrame
& frame
) {}
168 // Called when a StopWaitingFrame has been parsed.
169 virtual void OnStopWaitingFrame(const QuicStopWaitingFrame
& frame
) {}
171 // Called when a Ping has been parsed.
172 virtual void OnPingFrame(const QuicPingFrame
& frame
) {}
174 // Called when a GoAway has been parsed.
175 virtual void OnGoAwayFrame(const QuicGoAwayFrame
& frame
) {}
177 // Called when a RstStreamFrame has been parsed.
178 virtual void OnRstStreamFrame(const QuicRstStreamFrame
& frame
) {}
180 // Called when a ConnectionCloseFrame has been parsed.
181 virtual void OnConnectionCloseFrame(
182 const QuicConnectionCloseFrame
& frame
) {}
184 // Called when a WindowUpdate has been parsed.
185 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame
& frame
) {}
187 // Called when a BlockedFrame has been parsed.
188 virtual void OnBlockedFrame(const QuicBlockedFrame
& frame
) {}
190 // Called when a public reset packet has been received.
191 virtual void OnPublicResetPacket(const QuicPublicResetPacket
& packet
) {}
193 // Called when a version negotiation packet has been received.
194 virtual void OnVersionNegotiationPacket(
195 const QuicVersionNegotiationPacket
& packet
) {}
197 // Called after a packet has been successfully parsed which results
198 // in the revival of a packet via FEC.
199 virtual void OnRevivedPacket(const QuicPacketHeader
& revived_header
,
200 base::StringPiece payload
) {}
202 // Called when the connection is closed.
203 virtual void OnConnectionClosed(QuicErrorCode error
, bool from_peer
) {}
205 // Called when the version negotiation is successful.
206 virtual void OnSuccessfulVersionNegotiation(const QuicVersion
& version
) {}
209 class NET_EXPORT_PRIVATE QuicConnectionHelperInterface
{
211 virtual ~QuicConnectionHelperInterface() {}
213 // Returns a QuicClock to be used for all time related functions.
214 virtual const QuicClock
* GetClock() const = 0;
216 // Returns a QuicRandom to be used for all random number related functions.
217 virtual QuicRandom
* GetRandomGenerator() = 0;
219 // Creates a new platform-specific alarm which will be configured to
220 // notify |delegate| when the alarm fires. Caller takes ownership
221 // of the new alarm, which will not yet be "set" to fire.
222 virtual QuicAlarm
* CreateAlarm(QuicAlarm::Delegate
* delegate
) = 0;
225 class NET_EXPORT_PRIVATE QuicConnection
226 : public QuicFramerVisitorInterface
,
227 public QuicBlockedWriterInterface
,
228 public QuicPacketGenerator::DelegateInterface
,
229 public QuicSentPacketManager::NetworkChangeVisitor
{
234 BUNDLE_PENDING_ACK
= 2,
237 class PacketWriterFactory
{
239 virtual ~PacketWriterFactory() {}
241 virtual QuicPacketWriter
* Create(QuicConnection
* connection
) const = 0;
244 // Constructs a new QuicConnection for |connection_id| and |address|. Invokes
245 // writer_factory->Create() to get a writer; |owns_writer| specifies whether
246 // the connection takes ownership of the returned writer. |helper| must
247 // outlive this connection.
248 QuicConnection(QuicConnectionId connection_id
,
250 QuicConnectionHelperInterface
* helper
,
251 const PacketWriterFactory
& writer_factory
,
254 const QuicVersionVector
& supported_versions
);
255 ~QuicConnection() override
;
257 // Sets connection parameters from the supplied |config|.
258 void SetFromConfig(const QuicConfig
& config
);
260 // Send the data in |data| to the peer in as few packets as possible.
261 // Returns a pair with the number of bytes consumed from data, and a boolean
262 // indicating if the fin bit was consumed. This does not indicate the data
263 // has been sent on the wire: it may have been turned into a packet and queued
264 // if the socket was unexpectedly blocked. |fec_protection| indicates if
265 // data is to be FEC protected. Note that data that is sent immediately
266 // following MUST_FEC_PROTECT data may get protected by falling within the
268 // If |delegate| is provided, then it will be informed once ACKs have been
269 // received for all the packets written in this call.
270 // The |delegate| is not owned by the QuicConnection and must outlive it.
271 QuicConsumedData
SendStreamData(QuicStreamId id
,
272 const IOVector
& data
,
273 QuicStreamOffset offset
,
275 FecProtection fec_protection
,
276 QuicAckNotifier::DelegateInterface
* delegate
);
278 // Send a RST_STREAM frame to the peer.
279 virtual void SendRstStream(QuicStreamId id
,
280 QuicRstStreamErrorCode error
,
281 QuicStreamOffset bytes_written
);
283 // Send a BLOCKED frame to the peer.
284 virtual void SendBlocked(QuicStreamId id
);
286 // Send a WINDOW_UPDATE frame to the peer.
287 virtual void SendWindowUpdate(QuicStreamId id
,
288 QuicStreamOffset byte_offset
);
290 // Sends the connection close packet without affecting the state of the
291 // connection. This should only be called if the session is actively being
292 // destroyed: otherwise call SendConnectionCloseWithDetails instead.
293 virtual void SendConnectionClosePacket(QuicErrorCode error
,
294 const std::string
& details
);
296 // Sends a connection close frame to the peer, and closes the connection by
297 // calling CloseConnection(notifying the visitor as it does so).
298 virtual void SendConnectionClose(QuicErrorCode error
);
299 virtual void SendConnectionCloseWithDetails(QuicErrorCode error
,
300 const std::string
& details
);
301 // Notifies the visitor of the close and marks the connection as disconnected.
302 void CloseConnection(QuicErrorCode error
, bool from_peer
) override
;
303 virtual void SendGoAway(QuicErrorCode error
,
304 QuicStreamId last_good_stream_id
,
305 const std::string
& reason
);
307 // Returns statistics tracked for this connection.
308 const QuicConnectionStats
& GetStats();
310 // Processes an incoming UDP packet (consisting of a QuicEncryptedPacket) from
311 // the peer. If processing this packet permits a packet to be revived from
312 // its FEC group that packet will be revived and processed.
313 virtual void ProcessUdpPacket(const IPEndPoint
& self_address
,
314 const IPEndPoint
& peer_address
,
315 const QuicEncryptedPacket
& packet
);
317 // QuicBlockedWriterInterface
318 // Called when the underlying connection becomes writable to allow queued
320 void OnCanWrite() override
;
322 // Called when an error occurs while attempting to write a packet to the
324 void OnWriteError(int error_code
);
326 // If the socket is not blocked, writes queued packets.
327 void WriteIfNotBlocked();
329 // The version of the protocol this connection is using.
330 QuicVersion
version() const { return framer_
.version(); }
332 // The versions of the protocol that this connection supports.
333 const QuicVersionVector
& supported_versions() const {
334 return framer_
.supported_versions();
337 // From QuicFramerVisitorInterface
338 void OnError(QuicFramer
* framer
) override
;
339 bool OnProtocolVersionMismatch(QuicVersion received_version
) override
;
340 void OnPacket() override
;
341 void OnPublicResetPacket(const QuicPublicResetPacket
& packet
) override
;
342 void OnVersionNegotiationPacket(
343 const QuicVersionNegotiationPacket
& packet
) override
;
344 void OnRevivedPacket() override
;
345 bool OnUnauthenticatedPublicHeader(
346 const QuicPacketPublicHeader
& header
) override
;
347 bool OnUnauthenticatedHeader(const QuicPacketHeader
& header
) override
;
348 void OnDecryptedPacket(EncryptionLevel level
) override
;
349 bool OnPacketHeader(const QuicPacketHeader
& header
) override
;
350 void OnFecProtectedPayload(base::StringPiece payload
) override
;
351 bool OnStreamFrame(const QuicStreamFrame
& frame
) override
;
352 bool OnAckFrame(const QuicAckFrame
& frame
) override
;
353 bool OnCongestionFeedbackFrame(
354 const QuicCongestionFeedbackFrame
& frame
) override
;
355 bool OnStopWaitingFrame(const QuicStopWaitingFrame
& frame
) override
;
356 bool OnPingFrame(const QuicPingFrame
& frame
) override
;
357 bool OnRstStreamFrame(const QuicRstStreamFrame
& frame
) override
;
358 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame
& frame
) override
;
359 bool OnGoAwayFrame(const QuicGoAwayFrame
& frame
) override
;
360 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame
& frame
) override
;
361 bool OnBlockedFrame(const QuicBlockedFrame
& frame
) override
;
362 void OnFecData(const QuicFecData
& fec
) override
;
363 void OnPacketComplete() override
;
365 // QuicPacketGenerator::DelegateInterface
366 bool ShouldGeneratePacket(TransmissionType transmission_type
,
367 HasRetransmittableData retransmittable
,
368 IsHandshake handshake
) override
;
369 QuicAckFrame
* CreateAckFrame() override
;
370 QuicCongestionFeedbackFrame
* CreateFeedbackFrame() override
;
371 QuicStopWaitingFrame
* CreateStopWaitingFrame() override
;
372 void OnSerializedPacket(const SerializedPacket
& packet
) override
;
374 // QuicSentPacketManager::NetworkChangeVisitor
375 void OnCongestionWindowChange(QuicByteCount congestion_window
) override
;
377 // Called by the crypto stream when the handshake completes. In the server's
378 // case this is when the SHLO has been ACKed. Clients call this on receipt of
380 void OnHandshakeComplete();
383 void set_visitor(QuicConnectionVisitorInterface
* visitor
) {
386 // This method takes ownership of |debug_visitor|.
387 void set_debug_visitor(QuicConnectionDebugVisitor
* debug_visitor
) {
388 debug_visitor_
.reset(debug_visitor
);
389 packet_generator_
.set_debug_delegate(debug_visitor
);
390 sent_packet_manager_
.set_debug_delegate(debug_visitor
);
392 const IPEndPoint
& self_address() const { return self_address_
; }
393 const IPEndPoint
& peer_address() const { return peer_address_
; }
394 QuicConnectionId
connection_id() const { return connection_id_
; }
395 const QuicClock
* clock() const { return clock_
; }
396 QuicRandom
* random_generator() const { return random_generator_
; }
397 size_t max_packet_length() const;
398 void set_max_packet_length(size_t length
);
400 bool connected() const { return connected_
; }
402 // Must only be called on client connections.
403 const QuicVersionVector
& server_supported_versions() const {
405 return server_supported_versions_
;
408 size_t NumFecGroups() const { return group_map_
.size(); }
411 size_t NumQueuedPackets() const { return queued_packets_
.size(); }
413 QuicEncryptedPacket
* ReleaseConnectionClosePacket() {
414 return connection_close_packet_
.release();
417 // Returns true if the underlying UDP socket is writable, there is
418 // no queued data and the connection is not congestion-control
420 bool CanWriteStreamData();
422 // Returns true if the connection has queued packets or frames.
423 bool HasQueuedData() const;
425 // TODO(ianswett): Remove when quic_unified_timeouts is removed.
426 // Sets (or resets) the idle state connection timeout. Also, checks and times
427 // out the connection if network timer has expired for |timeout|.
428 void SetIdleNetworkTimeout(QuicTime::Delta timeout
);
429 // Sets (or resets) the total time delta the connection can be alive for.
430 // Also, checks and times out the connection if timer has expired for
431 // |timeout|. Used to limit the time a connection can be alive before crypto
432 // handshake finishes.
433 void SetOverallConnectionTimeout(QuicTime::Delta timeout
);
435 // Sets the overall and idle state connection timeouts.
436 // Times out the connection if the timeout has been reached and
437 // the quic_timeouts_only_from_alarms flag is false.
438 void SetNetworkTimeouts(QuicTime::Delta overall_timeout
,
439 QuicTime::Delta idle_timeout
);
441 // If the connection has timed out, this will close the connection.
442 // Otherwise, it will reschedule the timeout alarm.
443 void CheckForTimeout();
445 // Sends a ping, and resets the ping alarm.
448 // Sets up a packet with an QuicAckFrame and sends it out.
451 // Called when an RTO fires. Resets the retransmission alarm if there are
452 // remaining unacked packets.
453 void OnRetransmissionTimeout();
455 // Retransmits all unacked packets with retransmittable frames if
456 // |retransmission_type| is ALL_UNACKED_PACKETS, otherwise retransmits only
457 // initially encrypted packets. Used when the negotiated protocol version is
458 // different from what was initially assumed and when the initial encryption
460 void RetransmitUnackedPackets(TransmissionType retransmission_type
);
462 // Calls |sent_packet_manager_|'s NeuterUnencryptedPackets. Used when the
463 // connection becomes forward secure and hasn't received acks for all packets.
464 void NeuterUnencryptedPackets();
466 // Changes the encrypter used for level |level| to |encrypter|. The function
467 // takes ownership of |encrypter|.
468 void SetEncrypter(EncryptionLevel level
, QuicEncrypter
* encrypter
);
469 const QuicEncrypter
* encrypter(EncryptionLevel level
) const;
471 // SetDefaultEncryptionLevel sets the encryption level that will be applied
473 void SetDefaultEncryptionLevel(EncryptionLevel level
);
475 // SetDecrypter sets the primary decrypter, replacing any that already exists,
476 // and takes ownership. If an alternative decrypter is in place then the
477 // function DCHECKs. This is intended for cases where one knows that future
478 // packets will be using the new decrypter and the previous decrypter is now
479 // obsolete. |level| indicates the encryption level of the new decrypter.
480 void SetDecrypter(QuicDecrypter
* decrypter
, EncryptionLevel level
);
482 // SetAlternativeDecrypter sets a decrypter that may be used to decrypt
483 // future packets and takes ownership of it. |level| indicates the encryption
484 // level of the decrypter. If |latch_once_used| is true, then the first time
485 // that the decrypter is successful it will replace the primary decrypter.
486 // Otherwise both decrypters will remain active and the primary decrypter
487 // will be the one last used.
488 void SetAlternativeDecrypter(QuicDecrypter
* decrypter
,
489 EncryptionLevel level
,
490 bool latch_once_used
);
492 const QuicDecrypter
* decrypter() const;
493 const QuicDecrypter
* alternative_decrypter() const;
495 bool is_server() const { return is_server_
; }
497 // Returns the underlying sent packet manager.
498 const QuicSentPacketManager
& sent_packet_manager() const {
499 return sent_packet_manager_
;
502 bool CanWrite(HasRetransmittableData retransmittable
);
504 // Stores current batch state for connection, puts the connection
505 // into batch mode, and destruction restores the stored batch state.
506 // While the bundler is in scope, any generated frames are bundled
507 // as densely as possible into packets. In addition, this bundler
508 // can be configured to ensure that an ACK frame is included in the
509 // first packet created, if there's new ack information to be sent.
510 class ScopedPacketBundler
{
512 // In addition to all outgoing frames being bundled when the
513 // bundler is in scope, setting |include_ack| to true ensures that
514 // an ACK frame is opportunistically bundled with the first
516 ScopedPacketBundler(QuicConnection
* connection
, AckBundling send_ack
);
517 ~ScopedPacketBundler();
520 QuicConnection
* connection_
;
521 bool already_in_batch_mode_
;
525 // Packets which have not been written to the wire.
526 // Owns the QuicPacket* packet.
527 struct QueuedPacket
{
528 QueuedPacket(SerializedPacket packet
,
529 EncryptionLevel level
);
530 QueuedPacket(SerializedPacket packet
,
531 EncryptionLevel level
,
532 TransmissionType transmission_type
,
533 QuicPacketSequenceNumber original_sequence_number
);
535 SerializedPacket serialized_packet
;
536 const EncryptionLevel encryption_level
;
537 TransmissionType transmission_type
;
538 // The packet's original sequence number if it is a retransmission.
539 // Otherwise it must be 0.
540 QuicPacketSequenceNumber original_sequence_number
;
543 // Do any work which logically would be done in OnPacket but can not be
544 // safely done until the packet is validated. Returns true if the packet
545 // can be handled, false otherwise.
546 virtual bool ProcessValidatedPacket();
548 // Send a packet to the peer, and takes ownership of the packet if the packet
549 // cannot be written immediately.
550 virtual void SendOrQueuePacket(QueuedPacket packet
);
552 QuicConnectionHelperInterface
* helper() { return helper_
; }
554 // Selects and updates the version of the protocol being used by selecting a
555 // version from |available_versions| which is also supported. Returns true if
556 // such a version exists, false otherwise.
557 bool SelectMutualVersion(const QuicVersionVector
& available_versions
);
559 QuicPacketWriter
* writer() { return writer_
; }
561 bool peer_port_changed() const { return peer_port_changed_
; }
563 QuicPacketSequenceNumber
sequence_number_of_last_sent_packet() const {
564 return sequence_number_of_last_sent_packet_
;
568 friend class test::QuicConnectionPeer
;
569 friend class test::PacketSavingConnection
;
571 typedef std::list
<QueuedPacket
> QueuedPacketList
;
572 typedef std::map
<QuicFecGroupNumber
, QuicFecGroup
*> FecGroupMap
;
574 // Writes the given packet to socket, encrypted with packet's
575 // encryption_level. Returns true on successful write, and false if the writer
576 // was blocked and the write needs to be tried again. Notifies the
577 // SentPacketManager when the write is successful and sets
578 // retransmittable frames to nullptr.
579 // Saves the connection close packet for later transmission, even if the
580 // writer is write blocked.
581 bool WritePacket(QueuedPacket
* packet
);
583 // Does the main work of WritePacket, but does not delete the packet or
584 // retransmittable frames upon success.
585 bool WritePacketInner(QueuedPacket
* packet
);
587 // Make sure an ack we got from our peer is sane.
588 bool ValidateAckFrame(const QuicAckFrame
& incoming_ack
);
590 // Make sure a stop waiting we got from our peer is sane.
591 bool ValidateStopWaitingFrame(const QuicStopWaitingFrame
& stop_waiting
);
593 // Sends a version negotiation packet to the peer.
594 void SendVersionNegotiationPacket();
596 // Clears any accumulated frames from the last received packet.
597 void ClearLastFrames();
599 // Writes as many queued packets as possible. The connection must not be
600 // blocked when this is called.
601 void WriteQueuedPackets();
603 // Writes as many pending retransmissions as possible.
604 void WritePendingRetransmissions();
606 // Returns true if the packet should be discarded and not sent.
607 bool ShouldDiscardPacket(const QueuedPacket
& packet
);
609 // Queues |packet| in the hopes that it can be decrypted in the
610 // future, when a new key is installed.
611 void QueueUndecryptablePacket(const QuicEncryptedPacket
& packet
);
613 // Attempts to process any queued undecryptable packets.
614 void MaybeProcessUndecryptablePackets();
616 // If a packet can be revived from the current FEC group, then
617 // revive and process the packet.
618 void MaybeProcessRevivedPacket();
620 void ProcessAckFrame(const QuicAckFrame
& incoming_ack
);
622 void ProcessStopWaitingFrame(const QuicStopWaitingFrame
& stop_waiting
);
624 // Update |stop_waiting| for an outgoing ack.
625 void UpdateStopWaiting(QuicStopWaitingFrame
* stop_waiting
);
627 // Queues an ack or sets the ack alarm when an incoming packet arrives that
629 void MaybeQueueAck();
631 // Checks if the last packet should instigate an ack.
632 bool ShouldLastPacketInstigateAck() const;
634 // Checks if the peer is waiting for packets that have been given up on, and
635 // therefore an ack frame should be sent with a larger least_unacked.
636 void UpdateStopWaitingCount();
638 // Sends any packets which are a response to the last packet, including both
639 // acks and pending writes if an ack opened the congestion window.
640 void MaybeSendInResponseToPacket();
642 // Gets the least unacked sequence number, which is the next sequence number
643 // to be sent if there are no outstanding packets.
644 QuicPacketSequenceNumber
GetLeastUnacked() const;
646 // Get the FEC group associate with the last processed packet or nullptr, if
647 // the group has already been deleted.
648 QuicFecGroup
* GetFecGroup();
650 // Closes any FEC groups protecting packets before |sequence_number|.
651 void CloseFecGroupsBefore(QuicPacketSequenceNumber sequence_number
);
653 // Sets the timeout alarm to the appropriate value, if any.
654 void SetTimeoutAlarm();
656 // Sets the ping alarm to the appropriate value, if any.
659 // On arrival of a new packet, checks to see if the socket addresses have
660 // changed since the last packet we saw on this connection.
661 void CheckForAddressMigration(const IPEndPoint
& self_address
,
662 const IPEndPoint
& peer_address
);
664 HasRetransmittableData
IsRetransmittable(const QueuedPacket
& packet
);
665 bool IsConnectionClose(QueuedPacket packet
);
668 QuicConnectionHelperInterface
* helper_
; // Not owned.
669 QuicPacketWriter
* writer_
; // Owned or not depending on |owns_writer_|.
671 EncryptionLevel encryption_level_
;
672 const QuicClock
* clock_
;
673 QuicRandom
* random_generator_
;
675 const QuicConnectionId connection_id_
;
676 // Address on the last successfully processed packet received from the
678 IPEndPoint self_address_
;
679 IPEndPoint peer_address_
;
680 // Used to store latest peer port to possibly migrate to later.
681 int migrating_peer_port_
;
683 // True if the last packet has gotten far enough in the framer to be
685 bool last_packet_decrypted_
;
686 bool last_packet_revived_
; // True if the last packet was revived from FEC.
687 size_t last_size_
; // Size of the last received packet.
688 EncryptionLevel last_decrypted_packet_level_
;
689 QuicPacketHeader last_header_
;
690 std::vector
<QuicStreamFrame
> last_stream_frames_
;
691 std::vector
<QuicAckFrame
> last_ack_frames_
;
692 std::vector
<QuicCongestionFeedbackFrame
> last_congestion_frames_
;
693 std::vector
<QuicStopWaitingFrame
> last_stop_waiting_frames_
;
694 std::vector
<QuicRstStreamFrame
> last_rst_frames_
;
695 std::vector
<QuicGoAwayFrame
> last_goaway_frames_
;
696 std::vector
<QuicWindowUpdateFrame
> last_window_update_frames_
;
697 std::vector
<QuicBlockedFrame
> last_blocked_frames_
;
698 std::vector
<QuicPingFrame
> last_ping_frames_
;
699 std::vector
<QuicConnectionCloseFrame
> last_close_frames_
;
701 QuicCongestionFeedbackFrame outgoing_congestion_feedback_
;
703 // Track some peer state so we can do less bookkeeping
704 // Largest sequence sent by the peer which had an ack frame (latest ack info).
705 QuicPacketSequenceNumber largest_seen_packet_with_ack_
;
707 // Largest sequence number sent by the peer which had a stop waiting frame.
708 QuicPacketSequenceNumber largest_seen_packet_with_stop_waiting_
;
710 // Collection of packets which were received before encryption was
711 // established, but which could not be decrypted. We buffer these on
712 // the assumption that they could not be processed because they were
713 // sent with the INITIAL encryption and the CHLO message was lost.
714 std::deque
<QuicEncryptedPacket
*> undecryptable_packets_
;
716 // Maximum number of undecryptable packets the connection will store.
717 size_t max_undecryptable_packets_
;
719 // When the version negotiation packet could not be sent because the socket
720 // was not writable, this is set to true.
721 bool pending_version_negotiation_packet_
;
723 // When packets could not be sent because the socket was not writable,
724 // they are added to this list. All corresponding frames are in
725 // unacked_packets_ if they are to be retransmitted.
726 QueuedPacketList queued_packets_
;
728 // Contains the connection close packet if the connection has been closed.
729 scoped_ptr
<QuicEncryptedPacket
> connection_close_packet_
;
731 FecGroupMap group_map_
;
733 QuicReceivedPacketManager received_packet_manager_
;
734 QuicSentEntropyManager sent_entropy_manager_
;
736 // Indicates whether an ack should be sent the next time we try to write.
738 // Indicates how many consecutive packets have arrived without sending an ack.
739 uint32 num_packets_received_since_last_ack_sent_
;
740 // Indicates how many consecutive times an ack has arrived which indicates
741 // the peer needs to stop waiting for some packets.
742 int stop_waiting_count_
;
744 // An alarm that fires when an ACK should be sent to the peer.
745 scoped_ptr
<QuicAlarm
> ack_alarm_
;
746 // An alarm that fires when a packet needs to be retransmitted.
747 scoped_ptr
<QuicAlarm
> retransmission_alarm_
;
748 // An alarm that is scheduled when the sent scheduler requires a
749 // a delay before sending packets and fires when the packet may be sent.
750 scoped_ptr
<QuicAlarm
> send_alarm_
;
751 // An alarm that is scheduled when the connection can still write and there
752 // may be more data to send.
753 scoped_ptr
<QuicAlarm
> resume_writes_alarm_
;
754 // An alarm that fires when the connection may have timed out.
755 scoped_ptr
<QuicAlarm
> timeout_alarm_
;
756 // An alarm that fires when a ping should be sent.
757 scoped_ptr
<QuicAlarm
> ping_alarm_
;
759 QuicConnectionVisitorInterface
* visitor_
;
760 scoped_ptr
<QuicConnectionDebugVisitor
> debug_visitor_
;
761 QuicPacketGenerator packet_generator_
;
763 // Network idle time before we kill of this connection.
764 QuicTime::Delta idle_network_timeout_
;
765 // Overall connection timeout.
766 QuicTime::Delta overall_connection_timeout_
;
768 // Statistics for this session.
769 QuicConnectionStats stats_
;
771 // The time that we got a packet for this connection.
772 // This is used for timeouts, and does not indicate the packet was processed.
773 QuicTime time_of_last_received_packet_
;
775 // The last time a new (non-retransmitted) packet was sent for this
777 QuicTime time_of_last_sent_new_packet_
;
779 // Sequence number of the last sent packet. Packets are guaranteed to be sent
780 // in sequence number order.
781 QuicPacketSequenceNumber sequence_number_of_last_sent_packet_
;
783 // Sent packet manager which tracks the status of packets sent by this
784 // connection and contains the send and receive algorithms to determine when
786 QuicSentPacketManager sent_packet_manager_
;
788 // The state of connection in version negotiation finite state machine.
789 QuicVersionNegotiationState version_negotiation_state_
;
791 // Tracks if the connection was created by the server.
794 // True by default. False if we've received or sent an explicit connection
798 // Set to true if the UDP packet headers have a new IP address for the peer.
799 // If true, do not perform connection migration.
800 bool peer_ip_changed_
;
802 // Set to true if the UDP packet headers have a new port for the peer.
803 // If true, and the IP has not changed, then we can migrate the connection.
804 bool peer_port_changed_
;
806 // Set to true if the UDP packet headers are addressed to a different IP.
807 // We do not support connection migration when the self IP changed.
808 bool self_ip_changed_
;
810 // Set to true if the UDP packet headers are addressed to a different port.
811 // We do not support connection migration when the self port changed.
812 bool self_port_changed_
;
814 // If non-empty this contains the set of versions received in a
815 // version negotiation packet.
816 QuicVersionVector server_supported_versions_
;
818 DISALLOW_COPY_AND_ASSIGN(QuicConnection
);
823 #endif // NET_QUIC_QUIC_CONNECTION_H_