Boost asio:是否可以将接受的 tcp 套接字转换为 basic_socket_iostream (或以某种方式从中获取它)?

发布于 2024-12-13 17:59:32 字数 492 浏览 0 评论 0 原文

所以这是我的问题:我们已经接受一个 boost asio tcp 套接字。我们所有的 API 都使用它。我们只需要在一个函数中执行这样的“超时读取”即可。

可以执行此类“超时读取”,如下所示 这里。但是我从我的 API 中获得了 boost asio tcp 套接字,所以我想知道是否有可能将 Boost::asio 套接字变成 basic_socket_iostream。怎么做呢?

So here is my problem: we already have accepted a boost asio tcp socket. And all of our APIs use it. And we just need to do such 'timeouted read' in one function.

It is possible to do such 'timeouted read' as shown here. But I get boost asio tcp socket from my APIs so I wonder if it was any how possible to turn Boost::asio socket into basic_socket_iostream. How to do it?

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

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

发布评论

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

评论(3

梦巷 2024-12-20 17:59:32

我想知道是否有可能将 Boost::asio 套接字变成
basic_socket_iostream。怎么做?

您可以使用 tcp: :iostream::rdbuf() 获取指向 socket::basic_socket_streambuf 的指针然后分配()< /code> 描述符。

#include <boost/asio.hpp>

int
main()
{
    boost::asio::io_service io_service;
    boost::asio::ip::tcp::socket socket( io_service );
    boost::asio::ip::tcp::iostream stream;
    stream.rdbuf()->assign( boost::asio::ip::tcp::v4(), socket.native_handle() );
}

虽然这非常很复杂,但我强烈建议不要走这条路。作为拉尔夫的回答表明了,并且我对您所建议的链接问题的回答,您确实应该为此使用deadline_timer

I wonder if it was any how possible to turn Boost::asio socket into
basic_socket_iostream. How to do it?

you can use tcp::iostream::rdbuf() to get a pointer to the socket::basic_socket_streambuf then assign() the descriptor.

#include <boost/asio.hpp>

int
main()
{
    boost::asio::io_service io_service;
    boost::asio::ip::tcp::socket socket( io_service );
    boost::asio::ip::tcp::iostream stream;
    stream.rdbuf()->assign( boost::asio::ip::tcp::v4(), socket.native_handle() );
}

Though this is very convoluted and I strongly suggest not going this route. As Ralf's answer indicated, and my answer to your linked question suggested, you really should be using a deadline_timer for this.

嘿咻 2024-12-20 17:59:32

您没有说明是否要求不使用异步 API,因此这可能对您有帮助,也可能不会:

除非我误解了您,否则我认为您引用的 SO 答案正在考虑不同的用例,即使用 tcp 超时::iostream。如果您只是想为读取添加超时,请查看 asio 文档中的超时示例?这是使套接字操作超时的标准方法。

You don't state whether there is a requirement not to use the asynchronous APIs so perhaps this helps you and perhaps not:

Unless I'm misunderstanding you, I think the SO answer you reference is looking at a different use case i.e. timeout using tcp::iostream. If you simply want to add a timeout to a read, have you looked at the timeout examples in the asio documentation? This is the standard approach to timeout a socket operation.

回眸一遍 2024-12-20 17:59:32

对于遇到此问题的其他人,在尝试使其工作几个小时后,我决定使用 Boost“Devices”编写一个简单的包装类:
http://www.boost.org/ doc/libs/1_64_0/libs/iostreams/doc/tutorial/container_source.html

#include <iostream>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/concepts.hpp> 

using boost::asio::ip::tcp;

typedef boost::shared_ptr<tcp::socket> socket_ptr;

class my_source : public boost::iostreams::source
{
public:
    my_source(socket_ptr sock) : _sock(sock)
    {

    }

    std::streamsize read(char* s, std::streamsize n)
    {
        return _sock->read_some(boost::asio::buffer(s, n));
    }

private:
    socket_ptr _sock;

};

使用示例:

boost::iostreams::stream<my_source> in(sock);

for (;;) {
    std::getline(in, line);
    if (line.length() > 0)
        std::cout << line << std::endl;
    else
        break;
}

For anyone else who will come across this problem, after trying to get this working for hours I decided to write a simple wrapper class using Boost "Devices":
http://www.boost.org/doc/libs/1_64_0/libs/iostreams/doc/tutorial/container_source.html

#include <iostream>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/concepts.hpp> 

using boost::asio::ip::tcp;

typedef boost::shared_ptr<tcp::socket> socket_ptr;

class my_source : public boost::iostreams::source
{
public:
    my_source(socket_ptr sock) : _sock(sock)
    {

    }

    std::streamsize read(char* s, std::streamsize n)
    {
        return _sock->read_some(boost::asio::buffer(s, n));
    }

private:
    socket_ptr _sock;

};

usage example:

boost::iostreams::stream<my_source> in(sock);

for (;;) {
    std::getline(in, line);
    if (line.length() > 0)
        std::cout << line << std::endl;
    else
        break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文