为什么没有asio :: ssl :: iostream? (以及如何实施) [英] Why is there no asio::ssl::iostream? (and how to implement it)

查看:93
本文介绍了为什么没有asio :: ssl :: iostream? (以及如何实施)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究Asio库,并且具有用于常规TCP连接的工作代码.我使用了asio::ip::tcp::iostream对象,因为我想传输的内容已经可以从iostream序列化到/反序列化,所以这确实很方便并且对我来说很好.

I'am currently exploring the Asio library and have working code for regular TCP connections. I used asio::ip::tcp::iostream objects since stuff I want to transmit already can serialize to/deserialize from iostreams, so this was really handy and worked well for me.

然后我尝试切换到SSL连接,那时一切都变得疯狂了.显然没有内置支持来获得与所有其他协议支持的安全连接相同的iostream接口.从设计的角度来看,这确实让我感到困惑.有什么理由吗?

I then tried to switch to SSL connections and that's when everything turned crazy. There is apparently no built-in support to get the same iostream interface that all other protocols support for a secured connection. From a design perspective this is really perplexing to me. Is there any reason why this is the case?

我知道>如何创建Boost ssl iostream中的讨论? 以包装器类结束,以使用boost提供iostream功能.除此之外,根据评论,该实现是有缺陷的,这也没有提供与其他协议(a basic_socket_iostream)相同的接口,这也允许例如设置到期时间并关闭连接. (我也在非增强版中使用asio,并希望尽可能避免将boost作为附加依赖项添加.)

I am aware of the discussion in How to create a boost ssl iostream? which concludes with a wrapper class to provide iostream functionality using boost. Apart from that, according to a comment, the implementation is flawed, this also does not give the same interface as for the other protocols (a basic_socket_iostream) which also allows to e.g., set expiration times and close the connection. (I am also using asio in the non-boost version and want to avoid adding boost as an additional dependency if possible).

所以,我想我的问题是:

So, I guess my questions are:

  1. 要为SSL连接获取basic_socket_iostream,我到底需要实现什么?我认为这是asio::basic_streambufasio::basic_socket_streambuf的派生产品,但我不知何故无法弄清楚它们是如何工作的,需要进行调整.不清楚何时确切实现目标...
  2. 为什么这首先没有出现?将此协议的行为与其他协议完全不同似乎非常不合理,因此需要大量重构来更改基于tcp::iostream的项目以支持安全连接
  1. What exactly would I need to implement to get a basic_socket_iostream for an SSL connection? I assume it would be a derivation of asio::basic_streambuf or asio::basic_socket_streambuf but I somehow can't figure out how they work and need to be tweaked.. there's just a bunch of weird pointer movement and buffer allocations and documentation to me is quite unclear on what happens when exactly to achieve what...
  2. Why is this not already present in the first place? It seems very unreasonable to have this one protocol behave entirely different from any other and thus require major refactoring for changing a tcp::iostream based project to support secured connections

推荐答案

>好吧,我的问题是ssl :: stream确实没有:我没有提供套接字,但也没有提供与其他协议兼容的流接口而且,是的,从这个意义上说,它的行为与其他行为有很大不同(没有明显的原因)

> Well, the problem I have is that the ssl::stream really does neither: I doesn't give a socket but it also doesn't give me a stream interface that would be compatible to those available from the other protocols and, yes, in that sense it behaves very differently from the others (for no apparent reason)

我认为流的行为与其他协议没有什么不同(请参阅 https://www.boost. org/doc/libs/1_66_0/doc/html/boost_asio/overview/core/streams.html ):

I don't think the stream behaves any differently from the other protocols (see https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/overview/core/streams.html):

流,短读和短写

Boost.Asio中的许多I/O对象都是面向流的.这意味着:

Streams, Short Reads and Short Writes

Many I/O objects in Boost.Asio are stream-oriented. This means that:

没有消息边界.正在传输的数据是连续的字节序列.
读或写操作可能传输的字节数少于请求的字节数.这称为短读或短写.
提供面向流的I/O的对象将满足以下一种或多种类型要求:

There are no message boundaries. The data being transferred is a continuous sequence of bytes.
Read or write operations may transfer fewer bytes than requested. This is referred to as a short read or short write.
Objects that provide stream-oriented I/O model one or more of the following type requirements:

  • SyncReadStream,其中使用称为read_some()的成员函数执行同步读取操作.
  • AsyncReadStream,其中使用称为async_read_some()的成员函数执行异步读取操作.
  • SyncWriteStream,其中使用称为write_some()的成员函数执行同步写操作.
  • AsyncWriteStream,其中使用称为async_write_some()的成员函数执行同步写操作.
  • SyncReadStream, where synchronous read operations are performed using a member function called read_some().
  • AsyncReadStream, where asynchronous read operations are performed using a member function called async_read_some().
  • SyncWriteStream, where synchronous write operations are performed using a member function called write_some().
  • AsyncWriteStream, where synchronous write operations are performed using a member function called async_write_some().

面向流的I/O对象的示例包括ip::tcp::socketssl::stream<>posix::stream_descriptorwindows::stream_handle等.

Examples of stream-oriented I/O objects include ip::tcp::socket, ssl::stream<>, posix::stream_descriptor, windows::stream_handle, etc.

也许混淆之处在于您正在将其与iostream接口进行比较,这根本不是一个相同的概念(它来自标准库).

Perhaps the confusion is that you're comparing to the iostream interface, which is simply not the same concept (it comes from the standard library).

对于如何为ssl流制作iostream兼容流包装器的问题,我无法在不进一步参考文档并使用编译器的情况下设计出答案,这是我目前手头没有的.

To the question how you could make a iostream compatible stream wrapper for the ssl stream, I cannot devise an answer without consulting the documentations more and using a compiler, which I don't have on hand at the moment.

这篇关于为什么没有asio :: ssl :: iostream? (以及如何实施)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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