将安全套接字作为普通套接字传递到升压中

pass secure socket as normal socket in boost

本文关键字:套接字 安全套 安全      更新时间:2023-10-16

我有这些类型定义,问题是我需要传递一个安全套接字,因为 TSocket 会从 TSecureSocket 直接投射到 TSocket 工作吗? 还是有其他解决方案? 根据端口的不同,我将使套接字安全,而在其他情况下则不会。我只需要返回类型是 TSocket。

typedef boost::asio::ip::tcp::socket            TBoostSocket;
typedef boost::asio::ssl::stream<TBoostSocket>  TSLLSocket;
typedef boost::shared_ptr<TBoostSocket>         TSocket;
typedef boost::shared_ptr<TSLLSocket>           TSecureSocket;

我看过这个 提升::ASIO 将套接字转换为安全套接字

我有这些类型定义

typedef boost::asio::ip::tcp::socket            TBoostSocket;
typedef boost::asio::ssl::stream<TBoostSocket>  TSLLSocket;
typedef boost::shared_ptr<TBoostSocket>         TSocket;
typedef boost::shared_ptr<TSLLSocket>           TSecureSocket;

问题是我需要传递一个安全套接字作为 TSocket。将直接 从插座到工作

简答题 否。因为boost::asio::ssl::stream<TBoostSocket>包裹了插座。

还是有其他解决方案? 根据端口,我将使 套接字安全,在其他情况下,我不会,我只需要返回类型 是索克特。

但是,TSLLSocket(或 boost::asio::ssl::stream )提供了一种从实例中检索套接字的方法:

const next_layer_type & next_layer() const;

http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/reference/ssl__stream/next_layer/overload1.html

其中next_layer_type由以下 typedef 定义:

typedef boost::remove_reference< Stream >::type next_layer_type;

http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/reference/ssl__stream/next_layer_type.html

由于您使用TBoostSocket定义模板,因此当您调用next_layer()lowest_layer()时,您应该得到

当然,这将返回一个引用而不是一个指针,并且该引用是指向不属于您的实例。因此,您现在需要以某种方式将其包装在shared_ptr中,这可能并不容易,因为您不允许将其删除。