Class AsyncReader (2.17.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
Name Description
AsyncReader &&

AsyncReader(AsyncReader const &)

Move-only.

Parameter
Name Description
AsyncReader const &

AsyncReader()

Creates a reader that always returns errors.

AsyncReader(std::unique_ptr< AsyncReaderConnection >)

Initialize a reader from its implementation class.

Parameter
Name Description
impl std::unique_ptr< AsyncReaderConnection >

Operators

operator=(AsyncReader &&)

Move-only.

Parameter
Name Description
AsyncReader &&
Returns
Type Description
AsyncReader &

operator=(AsyncReader const &)

Move-only.

Parameter
Name Description
AsyncReader const &
Returns
Type Description
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
Name Description
AsyncToken
Returns
Type Description
future< StatusOr< std::pair< ReadPayload, AsyncToken > > >