【问题标题】:pass secure socket as normal socket in boost在 boost 中将安全套接字作为普通套接字传递
【发布时间】:2012-11-26 12:50:04
【问题描述】:

我有这些 typedef 问题是我需要传递一个安全套接字,因为 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;

我看过这个 boost::asio convert socket to secure

【问题讨论】:

    标签: c++ boost-asio shared-ptr


    【解决方案1】:

    我有这些类型定义

      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 传递。会直接 从 TSecureSocket 转换为 TSocket 工作?

    简答否。因为boost::asio::ssl::stream&lt;TBoostSocket&gt; 包裹了套接字。

    或者还有其他解决方案吗?根据我将制作的端口 套接字安全,而在其他情况下,我不会只需要返回类型 是 TSocket。

    但是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 中,这可能不是那么容易,因为你不能允许它被删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 2023-03-11
      相关资源
      最近更新 更多