Class AsyncReader (2.19.0)

A handle for streaming downloads.

Applications use this object to handle asynchronous streaming downloads. The application repeatedly calls Read() until it has received all the data it wants.

Constructors

AsyncReader(AsyncReader &&)

Move-only.

Parameter
NameDescription
AsyncReader &&

AsyncReader(AsyncReader const &)

Move-only.

Parameter
NameDescription
AsyncReader const &

AsyncReader()

Creates a reader that always returns errors.

AsyncReader(std::unique_ptr< AsyncReaderConnection >)

Initialize a reader from its implementation class.

Parameter
NameDescription
impl std::unique_ptr< AsyncReaderConnection >

Operators

operator=(AsyncReader &&)

Move-only.

Parameter
NameDescription
AsyncReader &&
Returns
TypeDescription
AsyncReader &

operator=(AsyncReader const &)

Move-only.

Parameter
NameDescription
AsyncReader const &
Returns
TypeDescription
AsyncReader &

Functions

Read(AsyncToken)

Retrieves more data from the object.

The returned future becomes satisfied when more data is available. Reading an object can fail even after the download starts, thus this function returns wraps the value with StatusOr<>. A successful end-of-stream is indicated by an invalid token.

Example (with coroutines)
future<std::int64_t> CountLines(AsyncReader reader, AsyncToken token) {
  std::int64_t result = 0;
  while (token.valid()) {
    // Wait for next chunk and throw on error.
    auto [payload, t_] = (co_await reader.Read(std::move(token))).value();
    token = std::move(t_);
    for (absl::string_view c : payload.contents()) {
      result += std::count_if(c.begin(), c.end(), '\n');
    }
  }
  co_return result;
}
Parameter
NameDescription
AsyncToken
Returns
TypeDescription
future< StatusOr< std::pair< ReadPayload, AsyncToken > > >