3

我正在尝试将boost::beast::websocket::stream<T>(针对 2 个特定T的)包装在 aboost::variant中,以允许将 TLS ( [T = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>]) 和非 TLS ( [T = boost::asio::ip::tcp::socket]) websocket 视为相同。我陷入了编译失败。

我能想到的最简单的失败示例是:

#include <boost/asio/ssl.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/variant.hpp>

using tcp = boost::asio::ip::tcp;
namespace ws = boost::beast::websocket;
namespace ssl = boost::asio::ssl;

using base_ws = boost::variant<
    ws::stream<tcp::socket>, ws::stream<ssl::stream<tcp::socket>>>;

class test
{
 public:
    static void init( tcp::socket &&  socket )
    {
        ssl::context  ctx{ ssl::context::tlsv12_server };
        ws::stream<ssl::stream<tcp::socket>>  s{ std::move( socket ), ctx };
        base_ws{ std::move( s ) };
    }
};

int main()
{}

无法编译并出现错误:

In file included from /server/src/ws_server.cpp:9:
In file included from /include/boost/beast/websocket.hpp:18:
In file included from /include/boost/beast/websocket/stream.hpp:3455:
/include/boost/beast/websocket/impl/stream.ipp:47:7: error: no matching constructor for initialization of 'boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >'
    : stream_(std::forward<Args>(args)...)
      ^       ~~~~~~~~~~~~~~~~~~~~~~~~
/include/boost/variant/detail/initializer.hpp:122:27: note: in instantiation of function template specialization 'boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > >::stream<boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > >' requested here
                new(dest) value_T( boost::detail::variant::move(operand) );
                          ^
/include/boost/variant/variant.hpp:1687:28: note: in instantiation of member function 'boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >, boost::mpl::l_iter<boost::mpl::list2<boost::beast::websocket::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >, boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > > > >::initializer_node, mpl_::int_<1> >, boost::mpl::l_iter<boost::mpl::list1<boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > > > >::initializer_node::initialize' requested here
              initializer::initialize(
                           ^
/include/boost/variant/variant.hpp:1858:9: note: in instantiation of function template specialization 'boost::variant<boost::beast::websocket::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >, boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > >::convert_construct<boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > >' requested here
        convert_construct( detail::variant::move(operand), 1L);
        ^
/server/src/ws_server.cpp:372:20: note: in instantiation of function template specialization 'boost::variant<boost::beast::websocket::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >, boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > >::variant<boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > >' requested here
            return base_ws( std::move( s ) );
                   ^
/include/boost/asio/ssl/stream.hpp:64:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > >' to 'const boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >' for 1st argument
class stream :
      ^
/include/boost/asio/ssl/stream.hpp:98:3: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided
  stream(Arg&& arg, context& ctx)
  ^

我会提供godbolt,但是它会超时,而coliru 不提供.openssl 所需的openssl boost/asio/ssl.hpp

Per hereboost::variant应该接受并T &&使用它在T内部实例化。

Per here确实boost::beast::websocket::stream有一个移动构造函数。

移动构造函数更具体,那么为什么编译器会选择stream<...>第一个注释中所示的可变参数构造函数()?

boost::beast::websocket::stream<
  boost::asio::ssl::stream<
    boost::asio::basic_stream_socket<boost::asio::ip::tcp>
  >
>::stream<
  boost::beast::websocket::stream<
    boost::asio::ssl::stream<
      boost::asio::basic_stream_socket<boost::asio::ip::tcp>
    >
  >
>

我正在使用 Boost 1.66.0 和 Clang 7。

4

2 回答 2

2

我忽略的关键信息在以下描述中boost::beast::websocket::stream::stream(stream&&)

如果NextLayer是 moveconstructible,此函数将从现有流中移动构造一个新流。

事实证明,它boost::asio::ssl::stream是不可移动构造的,因此stream没有生成默认的移动构造函数。在引用boost bugtracker 的chriskohlhoff/asio#124中有一个开放的问题可以使ssl::streammove 可构造。

那么问题的解决方案是使用 的移动构造实现,就像这里ssl::stream的 Beast 示例中提供的那样,看起来它可能在更高版本的 Beast 中可用(不在 1.67.0 中)。boost/beast/experimental/core/ssl_stream.hpp

于 2018-07-29T22:43:20.783 回答
0

advanced-server-flex示例展示了如何使用处理 SSL 和普通 websocket 会话的代码构建服务器,而无需求助于变体:https ://github.com/boostorg/beast/blob/c2ecba968c06a22a61c67b8887f5b477bb32a99a/example /advanced/server-flex/advanced_server_flex.cpp#L227

于 2018-07-30T08:47:19.773 回答