xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/web_transport/complete_buffer_visitor.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 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/web_transport/complete_buffer_visitor.h"
6 
7 #include <utility>
8 
9 #include "quiche/common/platform/api/quiche_logging.h"
10 #include "quiche/common/quiche_stream.h"
11 
12 namespace webtransport {
13 
OnCanRead()14 void CompleteBufferVisitor::OnCanRead() {
15   if (!incoming_data_callback_.has_value()) {
16     return;
17   }
18   Stream::ReadResult result = stream_->Read(&incoming_data_buffer_);
19   if (result.fin) {
20     (*std::move(incoming_data_callback_))(std::move(incoming_data_buffer_));
21     incoming_data_callback_.reset();
22   }
23 }
24 
OnCanWrite()25 void CompleteBufferVisitor::OnCanWrite() {
26   if (!outgoing_data_.has_value()) {
27     return;
28   }
29   if (!stream_->CanWrite()) {
30     return;
31   }
32   quiche::StreamWriteOptions options;
33   options.set_send_fin(true);
34   absl::Status status =
35       quiche::WriteIntoStream(*stream_, *outgoing_data_, options);
36   if (!status.ok()) {
37     QUICHE_DLOG(WARNING) << "Write from OnCanWrite() failed: " << status;
38     return;
39   }
40   outgoing_data_.reset();
41 }
42 
SetOutgoingData(std::string data)43 void CompleteBufferVisitor::SetOutgoingData(std::string data) {
44   QUICHE_DCHECK(!outgoing_data_.has_value());
45   outgoing_data_ = std::move(data);
46   if (stream_->CanWrite()) {
47     OnCanWrite();
48   }
49 }
50 
51 }  // namespace webtransport
52