c++ - 为什么没有 asio::ssl::iostream? (以及如何实现)

标签 c++ ssl boost-asio

我目前正在探索 Asio 库,并拥有用于常规 TCP 连接的工作代码。我使用了 asio::ip::tcp::iostream 对象,因为我想要传输的东西已经可以序列化到 iostreams 或从 iostreams 反序列化,所以这真的很方便,对我来说效果很好。

然后我尝试切换到 SSL 连接,那时一切都变得疯狂了。显然没有内置支持来获得所有其他协议(protocol)支持安全连接的相同 iostream 接口(interface)。从设计的角度来看,这真的让我感到困惑。为什么会这样?

我知道 How to create a boost ssl iostream? 中的讨论它以包装器类结束,以使用 boost 提供 iostream 功能。除此之外,根据评论,该实现存在缺陷,这也没有提供与其他协议(protocol)相同的接口(interface)(basic_socket_iostream),它还允许例如设置过期时间并关闭联系。 (我还在非提升版本中使用 asio,并且希望尽可能避免将提升作为附加依赖项添加)。

所以,我想我的问题是:

  1. 要获得用于 SSL 连接的 basic_socket_iostream,我究竟需要实现什么?我认为这将是 asio::basic_streambufasio::basic_socket_streambuf 的派生,但我不知何故无法弄清楚它们是如何工作的,需要进行调整..有只是一堆奇怪的指针移动和缓冲区分配和文档对我来说很不清楚什么时候会发生什么来实现什么...
  2. 为什么它一开始还没有出现?让这个协议(protocol)的行为与任何其他协议(protocol)完全不同似乎是非常不合理的,因此需要进行重大重构以更改基于 tcp::iostream 的项目以支持安全连接

最佳答案

> 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)

我不认为流的行为与其他协议(protocol)有任何不同(参见 https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/overview/core/streams.html ):

Streams, Short Reads and Short Writes

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

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, 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().

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

也许混淆是因为您正在与 iostream 进行比较接口(interface),这根本不是同一个概念(它来自标准库)。

关于如何制作 iostream 的问题ssl 流的兼容流包装器,如果不更多地查阅文档并使用我目前手头没有的编译器,我无法设计出答案。

关于c++ - 为什么没有 asio::ssl::iostream? (以及如何实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54311116/

相关文章:

c++ - 从未定义它的基类调用纯虚函数?

c++ - 显示html页面?

c# - Mono 下的 WCF over TLS

ssl - 来自证书管理器的空 "ca.crt"文件

c++ - 使用 boost::asio 和 c++11 lambda 的简单回调控制

c++ - 在 Windows 中 - API - 如何获取/使用当前用户权限(或单点登录)

c++ - 重用可变类型

amazon-web-services - AWS 的动态 SSL 证书

c++ - 消息顺序不正确(使用 io_service::wrap)

c++ - boost::asio::streambuf 与 wchar_t