xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/qpack/qpack_encoder_stream_sender.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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(HuffmanEncoding huffman_encoding)25 QpackEncoderStreamSender::QpackEncoderStreamSender(
26     HuffmanEncoding huffman_encoding)
27     : delegate_(nullptr), instruction_encoder_(huffman_encoding) {}
28 
SendInsertWithNameReference(bool is_static,uint64_t name_index,absl::string_view value)29 void QpackEncoderStreamSender::SendInsertWithNameReference(
30     bool is_static, uint64_t name_index, absl::string_view value) {
31   instruction_encoder_.Encode(
32       QpackInstructionWithValues::InsertWithNameReference(is_static, name_index,
33                                                           value),
34       &buffer_);
35 }
36 
SendInsertWithoutNameReference(absl::string_view name,absl::string_view value)37 void QpackEncoderStreamSender::SendInsertWithoutNameReference(
38     absl::string_view name, absl::string_view value) {
39   instruction_encoder_.Encode(
40       QpackInstructionWithValues::InsertWithoutNameReference(name, value),
41       &buffer_);
42 }
43 
SendDuplicate(uint64_t index)44 void QpackEncoderStreamSender::SendDuplicate(uint64_t index) {
45   instruction_encoder_.Encode(QpackInstructionWithValues::Duplicate(index),
46                               &buffer_);
47 }
48 
SendSetDynamicTableCapacity(uint64_t capacity)49 void QpackEncoderStreamSender::SendSetDynamicTableCapacity(uint64_t capacity) {
50   instruction_encoder_.Encode(
51       QpackInstructionWithValues::SetDynamicTableCapacity(capacity), &buffer_);
52 }
53 
CanWrite() const54 bool QpackEncoderStreamSender::CanWrite() const {
55   return delegate_ && delegate_->NumBytesBuffered() + buffer_.size() <=
56                           kMaxBytesBufferedByStream;
57 }
58 
Flush()59 void QpackEncoderStreamSender::Flush() {
60   if (buffer_.empty()) {
61     return;
62   }
63 
64   delegate_->WriteStreamData(buffer_);
65   buffer_.clear();
66 }
67 
68 }  // namespace quic
69