1 // Copyright (c) 2018 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.
4
5 #include "quiche/quic/core/qpack/qpack_encoder_stream_sender.h"
6
7 #include <cstddef>
8 #include <limits>
9 #include <string>
10
11 #include "absl/strings/string_view.h"
12 #include "quiche/quic/core/qpack/qpack_instructions.h"
13 #include "quiche/quic/platform/api/quic_logging.h"
14
15 namespace quic {
16
17 namespace {
18
19 // If QUIC stream bufferes more that this number of bytes,
20 // CanWrite() will return false.
21 constexpr uint64_t kMaxBytesBufferedByStream = 64 * 1024;
22
23 } // anonymous namespace
24
QpackEncoderStreamSender()25 QpackEncoderStreamSender::QpackEncoderStreamSender() : delegate_(nullptr) {}
26
SendInsertWithNameReference(bool is_static,uint64_t name_index,absl::string_view value)27 void QpackEncoderStreamSender::SendInsertWithNameReference(
28 bool is_static, uint64_t name_index, absl::string_view value) {
29 instruction_encoder_.Encode(
30 QpackInstructionWithValues::InsertWithNameReference(is_static, name_index,
31 value),
32 &buffer_);
33 }
34
SendInsertWithoutNameReference(absl::string_view name,absl::string_view value)35 void QpackEncoderStreamSender::SendInsertWithoutNameReference(
36 absl::string_view name, absl::string_view value) {
37 instruction_encoder_.Encode(
38 QpackInstructionWithValues::InsertWithoutNameReference(name, value),
39 &buffer_);
40 }
41
SendDuplicate(uint64_t index)42 void QpackEncoderStreamSender::SendDuplicate(uint64_t index) {
43 instruction_encoder_.Encode(QpackInstructionWithValues::Duplicate(index),
44 &buffer_);
45 }
46
SendSetDynamicTableCapacity(uint64_t capacity)47 void QpackEncoderStreamSender::SendSetDynamicTableCapacity(uint64_t capacity) {
48 instruction_encoder_.Encode(
49 QpackInstructionWithValues::SetDynamicTableCapacity(capacity), &buffer_);
50 }
51
CanWrite() const52 bool QpackEncoderStreamSender::CanWrite() const {
53 return delegate_ && delegate_->NumBytesBuffered() + buffer_.size() <=
54 kMaxBytesBufferedByStream;
55 }
56
Flush()57 void QpackEncoderStreamSender::Flush() {
58 if (buffer_.empty()) {
59 return;
60 }
61
62 delegate_->WriteStreamData(buffer_);
63 buffer_.clear();
64 }
65
66 } // namespace quic
67