Connect PPAPI IPC channels for non-SFI mode.
[chromium-blink-merge.git] / net / quic / quic_packet_generator.cc
blob34c65e032f903987b573e41d6797fa6e53285e42
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;
14 namespace net {
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),
24 batch_mode_(false),
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) {
32 switch (it->type) {
33 case PADDING_FRAME:
34 delete it->padding_frame;
35 break;
36 case STREAM_FRAME:
37 delete it->stream_frame;
38 break;
39 case ACK_FRAME:
40 delete it->ack_frame;
41 break;
42 case CONGESTION_FEEDBACK_FRAME:
43 delete it->congestion_feedback_frame;
44 break;
45 case RST_STREAM_FRAME:
46 delete it->rst_stream_frame;
47 break;
48 case CONNECTION_CLOSE_FRAME:
49 delete it->connection_close_frame;
50 break;
51 case GOAWAY_FRAME:
52 delete it->goaway_frame;
53 break;
54 case WINDOW_UPDATE_FRAME:
55 delete it->window_update_frame;
56 break;
57 case BLOCKED_FRAME:
58 delete it->blocked_frame;
59 break;
60 case NUM_FRAME_TYPES:
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,
80 bool fin,
81 QuicAckNotifier* notifier) {
82 IsHandshake handshake = id == kCryptoStreamId ? IS_HANDSHAKE : NOT_HANDSHAKE;
83 // The caller should have flushed pending frames before sending handshake
84 // messages.
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)) {
99 QuicFrame frame;
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);
105 } else {
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();
127 if (data.Empty()) {
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.
131 break;
135 // Ensure the FEC group is closed at the end of this method if not in batch
136 // mode.
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,
156 NOT_HANDSHAKE);
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() {
185 return batch_mode_;
188 void QuicPacketGenerator::StartBatchOperations() {
189 batch_mode_ = true;
192 void QuicPacketGenerator::FinishBatchOperations() {
193 batch_mode_ = false;
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())) {
233 // Packet was full.
234 return false;
236 queued_control_frames_.pop_back();
237 return true;
240 bool QuicPacketGenerator::AddFrame(const QuicFrame& frame) {
241 bool success = packet_creator_->AddSavedFrame(frame);
242 if (success && debug_delegate_) {
243 debug_delegate_->OnFrameAddedToPacket(frame);
245 return success;
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);
260 } // namespace net