I am trying to write a https flex server, that can upgrade to websocket based on upgrade request. https class does the ssl handshake on std::shared_ptr<boost::beast::ssl_stream<boost::beast::tcp_stream>> m_ptls_stream
Now I need to transfer this stream to websocket class and transform it into type
std::shared_ptr<boost::beast::websocket::stream<
boost::beast::ssl_stream<boost::beast::tcp_stream>>>
But for some reason the constructor of websocket stream doesn't accept a shared pointer, and I am unable to dereference the ssl_stream shared_ptr as I get the error that the copy constructor is deleted
Severity Code Description Project File Line Suppression State Error C2280 'boost::beast::ssl_streamboost::beast::tcp_stream::ssl_stream(const boost::beast::ssl_streamboost::beast::tcp_stream &)': attempting to reference a deleted function D:\Work\remote_pc\out\build\x64-Debug\remote_pc D:\Work\boost_1_73_0\boost\asio\impl\executor.hpp 218
void async_ws_client::add_stream(std::shared_ptr<boost::beast::ssl_stream<boost::beast::tcp_stream>>&& ptls_stream)
{
if (m_ptls_context)
{
m_p_wss_stream = std::make_shared<
boost::beast::websocket::stream<
boost::beast::ssl_stream<
boost::beast::tcp_stream>>>(std::move(*ptls_stream), *m_ptls_context);
}
}
Feels like im missing something, unable to figure it out for a couple of days. Please help..!!
Also, if I do it this way
m_p_wss_stream = std::make_shared<
boost::beast::websocket::stream<
boost::beast::ssl_stream<
boost::beast::tcp_stream>>>(std::move(ptls_stream->next_layer()),
*m_ptls_context);
The socket throws error : uninitialized when I do async_accept() on the stream after creating it.
Dereferencing works. It's just that you can't construct the type you want from the arguments you pass.
Simplifying your code so it becomes readable and self-contained:
Live On Coliru
The error novel hides the messages deep inside the shared_ptr construction forwarding machinery. However, the equivalent, much simpler code also will not compile, and for the same reason:
Live On Coliru
A quick glance at your link tells me that ought to have been more like: Live On Coliru
Now, you can translate that back to the fully shared_ptr-overgrown version of the code:
Note though that (ab)using shared pointers at this scale is a bit of an anti-pattern. You should probably consider
unique_ptr
oroptional
(for lazy-constructed types), or indeedenable_shared_from_this
for the entire class, so all members have shared ownership by extension. This is also used in the linked documentation sample.