Is there a boost::iostreams (bidirectional) Device for a blocking boost::asio TCP connection?

南楼画角 提交于 2019-12-10 16:38:56

问题


I'm surveying c++ libraries for portable, blocking I/O access to the filesystem and network. It looks like boost::filesystem, boost::iostreams and boost::asio will, between the three of them, do the job.

To be clear, I'm not currently interested in the asynchronous aspects of boost::asio; I just want a portable, blocking interface to the network.

Digging in, I see boost::iostreams has a notion of Devices, each of which has an associated mode concept. The bidirectional mode specifically seems hand-tailored for streaming access to a full-duplex TCP connection. Awesome.

boost::iostreams does not seem to offer support for actually opening TCP connections (unlike the local filesystem.) That's fine, surely boost::asio will let me open the connection, appropriately model it as a bidirectional Device, and wrap it in a boost::iostreams::stream.

..except it won't? I see boost::asio::ip::tcp::iostream, which would replace the boost::iostreams::stream, but presumably not act as a Device.

I understand the tcp::iostream would act similarly, but I would still prefer to learn and code against just one interface, not two. Specifically, dealing with two error handling regimes & exception hierarchies is not very palatable.

So, the question: am I blind? Maybe an adapter between the two libraries exists, that I missed googling around. Or perhaps someone has already released such an adapter as a 3rd-party component I could drop in?


回答1:


I'm not aware of a direct mapping. However, if you were interested, writing such a device is fairly straightforward. This version throws boost::system::system_error for non-EOF errors, but you could choose to do something else.

#include <iosfwd>

#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/iostreams/categories.hpp>
#include <boost/system/system_error.hpp>


class asio_stream_device
{
public:
    typedef char char_type;
    typedef boost::iostreams::bidirectional_device_tag category;

    explicit asio_stream_device(boost::asio::ip::tcp::socket& sock) : socket_(sock)
    {

    }

    std::streamsize read(char* s, std::streamsize n)
    {
        // Read up to n characters from the underlying data source
        // into the buffer s, returning the number of characters
        // read; return -1 to indicate EOF
        boost::system::error_code ec;

        std::size_t rval = socket_.read_some(boost::asio::buffer(s, n), ec);
        if (!ec)
        {
            return rval;
        }
        else if (ec == boost::asio::error::eof)
        {
            return -1;
        }
        else
        {
            throw boost::system::system_error(ec,"read_some");
        }

    }


    std::streamsize write(const char* s, std::streamsize n)
    {
        // Write up to n characters to the underlying
        // data sink into the buffer s, returning the
        // number of characters written

        boost::system::error_code ec;
        std::size_t rval = socket_.write_some(boost::asio::buffer(s, n), ec);
        if (!ec)
        {
            return rval;
        }
        else if (ec == boost::asio::error::eof)
        {
            return -1;
        }
        else
        {
            throw boost::system::system_error(ec,"write_some");
        }

    }



private:

    boost::asio::ip::tcp::socket& socket_;

};

Basically, open/connect the socket as normal, then pass it to the constructor. The example simply reads and outputs to the screen.

void test
{
   namespace asio = boost::asio;
   namespace io = boost::iostreams;

   asio::io_service service;
   asio::ip::tcp::socket socket(service);


   asio::ip::tcp::endpoint remote -  ...; ////

   socket.connect(remote);

   io::stream<asio_stream_device> str(socket);

   std::string line;

   while (std::getline(str, line)) {
    std::cout << line << std::endl;
   }
}


来源:https://stackoverflow.com/questions/12023166/is-there-a-boostiostreams-bidirectional-device-for-a-blocking-boostasio-tc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!