有boost :: iostreams(双向)设备阻塞boost :: asio TCP连接? [英] Is there a boost::iostreams (bidirectional) Device for a blocking boost::asio TCP connection?

查看:522
本文介绍了有boost :: iostreams(双向)设备阻塞boost :: asio TCP连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调查c ++库,以便对文件系统和网络进行可移植的阻塞I / O访问。 看起来像 boost :: filesystem boost :: iostreams boost :: asio 会在三个人之间做这项工作。



要清楚,我目前不感兴趣在异步方面 boost :: asio ;



进入,我看到 boost :: iostreams 设备的概念,每个设备都有相关的模式概念。双向模式特别看起来是手工定制的,用于流式访问全双工TCP连接。真棒。



boost :: iostreams 似乎不支持实际打开TCP连接。)很好,当然 boost :: asio 会让我打开连接,将它作为双向设备 ,并将它包装在 boost :: iostreams :: stream



..我看到 提升:: asio :: ip :: tcp :: iostream ,它将替换 boost :: iostreams :: stream 可能不会作为设备



我理解 tcp :: iostream 会做类似的,但我仍然更喜欢学习和代码只是一个接口,而不是两个。具体来说,处理两个错误处理机制&例外层次结构不是很美观。



所以,问题:我盲目吗?也许两个库之间存在一个适配器,我错过了google。

解决方案

我不知道的直接映射。然而,如果你有兴趣,写这样的设备是相当简单。对于非EOF错误,此版本会抛出 boost :: system :: system_error ,但您可以选择执行其他操作。

  #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)
{
//从底层数据源读取n个字符
//到缓冲区s,返回字符数
// read;返回-1表示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)
{
//写入基础的
//数据的n个字符到缓冲区中,返回
//写入的字符数

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&插座_;

};

基本上,像正常一样打开/连接套接字,然后传递给构造函数。该示例只是读取并输出到屏幕。

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

asio :: io_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<线<< std :: endl;
}
}


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?

解决方案

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;
   }
}

这篇关于有boost :: iostreams(双向)设备阻塞boost :: asio TCP连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆