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 // Responsible for generating packets on behalf of a QuicConnection.
6 // Packets are serialized just-in-time. Control frames are queued.
7 // Ack and Feedback frames will be requested from the Connection
8 // just-in-time. When a packet needs to be sent, the Generator
9 // will serialize a packet and pass it to QuicConnection::SendOrQueuePacket()
11 // The Generator's mode of operation is controlled by two conditions:
13 // 1) Is the Delegate writable?
15 // If the Delegate is not writable, then no operations will cause
16 // a packet to be serialized. In particular:
17 // * SetShouldSendAck will simply record that an ack is to be sent.
18 // * AddControlFrame will enqueue the control frame.
19 // * ConsumeData will do nothing.
21 // If the Delegate is writable, then the behavior depends on the second
24 // 2) Is the Generator in batch mode?
26 // If the Generator is NOT in batch mode, then each call to a write
27 // operation will serialize one or more packets. The contents will
28 // include any previous queued frames. If an ack should be sent
29 // but has not been sent, then the Delegate will be asked to create
30 // an Ack frame which will then be included in the packet. When
31 // the write call completes, the current packet will be serialized
32 // and sent to the Delegate, even if it is not full.
34 // If the Generator is in batch mode, then each write operation will
35 // add data to the "current" packet. When the current packet becomes
36 // full, it will be serialized and sent to the packet. When batch
37 // mode is ended via |FinishBatchOperations|, the current packet
38 // will be serialzied, even if it is not full.
40 // FEC behavior also depends on batch mode. In batch mode, FEC packets
41 // will be sent after |max_packets_per_group| have been sent, as well
42 // as after batch operations are complete. When not in batch mode,
43 // an FEC packet will be sent after each write call completes.
45 // TODO(rch): This behavior should probably be tuned. When not in batch
46 // mode, we should probably set a timer so that several independent
47 // operations can be grouped into the same FEC group.
49 // When an FEC packet is generated, it will be send to the Delegate,
50 // even if the Delegate has become unwritable after handling the
51 // data packet immediately proceeding the FEC packet.
53 #ifndef NET_QUIC_QUIC_PACKET_GENERATOR_H_
54 #define NET_QUIC_QUIC_PACKET_GENERATOR_H_
56 #include "net/quic/quic_packet_creator.h"
57 #include "net/quic/quic_sent_packet_manager.h"
58 #include "net/quic/quic_types.h"
63 class QuicPacketGeneratorPeer
;
66 class QuicAckNotifier
;
68 class NET_EXPORT_PRIVATE QuicPacketGenerator
69 : public QuicSentPacketManager::NetworkChangeVisitor
{
71 class NET_EXPORT_PRIVATE DelegateInterface
{
73 virtual ~DelegateInterface() {}
74 virtual bool ShouldGeneratePacket(TransmissionType transmission_type
,
75 HasRetransmittableData retransmittable
,
76 IsHandshake handshake
) = 0;
77 virtual QuicAckFrame
* CreateAckFrame() = 0;
78 virtual QuicCongestionFeedbackFrame
* CreateFeedbackFrame() = 0;
79 virtual QuicStopWaitingFrame
* CreateStopWaitingFrame() = 0;
80 // Takes ownership of |packet.packet| and |packet.retransmittable_frames|.
81 virtual bool OnSerializedPacket(const SerializedPacket
& packet
) = 0;
82 virtual void CloseConnection(QuicErrorCode error
, bool from_peer
) = 0;
85 // Interface which gets callbacks from the QuicPacketGenerator at interesting
86 // points. Implementations must not mutate the state of the generator
87 // as a result of these callbacks.
88 class NET_EXPORT_PRIVATE DebugDelegate
{
90 virtual ~DebugDelegate() {}
92 // Called when a frame has been added to the current packet.
93 virtual void OnFrameAddedToPacket(const QuicFrame
& frame
) {}
96 QuicPacketGenerator(QuicConnectionId connection_id
,
98 QuicRandom
* random_generator
,
99 DelegateInterface
* delegate
);
101 virtual ~QuicPacketGenerator();
103 // QuicSentPacketManager::NetworkChangeVisitor methods.
104 virtual void OnCongestionWindowChange(QuicByteCount congestion_window
)
107 // Indicates that an ACK frame should be sent. If |also_send_feedback| is
108 // true, then it also indicates a CONGESTION_FEEDBACK frame should be sent.
109 // If |also_send_stop_waiting| is true, then it also indicates that a
110 // STOP_WAITING frame should be sent as well.
111 // The contents of the frame(s) will be generated via a call to the delegates
112 // CreateAckFrame() and CreateFeedbackFrame() when the packet is serialized.
113 void SetShouldSendAck(bool also_send_feedback
,
114 bool also_send_stop_waiting
);
116 // Indicates that a STOP_WAITING frame should be sent.
117 void SetShouldSendStopWaiting();
119 void AddControlFrame(const QuicFrame
& frame
);
121 // Given some data, may consume part or all of it and pass it to the
122 // packet creator to be serialized into packets. If not in batch
123 // mode, these packets will also be sent during this call. Also
124 // attaches a QuicAckNotifier to any created stream frames, which
125 // will be called once the frame is ACKed by the peer. The
126 // QuicAckNotifier is owned by the QuicConnection. |notifier| may
128 QuicConsumedData
ConsumeData(QuicStreamId id
,
129 const IOVector
& data
,
130 QuicStreamOffset offset
,
132 FecProtection fec_protection
,
133 QuicAckNotifier
* notifier
);
135 // Indicates whether batch mode is currently enabled.
137 // Disables flushing.
138 void StartBatchOperations();
139 // Enables flushing and flushes queued data which can be sent.
140 void FinishBatchOperations();
142 // Flushes all queued frames, even frames which are not sendable.
143 void FlushAllQueuedFrames();
145 bool HasQueuedFrames() const;
147 // Makes the framer not serialize the protocol version in sent packets.
148 void StopSendingVersion();
150 // Creates a version negotiation packet which supports |supported_versions|.
151 // Caller owns the created packet. Also, sets the entropy hash of the
152 // serialized packet to a random bool and returns that value as a member of
154 QuicEncryptedPacket
* SerializeVersionNegotiationPacket(
155 const QuicVersionVector
& supported_versions
);
158 // Re-serializes frames with the original packet's sequence number length.
159 // Used for retransmitting packets to ensure they aren't too long.
160 // Caller must ensure that any open FEC group is closed before calling this
162 SerializedPacket
ReserializeAllFrames(
163 const QuicFrames
& frames
,
164 QuicSequenceNumberLength original_length
);
166 // Update the sequence number length to use in future packets as soon as it
167 // can be safely changed.
168 void UpdateSequenceNumberLength(
169 QuicPacketSequenceNumber least_packet_awaited_by_peer
,
170 QuicByteCount congestion_window
);
172 // Sets the encryption level that will be applied to new packets.
173 void set_encryption_level(EncryptionLevel level
);
175 // Sequence number of the last created packet, or 0 if no packets have been
177 QuicPacketSequenceNumber
sequence_number() const;
179 size_t max_packet_length() const;
181 void set_max_packet_length(size_t length
);
183 void set_debug_delegate(DebugDelegate
* debug_delegate
) {
184 debug_delegate_
= debug_delegate
;
188 friend class test::QuicPacketGeneratorPeer
;
190 // Turn on FEC protection for subsequent packets in the generator.
191 // If no FEC group is currently open in the creator, this method flushes any
192 // queued frames in the generator and in the creator, and it then turns FEC on
193 // in the creator. This method may be called with an open FEC group in the
194 // creator, in which case, only the generator's state is altered.
195 void MaybeStartFecProtection();
197 // Serializes and calls the delegate on an FEC packet if one was under
198 // construction in the creator. When |force| is false, it relies on the
199 // creator being ready to send an FEC packet, otherwise FEC packet is sent
200 // as long as one is under construction in the creator. Also tries to turn
201 // off FEC protection in the creator if it's off in the generator.
202 void MaybeSendFecPacketAndCloseGroup(bool force
);
204 void SendQueuedFrames(bool flush
);
206 // Test to see if we have pending ack, feedback, or control frames.
207 bool HasPendingFrames() const;
208 // Test to see if the addition of a pending frame (which might be
209 // retransmittable) would still allow the resulting packet to be sent now.
210 bool CanSendWithNextPendingFrameAddition() const;
211 // Add exactly one pending frame, preferring ack over feedback over control
213 bool AddNextPendingFrame();
215 bool AddFrame(const QuicFrame
& frame
);
217 void SerializeAndSendPacket();
219 DelegateInterface
* delegate_
;
220 DebugDelegate
* debug_delegate_
;
222 QuicPacketCreator packet_creator_
;
223 QuicFrames queued_control_frames_
;
225 // True if batch mode is currently enabled.
228 // True if FEC protection is on. The creator may have an open FEC group even
229 // if this variable is false.
230 bool should_fec_protect_
;
232 // Flags to indicate the need for just-in-time construction of a frame.
233 bool should_send_ack_
;
234 bool should_send_feedback_
;
235 bool should_send_stop_waiting_
;
236 // If we put a non-retransmittable frame (namley ack or feedback frame) in
237 // this packet, then we have to hold a reference to it until we flush (and
238 // serialize it). Retransmittable frames are referenced elsewhere so that they
239 // can later be (optionally) retransmitted.
240 scoped_ptr
<QuicAckFrame
> pending_ack_frame_
;
241 scoped_ptr
<QuicCongestionFeedbackFrame
> pending_feedback_frame_
;
242 scoped_ptr
<QuicStopWaitingFrame
> pending_stop_waiting_frame_
;
244 DISALLOW_COPY_AND_ASSIGN(QuicPacketGenerator
);
249 #endif // NET_QUIC_QUIC_PACKET_GENERATOR_H_