在异步读取调用时递增升压数组地址

发布于 2025-01-06 19:54:19 字数 1345 浏览 1 评论 0原文

当将其作为参数传递时,有什么方法可以将地址增加到 boost::array 吗?

准确地说,我提供了一个缓冲区-> boost::array<字符,4096>缓冲区。

我将此缓冲区传递给 socket::async_read_some 调用以填充它。现在,当我的 async_read_some 完成时,缓冲区不一定已满(因为我可以使用 bytes_transferred 检查)。现在,如果发生这种情况,我想再次调用 async_read_some 来进一步填充缓冲区。

现在,如果我像平常一样传递缓冲区,它就会覆盖其中的内容。但我希望这些函数在第一次调用后结束的位置填充缓冲区。不幸的是,我不能只传递递增的地址 buffer+bytes_transferred 这对于普通的 char* 缓冲区是可能的。

这里是我想要做的一些(简化的)示例代码 - 请假设所有内容都已初始化并且 io_service 正在运行。

void myClass::startRead()
{
    mySocket.async_read_some(boost::asio::buffer(buffer,4096)),
         boost::bind(&myClass::handle_read, shared_from_this(),
               boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}

void myClass::handle_read(const boost::system::error_code& err ,size_t bytesTransferred)
{
    if ( bytes_transferred < 4096 )
    {
            mySocket.async_read_some(/* address of buffer + bytes_transferred */
                         boost::bind(&myClass::handle_read, shared_from_this(),
                              boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
    }
}

我知道这个示例仅适用于一次调用(然后我必须将已传输的字节保存在另一个变量中),但我只是想澄清我的观点。

那么有人可以帮助我如何做到这一点吗?

is there any way I can increment the adress to a boost::array when passing it as an argument?

To be precise, I provide a buffer -> boost::array<char, 4096> buffer.

This buffer I pass to a socket::async_read_some call to get filled by it. Now when my async_read_some finishes the buffer is not necessarily full ( as I can check with bytes_transferred ). Now if that happens, I want to call async_read_some again to fill the buffer further.

Now if I pass the buffer like normal it will just overwrite the stuff in it. But I want that the functions fills the buffer at the position wherei t ended after the first call. Unfortunately I can't just pass the incremented adress buffer+bytes_transferred which would be possible with a normal char* buffer.

Here some ( simplified ) example code of what I want to do - please presume that all stuff has been initialized and io_service is running.

void myClass::startRead()
{
    mySocket.async_read_some(boost::asio::buffer(buffer,4096)),
         boost::bind(&myClass::handle_read, shared_from_this(),
               boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}

void myClass::handle_read(const boost::system::error_code& err ,size_t bytesTransferred)
{
    if ( bytes_transferred < 4096 )
    {
            mySocket.async_read_some(/* address of buffer + bytes_transferred */
                         boost::bind(&myClass::handle_read, shared_from_this(),
                              boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
    }
}

I know this example would work for only one call ( then I would have to save the alread transferred bytes in another variable ) but I just wanted to make my point clear.

So anyone has some help how I could do this?

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

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

发布评论

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

评论(2

云之铃。 2025-01-13 19:54:19

您可以将 async_read 与包含数据大小的标头一起使用。然后再次调用 async_read 并获取读取大小。我只是在其他帖子中给出了一些例子,只需采取一个看看

You can use async_read with a header that contain data size. Then call again async_read with the read size. I just give some exemples on other post, just take a look.

岁月染过的梦 2025-01-13 19:54:19

从技术上讲,boost::array 有 c_array() 成员函数,允许您访问底层的 c 数组。所以你可以写类似 asio::buffer(buffer.c_array() + bytesTransferred, buffer.size() - bytesTransferred))

但使用 asio::streambuf 会更安全

Technically, boost::array has c_array() member function that allows you to access the underlying c-array. So you can write something like asio::buffer(buffer.c_array() + bytesTransferred, buffer.size() - bytesTransferred))

But it would be more safe to use asio::streambuf.

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