boost.asio - 设置最大读取流大小

发布于 2024-09-30 17:06:44 字数 1155 浏览 1 评论 0原文

http 中有示例 HTTP 客户端: //www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/example/http/client/async_client.cpp 请帮助我更改最大缓冲区大小,如以下代码中所述(它来自随库下载的示例,而不是从站点):

void handle_write_request(const boost::system::error_code& err)
{
  if (!err)
  {
    // Read the response status line. The response_ streambuf will
    // automatically grow to accommodate the entire line. The growth may be
    // limited by <b>passing a maximum size to the streambuf constructor</b>.
    boost::asio::async_read_until(socket_, response_, "\r\n",
        boost::bind(&client::handle_read_status_line, this,
          boost::asio::placeholders::error));
  }
  else
  {
    std::cout << "Error: " << err.message() << "\n";
  }
}

这是响应缓冲区的构造函数:

boost::asio::streambuf response_;

但编译器表示以下代码无效:

boost::asio::streambuf response_(1024);

看来默认缓冲区是512 字节大小,我需要更大的大小。

There's example HTTP Client at http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/example/http/client/async_client.cpp
Please help me to change maximum buffer size like explained in following code (it's from examples downloaded with library, not from site):

void handle_write_request(const boost::system::error_code& err)
{
  if (!err)
  {
    // Read the response status line. The response_ streambuf will
    // automatically grow to accommodate the entire line. The growth may be
    // limited by <b>passing a maximum size to the streambuf constructor</b>.
    boost::asio::async_read_until(socket_, response_, "\r\n",
        boost::bind(&client::handle_read_status_line, this,
          boost::asio::placeholders::error));
  }
  else
  {
    std::cout << "Error: " << err.message() << "\n";
  }
}

And here's the constructor of response buffer:

boost::asio::streambuf response_;

But compiler says that following code is invalid:

boost::asio::streambuf response_(1024);

It seems that default buffer is 512 bytes sized, I need larger size.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放肆 2024-10-07 17:06:44

1)我不确定您的 512 字节限制来自哪里,因为 asio::basic_streambuf 的构造函数具有以下签名(这允许它存储超过 512 或 1024 字节的方式):

explicit basic_streambuf(
    std::size_t max_size = (std::numeric_limits<std::size_t>::max)(),
    const Allocator& allocator = Allocator())

2) 此代码 boost::asio::streambuf response_(1024); 无效,因为您无法在声明点初始化成员变量,您必须在构造函数的初始值设定项列表或其主体中执行此操作。如果不这样做,它将被默认初始化。

3)代码中的注释引用了限制/限制streambuf的最大大小 - 所以它绝对不会帮助你获得“更大的大小”,相反。

1) I'm not sure where your 512 bytes limit is coming from since the constructor for the asio::basic_streambuf has the following signature (which allows it to store way more than 512 or 1024 bytes):

explicit basic_streambuf(
    std::size_t max_size = (std::numeric_limits<std::size_t>::max)(),
    const Allocator& allocator = Allocator())

2) This code boost::asio::streambuf response_(1024); is invalid since you cannot initialize member variables at declaration point, you must do it in the constructor's initializer list or it's body. If you don't it will be default initialized.

3) The comment in the code referrers to limiting/restricting the maximum size of the streambuf - so it will definitely not help you get "a larger size", on the contrary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文