// Copyright 2023 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // General-purpose abstractions for read/write streams. #ifndef QUICHE_COMMON_QUICHE_STREAM_H_ #define QUICHE_COMMON_QUICHE_STREAM_H_ #include <cstddef> #include <string> #include "absl/status/status.h" #include "absl/strings/string_view.h" #include "absl/types/span.h" #include "quiche/common/platform/api/quiche_export.h" #include "quiche/common/quiche_callbacks.h" namespace quiche { // A shared base class for read and write stream to support abrupt termination. class QUICHE_EXPORT TerminableStream { … }; // A general-purpose visitor API that gets notifications for ReadStream-related // events. class QUICHE_EXPORT ReadStreamVisitor { … }; // General purpose abstraction for a stream of data that can be read from the // network. The class is designed around the idea that a network stream stores // all of the received data in a sequence of contiguous buffers. Because of // that, there are two ways to read from a stream: // - Read() will copy data into a user-provided buffer, reassembling it if it // is split across multiple buffers internally. // - PeekNextReadableRegion()/SkipBytes() let the caller access the underlying // buffers directly, potentially avoiding the copying at the cost of the // caller having to deal with discontinuities. class QUICHE_EXPORT ReadStream { … }; // Calls `callback` for every contiguous chunk available inside the stream. // Returns true if the FIN has been reached. inline bool ProcessAllReadableRegions( ReadStream& stream, UnretainedCallback<void(absl::string_view)> callback) { … } // A general-purpose visitor API that gets notifications for WriteStream-related // events. class QUICHE_EXPORT WriteStreamVisitor { … }; // Options for writing data into a WriteStream. class QUICHE_EXPORT StreamWriteOptions { … }; inline constexpr StreamWriteOptions kDefaultStreamWriteOptions = …; // WriteStream is an object that can accept a stream of bytes. // // The writes into a WriteStream are all-or-nothing. A WriteStream object has // to either accept all data written into it by returning absl::OkStatus, or ask // the caller to try again once via OnCanWrite() by returning // absl::UnavailableError. class QUICHE_EXPORT WriteStream { … }; // Convenience methods to write a single chunk of data into the stream. inline absl::Status WriteIntoStream( WriteStream& stream, absl::string_view data, const StreamWriteOptions& options = kDefaultStreamWriteOptions) { … } // Convenience methods to send a FIN on the stream. inline absl::Status SendFinOnStream(WriteStream& stream) { … } inline size_t TotalStringViewSpanSize( absl::Span<const absl::string_view> span) { … } } // namespace quiche #endif // QUICHE_COMMON_QUICHE_STREAM_H_