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 #include "net/quic/quic_packet_generator.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "net/quic/quic_fec_group.h"
10 #include "net/quic/quic_utils.h"
12 using base::StringPiece
;
16 class QuicAckNotifier
;
18 QuicPacketGenerator::QuicPacketGenerator(DelegateInterface
* delegate
,
19 DebugDelegateInterface
* debug_delegate
,
20 QuicPacketCreator
* creator
)
21 : delegate_(delegate
),
22 debug_delegate_(debug_delegate
),
23 packet_creator_(creator
),
25 should_send_ack_(false),
26 should_send_feedback_(false) {
29 QuicPacketGenerator::~QuicPacketGenerator() {
30 for (QuicFrames::iterator it
= queued_control_frames_
.begin();
31 it
!= queued_control_frames_
.end(); ++it
) {
34 delete it
->padding_frame
;
37 delete it
->stream_frame
;
42 case CONGESTION_FEEDBACK_FRAME
:
43 delete it
->congestion_feedback_frame
;
45 case RST_STREAM_FRAME
:
46 delete it
->rst_stream_frame
;
48 case CONNECTION_CLOSE_FRAME
:
49 delete it
->connection_close_frame
;
52 delete it
->goaway_frame
;
54 case WINDOW_UPDATE_FRAME
:
55 delete it
->window_update_frame
;
58 delete it
->blocked_frame
;
61 DCHECK(false) << "Cannot delete type: " << it
->type
;
66 void QuicPacketGenerator::SetShouldSendAck(bool also_send_feedback
) {
67 should_send_ack_
= true;
68 should_send_feedback_
= also_send_feedback
;
69 SendQueuedFrames(false);
72 void QuicPacketGenerator::AddControlFrame(const QuicFrame
& frame
) {
73 queued_control_frames_
.push_back(frame
);
74 SendQueuedFrames(false);
77 QuicConsumedData
QuicPacketGenerator::ConsumeData(QuicStreamId id
,
78 const IOVector
& data_to_write
,
79 QuicStreamOffset offset
,
81 QuicAckNotifier
* notifier
) {
82 IsHandshake handshake
= id
== kCryptoStreamId
? IS_HANDSHAKE
: NOT_HANDSHAKE
;
83 // The caller should have flushed pending frames before sending handshake
85 DCHECK(handshake
== NOT_HANDSHAKE
|| !HasPendingFrames());
86 SendQueuedFrames(false);
88 size_t total_bytes_consumed
= 0;
89 bool fin_consumed
= false;
91 if (!packet_creator_
->HasRoomForStreamFrame(id
, offset
)) {
92 SerializeAndSendPacket();
95 IOVector data
= data_to_write
;
96 size_t data_size
= data
.TotalBufferSize();
97 while (delegate_
->ShouldGeneratePacket(NOT_RETRANSMISSION
,
98 HAS_RETRANSMITTABLE_DATA
, handshake
)) {
100 size_t bytes_consumed
;
101 if (notifier
!= NULL
) {
102 // We want to track which packet this stream frame ends up in.
103 bytes_consumed
= packet_creator_
->CreateStreamFrameWithNotifier(
104 id
, data
, offset
+ total_bytes_consumed
, fin
, notifier
, &frame
);
106 bytes_consumed
= packet_creator_
->CreateStreamFrame(
107 id
, data
, offset
+ total_bytes_consumed
, fin
, &frame
);
109 if (!AddFrame(frame
)) {
110 LOG(DFATAL
) << "Failed to add stream frame.";
111 // Inability to add a STREAM frame creates an unrecoverable hole in a
112 // the stream, so it's best to close the connection.
113 delegate_
->CloseConnection(QUIC_INTERNAL_ERROR
, false);
114 return QuicConsumedData(0, false);
117 total_bytes_consumed
+= bytes_consumed
;
118 fin_consumed
= fin
&& total_bytes_consumed
== data_size
;
119 data
.Consume(bytes_consumed
);
120 DCHECK(data
.Empty() || packet_creator_
->BytesFree() == 0u);
122 // TODO(ianswett): Restore packet reordering.
123 if (!InBatchMode() || !packet_creator_
->HasRoomForStreamFrame(id
, offset
)) {
124 SerializeAndSendPacket();
128 // We're done writing the data. Exit the loop.
129 // We don't make this a precondition because we could have 0 bytes of data
130 // if we're simply writing a fin.
135 // Ensure the FEC group is closed at the end of this method if not in batch
137 if (!InBatchMode() && packet_creator_
->ShouldSendFec(true)) {
138 SerializedPacket serialized_fec
= packet_creator_
->SerializeFec();
139 DCHECK(serialized_fec
.packet
);
140 delegate_
->OnSerializedPacket(serialized_fec
);
143 DCHECK(InBatchMode() || !packet_creator_
->HasPendingFrames());
144 return QuicConsumedData(total_bytes_consumed
, fin_consumed
);
147 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const {
148 DCHECK(HasPendingFrames());
149 HasRetransmittableData retransmittable
=
150 (should_send_ack_
|| should_send_feedback_
) ? NO_RETRANSMITTABLE_DATA
151 : HAS_RETRANSMITTABLE_DATA
;
152 if (retransmittable
== HAS_RETRANSMITTABLE_DATA
) {
153 DCHECK(!queued_control_frames_
.empty()); // These are retransmittable.
155 return delegate_
->ShouldGeneratePacket(NOT_RETRANSMISSION
, retransmittable
,
159 void QuicPacketGenerator::SendQueuedFrames(bool flush
) {
160 // Only add pending frames if we are SURE we can then send the whole packet.
161 while (HasPendingFrames() &&
162 (flush
|| CanSendWithNextPendingFrameAddition())) {
163 if (!AddNextPendingFrame()) {
164 // Packet was full, so serialize and send it.
165 SerializeAndSendPacket();
169 if (!InBatchMode() || flush
) {
170 if (packet_creator_
->HasPendingFrames()) {
171 SerializeAndSendPacket();
174 // Ensure the FEC group is closed at the end of this method unless other
175 // writes are pending.
176 if (packet_creator_
->ShouldSendFec(true)) {
177 SerializedPacket serialized_fec
= packet_creator_
->SerializeFec();
178 DCHECK(serialized_fec
.packet
);
179 delegate_
->OnSerializedPacket(serialized_fec
);
184 bool QuicPacketGenerator::InBatchMode() {
188 void QuicPacketGenerator::StartBatchOperations() {
192 void QuicPacketGenerator::FinishBatchOperations() {
194 SendQueuedFrames(false);
197 void QuicPacketGenerator::FlushAllQueuedFrames() {
198 SendQueuedFrames(true);
201 bool QuicPacketGenerator::HasQueuedFrames() const {
202 return packet_creator_
->HasPendingFrames() || HasPendingFrames();
205 bool QuicPacketGenerator::HasPendingFrames() const {
206 return should_send_ack_
|| should_send_feedback_
||
207 !queued_control_frames_
.empty();
210 bool QuicPacketGenerator::AddNextPendingFrame() {
211 if (should_send_ack_
) {
212 pending_ack_frame_
.reset(delegate_
->CreateAckFrame());
213 // If we can't this add the frame now, then we still need to do so later.
214 should_send_ack_
= !AddFrame(QuicFrame(pending_ack_frame_
.get()));
215 // Return success if we have cleared out this flag (i.e., added the frame).
216 // If we still need to send, then the frame is full, and we have failed.
217 return !should_send_ack_
;
220 if (should_send_feedback_
) {
221 pending_feedback_frame_
.reset(delegate_
->CreateFeedbackFrame());
222 // If we can't this add the frame now, then we still need to do so later.
223 should_send_feedback_
= !AddFrame(QuicFrame(pending_feedback_frame_
.get()));
224 // Return success if we have cleared out this flag (i.e., added the frame).
225 // If we still need to send, then the frame is full, and we have failed.
226 return !should_send_feedback_
;
229 if (queued_control_frames_
.empty()) {
230 LOG(DFATAL
) << "AddNextPendingFrame called with no queued control frames.";
232 if (!AddFrame(queued_control_frames_
.back())) {
236 queued_control_frames_
.pop_back();
240 bool QuicPacketGenerator::AddFrame(const QuicFrame
& frame
) {
241 bool success
= packet_creator_
->AddSavedFrame(frame
);
242 if (success
&& debug_delegate_
) {
243 debug_delegate_
->OnFrameAddedToPacket(frame
);
248 void QuicPacketGenerator::SerializeAndSendPacket() {
249 SerializedPacket serialized_packet
= packet_creator_
->SerializePacket();
250 DCHECK(serialized_packet
.packet
);
251 delegate_
->OnSerializedPacket(serialized_packet
);
253 if (packet_creator_
->ShouldSendFec(false)) {
254 SerializedPacket serialized_fec
= packet_creator_
->SerializeFec();
255 DCHECK(serialized_fec
.packet
);
256 delegate_
->OnSerializedPacket(serialized_fec
);