使用 boost::asio 丢弃数据

发布于 2024-11-30 23:06:59 字数 305 浏览 1 评论 0原文

我在异步模式下使用 boost::asio,并且我想跳过/丢弃/删除通过 TCP 发送给我的消息。我想这样做是因为我已经阅读了该消息的标题,并且我知道我对此不感兴趣。该消息可能很大,因此我宁愿不为其分配空间,甚至最好根本不将其传输到用户空间。

我看到 boost::asio::null_buffers 但它似乎不适用于此处(请参阅 https ://svn.boost.org/trac/boost/ticket/3627)。

I'm using boost::asio in asynchronous mode and I'd like to skip/discard/drop a message that has been sent to me over TCP. I want to do this because I've already read the header for the message and I know that it is of no interest to me. The message may be large so it I would prefer not to allocate space for it and even better not to transfer it into user space at all.

I see boost::asio::null_buffers but it does not appear to be applicable here (see https://svn.boost.org/trac/boost/ticket/3627).

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

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

发布评论

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

评论(2

绮烟 2024-12-07 23:06:59

据我所知,BSD 套接字接口不提供此功能。你总是必须读入缓冲区。现在,为了不分配巨大的缓冲区,您可以做的就是在循环中读入较小的缓冲区。像这样的事情:

void skip_impl(tcp::socket& s, int n, boost::function<void(error_code const&)> h
    , char* buf, error_code const& ec, std::size_t bytes_transferred)
{
    assert(bytes_transferred <= n);
    n -= bytes_transferred;
    if (ec || n == 0) {
        delete[] buf;
        h(ec);
        return;
    }

    s.async_read_some(boost::asio::buffer(temp, std::min(4096, n))
        , boost::bind(&skip_impl, boost::ref(s), n, h, temp, _1, _2));
}

void async_skip_bytes(tcp::socket& s, int n, boost::function<void(error_code const&)> h)
{
    char* temp = new char[4096];
    s.async_read_some(boost::asio::buffer(temp, std::min(4096, n))
        , boost::bind(&skip_impl, boost::ref(s), n, h, temp, _1, _2));
}

这还没有通过编译器传递,因此可能存在愚蠢的拼写错误,但它应该说明这一点。

As far as I know, the BSD socket interface doesn't give you this functionality. You always have to read into a buffer. Now, what you can do in order to not allocate a huge buffer is to read into a smaller buffer in a loop. Something like this:

void skip_impl(tcp::socket& s, int n, boost::function<void(error_code const&)> h
    , char* buf, error_code const& ec, std::size_t bytes_transferred)
{
    assert(bytes_transferred <= n);
    n -= bytes_transferred;
    if (ec || n == 0) {
        delete[] buf;
        h(ec);
        return;
    }

    s.async_read_some(boost::asio::buffer(temp, std::min(4096, n))
        , boost::bind(&skip_impl, boost::ref(s), n, h, temp, _1, _2));
}

void async_skip_bytes(tcp::socket& s, int n, boost::function<void(error_code const&)> h)
{
    char* temp = new char[4096];
    s.async_read_some(boost::asio::buffer(temp, std::min(4096, n))
        , boost::bind(&skip_impl, boost::ref(s), n, h, temp, _1, _2));
}

This has not been passed through a compiler, so there might be silly typos, but it should illustrate the point.

夏末 2024-12-07 23:06:59

Boost的Asio是一个我自己还没有使用过的库。所以我不知道是否任何带有 Sink 接口的东西都可以插入。但如果是这样,boost 的 null_sink 是否足以适合您的情况......?

http://www.boost.org/doc /libs/1_46_1/libs/iostreams/doc/classes/null.html

(它只会丢弃数据,所以你仍然在用户空间中。如果还有一种方法可以做到这一点,但如果可以的话那就太酷了。)

Boost's Asio is a library I haven't used myself yet. So I don't know if just anything with a Sink interface can plug in. But if so, would boost's null_sink work well enough for your situation...?

http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/classes/null.html

(It would just throw the data away, so you're still in user space. I'd be surprised if there's a way to do otherwise, but would be cool if you could.)

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