1 // Copyright 2021 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_QUIC_CORE_HTTP_WEB_TRANSPORT_STREAM_ADAPTER_H_ 6 #define QUICHE_QUIC_CORE_HTTP_WEB_TRANSPORT_STREAM_ADAPTER_H_ 7 8 #include "quiche/quic/core/quic_session.h" 9 #include "quiche/quic/core/quic_stream.h" 10 #include "quiche/quic/core/quic_stream_sequencer.h" 11 #include "quiche/quic/core/quic_types.h" 12 #include "quiche/quic/core/web_transport_interface.h" 13 #include "quiche/web_transport/web_transport.h" 14 15 namespace quic { 16 17 // Converts WebTransportStream API calls into QuicStream API calls. The users 18 // of this class can either subclass it, or wrap around it. 19 class QUIC_EXPORT_PRIVATE WebTransportStreamAdapter 20 : public WebTransportStream { 21 public: 22 WebTransportStreamAdapter(QuicSession* session, QuicStream* stream, 23 QuicStreamSequencer* sequencer); 24 25 // WebTransportStream implementation. 26 ABSL_MUST_USE_RESULT ReadResult Read(absl::Span<char> output) override; 27 ABSL_MUST_USE_RESULT ReadResult Read(std::string* output) override; 28 absl::Status Writev(absl::Span<const absl::string_view> data, 29 const quiche::StreamWriteOptions& options) override; 30 bool CanWrite() const override; 31 void AbruptlyTerminate(absl::Status error) override; 32 size_t ReadableBytes() const override; SetVisitor(std::unique_ptr<WebTransportStreamVisitor> visitor)33 void SetVisitor(std::unique_ptr<WebTransportStreamVisitor> visitor) override { 34 visitor_ = std::move(visitor); 35 } GetStreamId()36 QuicStreamId GetStreamId() const override { return stream_->id(); } 37 38 void ResetWithUserCode(WebTransportStreamError error) override; ResetDueToInternalError()39 void ResetDueToInternalError() override { 40 stream_->Reset(QUIC_STREAM_INTERNAL_ERROR); 41 } 42 void SendStopSending(WebTransportStreamError error) override; MaybeResetDueToStreamObjectGone()43 void MaybeResetDueToStreamObjectGone() override { 44 if (stream_->write_side_closed() && stream_->read_side_closed()) { 45 return; 46 } 47 stream_->Reset(QUIC_STREAM_CANCELLED); 48 } 49 visitor()50 WebTransportStreamVisitor* visitor() override { return visitor_.get(); } 51 52 // Calls that need to be passed from the corresponding QuicStream methods. 53 void OnDataAvailable(); 54 void OnCanWriteNewData(); 55 56 private: 57 absl::Status CheckBeforeStreamWrite() const; 58 59 QuicSession* session_; // Unowned. 60 QuicStream* stream_; // Unowned. 61 QuicStreamSequencer* sequencer_; // Unowned. 62 std::unique_ptr<WebTransportStreamVisitor> visitor_; 63 bool fin_read_ = false; 64 }; 65 66 } // namespace quic 67 68 #endif // QUICHE_QUIC_CORE_HTTP_WEB_TRANSPORT_STREAM_ADAPTER_H_ 69