xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/web_transport/complete_buffer_visitor.h (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 #ifndef QUICHE_WEB_TRANSPORT_COMPLETE_BUFFER_VISITOR_H_
6 #define QUICHE_WEB_TRANSPORT_COMPLETE_BUFFER_VISITOR_H_
7 
8 #include <optional>
9 #include <utility>
10 
11 #include "quiche/common/quiche_callbacks.h"
12 #include "quiche/web_transport/web_transport.h"
13 
14 namespace webtransport {
15 
16 // A visitor that will buffer the entirety of the incoming stream into a string,
17 // and will send a pre-specified string all at once.
18 class QUICHE_EXPORT CompleteBufferVisitor : public StreamVisitor {
19  public:
20   using AllDataReadCallback = quiche::SingleUseCallback<void(std::string)>;
21 
CompleteBufferVisitor(webtransport::Stream * stream,std::string outgoing_data)22   CompleteBufferVisitor(webtransport::Stream* stream, std::string outgoing_data)
23       : stream_(stream),
24         outgoing_data_(std::in_place, std::move(outgoing_data)) {}
CompleteBufferVisitor(webtransport::Stream * stream,AllDataReadCallback incoming_data_callback)25   CompleteBufferVisitor(webtransport::Stream* stream,
26                         AllDataReadCallback incoming_data_callback)
27       : stream_(stream),
28         incoming_data_callback_(std::in_place,
29                                 std::move(incoming_data_callback)) {}
CompleteBufferVisitor(webtransport::Stream * stream,std::string outgoing_data,AllDataReadCallback incoming_data_callback)30   CompleteBufferVisitor(webtransport::Stream* stream, std::string outgoing_data,
31                         AllDataReadCallback incoming_data_callback)
32       : stream_(stream),
33         outgoing_data_(std::in_place, std::move(outgoing_data)),
34         incoming_data_callback_(std::in_place,
35                                 std::move(incoming_data_callback)) {}
36 
37   void OnCanRead() override;
38   void OnCanWrite() override;
39 
OnResetStreamReceived(StreamErrorCode)40   void OnResetStreamReceived(StreamErrorCode) override {}
OnStopSendingReceived(StreamErrorCode)41   void OnStopSendingReceived(StreamErrorCode) override {}
OnWriteSideInDataRecvdState()42   void OnWriteSideInDataRecvdState() override {}
43 
44  protected:
45   void SetOutgoingData(std::string data);
46 
47  private:
48   webtransport::Stream* stream_;
49   std::optional<std::string> outgoing_data_;
50   std::optional<AllDataReadCallback> incoming_data_callback_;
51   std::string incoming_data_buffer_;
52 };
53 
54 }  // namespace webtransport
55 
56 #endif  // QUICHE_WEB_TRANSPORT_COMPLETE_BUFFER_VISITOR_H_
57