summaryrefslogtreecommitdiffstats
path: root/net/quic/quic_packet_generator.cc
blob: e6aeac76f07ba4b11396928edd5cf25f914c08fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/quic/quic_packet_generator.h"

#include "base/logging.h"
#include "net/quic/quic_fec_group.h"
#include "net/quic/quic_utils.h"

using base::StringPiece;

namespace net {

QuicPacketGenerator::QuicPacketGenerator(DelegateInterface* delegate,
                                         DebugDelegateInterface* debug_delegate,
                                         QuicPacketCreator* creator)
    : delegate_(delegate),
      debug_delegate_(debug_delegate),
      packet_creator_(creator),
      should_flush_(true),
      should_send_ack_(false),
      should_send_feedback_(false) {
}

QuicPacketGenerator::~QuicPacketGenerator() {
  for (QuicFrames::iterator it = queued_control_frames_.begin();
       it != queued_control_frames_.end(); ++it) {
    switch (it->type) {
      case PADDING_FRAME:
        delete it->padding_frame;
        break;
      case STREAM_FRAME:
        delete it->stream_frame;
        break;
      case ACK_FRAME:
        delete it->ack_frame;
        break;
      case CONGESTION_FEEDBACK_FRAME:
        delete it->congestion_feedback_frame;
        break;
      case RST_STREAM_FRAME:
        delete it->rst_stream_frame;
        break;
      case CONNECTION_CLOSE_FRAME:
        delete it->connection_close_frame;
        break;
      case GOAWAY_FRAME:
        delete it->goaway_frame;
        break;
      case NUM_FRAME_TYPES:
        DCHECK(false) << "Cannot delete type: " << it->type;
    }
  }
}

void QuicPacketGenerator::SetShouldSendAck(bool also_send_feedback) {
  should_send_ack_ = true;
  should_send_feedback_ = also_send_feedback;
  SendQueuedFrames();
}


void QuicPacketGenerator::AddControlFrame(const QuicFrame& frame) {
  queued_control_frames_.push_back(frame);
  SendQueuedFrames();
}

QuicConsumedData QuicPacketGenerator::ConsumeData(QuicStreamId id,
                                                  StringPiece data,
                                                  QuicStreamOffset offset,
                                                  bool fin) {
  SendQueuedFrames();

  size_t total_bytes_consumed = 0;
  bool fin_consumed = false;

  while (delegate_->CanWrite(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA)) {
    QuicFrame frame;
    size_t bytes_consumed = packet_creator_->CreateStreamFrame(
        id, data, offset + total_bytes_consumed, fin, &frame);
    bool success = AddFrame(frame);
    DCHECK(success);

    total_bytes_consumed += bytes_consumed;
    fin_consumed = fin && bytes_consumed == data.size();
    data.remove_prefix(bytes_consumed);
    DCHECK(data.empty() || packet_creator_->BytesFree() == 0u);

    // TODO(ianswett): Restore packet reordering.
    if (should_flush_ || !packet_creator_->HasRoomForStreamFrame()) {
      SerializeAndSendPacket();
    }

    if (data.empty()) {
      // We're done writing the data. Exit the loop.
      // We don't make this a precondition because we could have 0 bytes of data
      // if we're simply writing a fin.
      break;
    }
  }

  // Ensure the FEC group is closed at the end of this method unless other
  // writes are pending.
  if (should_flush_ && packet_creator_->ShouldSendFec(true)) {
    SerializedPacket serialized_fec = packet_creator_->SerializeFec();
    DCHECK(serialized_fec.packet);
    delegate_->OnSerializedPacket(serialized_fec);
  }

  DCHECK(!should_flush_ || !packet_creator_->HasPendingFrames());
  return QuicConsumedData(total_bytes_consumed, fin_consumed);
}

void QuicPacketGenerator::SendQueuedFrames() {
  packet_creator_->MaybeStartFEC();
  while (HasPendingFrames() && delegate_->CanWrite(NOT_RETRANSMISSION,
             packet_creator_->HasPendingFrames() ?
                 HAS_RETRANSMITTABLE_DATA : NO_RETRANSMITTABLE_DATA)) {
    if (!AddNextPendingFrame()) {
      // Packet was full, so serialize and send it.
      SerializeAndSendPacket();
    }
  }

  if (should_flush_) {
    if (packet_creator_->HasPendingFrames()) {
      SerializeAndSendPacket();
    }

    // Ensure the FEC group is closed at the end of this method unless other
    // writes are pending.
    if (packet_creator_->ShouldSendFec(true)) {
      SerializedPacket serialized_fec = packet_creator_->SerializeFec();
      DCHECK(serialized_fec.packet);
      delegate_->OnSerializedPacket(serialized_fec);
      packet_creator_->MaybeStartFEC();
    }
  }
}

void QuicPacketGenerator::StartBatchOperations() {
  should_flush_ = false;
}

void QuicPacketGenerator::FinishBatchOperations() {
  should_flush_ = true;
  SendQueuedFrames();
}

bool QuicPacketGenerator::HasQueuedFrames() const {
  return packet_creator_->HasPendingFrames() || HasPendingFrames();
}

bool QuicPacketGenerator::HasPendingFrames() const {
  return should_send_ack_ || should_send_feedback_ ||
      !queued_control_frames_.empty();
}

bool QuicPacketGenerator::AddNextPendingFrame() {
  if (should_send_ack_) {
    pending_ack_frame_.reset(delegate_->CreateAckFrame());
    if (!AddFrame(QuicFrame(pending_ack_frame_.get()))) {
      // packet was full
      return false;
    }
    should_send_ack_ = false;
    return true;
  }

  if (should_send_feedback_) {
    pending_feedback_frame_.reset(delegate_->CreateFeedbackFrame());
    if (!AddFrame(QuicFrame(pending_feedback_frame_.get()))) {
      // packet was full
      return false;
    }
    should_send_feedback_ = false;
    return true;
  }

  DCHECK(!queued_control_frames_.empty());
  if (!AddFrame(queued_control_frames_.back())) {
    // packet was full
    return false;
  }
  queued_control_frames_.pop_back();
  return true;
}

bool QuicPacketGenerator::AddFrame(const QuicFrame& frame) {
  bool success = packet_creator_->AddSavedFrame(frame);
  if (success && debug_delegate_) {
    debug_delegate_->OnFrameAddedToPacket(frame);
  }
  return success;
}

void QuicPacketGenerator::SerializeAndSendPacket() {
  SerializedPacket serialized_packet = packet_creator_->SerializePacket();
  DCHECK(serialized_packet.packet);
  delegate_->OnSerializedPacket(serialized_packet);

  if (packet_creator_->ShouldSendFec(false)) {
    SerializedPacket serialized_fec = packet_creator_->SerializeFec();
    DCHECK(serialized_fec.packet);
    delegate_->OnSerializedPacket(serialized_fec);
    packet_creator_->MaybeStartFEC();
  }
}

}  // namespace net