Boost C++ 库

……世界上最受推崇、设计最精良的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu, C++ Coding Standards

async_write(8 个重载中的第 7 个) - Boost C++ 函数库
PrevUpHomeNext

启动一个异步操作,将所有提供的写入数据流。

template<
    typename AsyncWriteStream,
    typename DynamicBuffer_v2,
    typename WriteToken = default_completion_token_t<typename AsyncWriteStream::executor_type>>
DEDUCED async_write(
    AsyncWriteStream & s,
    DynamicBuffer_v2 buffers,
    WriteToken && token = default_completion_token_t< typename AsyncWriteStream::executor_type >(),
    constraint_t< is_dynamic_buffer_v2< DynamicBuffer_v2 >::value >  = 0,
    constraint_t< !is_completion_condition< decay_t< WriteToken > >::value >  = 0);

此函数用于将特定数量的字节数据异步写入流。它是 异步操作 的发起函数,并且始终立即返回。异步操作将继续进行,直到满足以下任一条件:

  • supplied 动态缓冲区序列中的所有数据都已写入。
  • 发生错误。

此操作通过调用零次或多次流的 async_write_some 函数来实现,并被称为 复合操作。程序必须确保在操作完成之前,流不再执行其他写入操作(例如 async_write、流的 async_write_some 函数或执行写入的任何其他复合操作)。

参数

s

要写入数据的流。类型必须支持 AsyncWriteStream 概念。

缓冲区

将从中写入数据的动态缓冲区序列。虽然缓冲区对象可以根据需要复制,但底层内存块的所有权仍由调用者保留,调用者必须保证这些内存块在调用完成处理程序之前保持有效。成功写入的数据将从缓冲区中自动消耗。

标记

用于生成完成处理程序的 完成标记,当写入完成时将调用该处理程序。可能的完成标记包括 use_futureuse_awaitableyield_context,或者具有正确完成签名的函数对象。完成处理程序的函数签名必须是

void handler(
  // Result of operation.
  const boost::system::error_code& error,

  // Number of bytes written from the buffers. If an error
  // occurred, this will be less than the sum of the buffer sizes.
  std::size_t bytes_transferred
);

无论异步操作是立即完成还是不立即完成,都不会在此函数内部调用完成处理程序。在立即完成的情况下,处理程序的调用将以等效于使用 async_immediate 的方式执行。

完成签名
void(boost::system::error_code, std::size_t)
按操作取消

此异步操作支持以下 cancellation_type 值的取消

  • cancellation_type::terminal
  • cancellation_type::partial

如果它们也由 AsyncWriteStream 类型的 async_write_some 操作支持。


PrevUpHomeNext