提升Asio如何在不使用SSL的SSL套接字上读/写?

use*_*111 5 c++ ssl boost boost-asio

标题是我的问题.

我已经在这里找到了与此相关的主题 - > 在Boost.Asio中同时使用SSL套接字和非SSL套接字?
基本上我处于相同的情况但由于某种原因我不能在那里发表评论和/或直接联系提问者所以我这样做是一个新问题.

我有一个设置ssl套接字ssl::stream<ip::tcp::socket> socket_;,客户端可以很好地连接

socket_.async_handshake(ssl::stream_base::server, session::handle_handshake)
Run Code Online (Sandbox Code Playgroud)

然后用/读/写

async_write(socket_, buffer(send_data, send_length), session::handle_read)
socket_.async_read_some(buffer(recieved_data, max_length), session::handle_read)
Run Code Online (Sandbox Code Playgroud)

但是现在我想使用相同类型的套接字来构建不使用SSL的连接.

首先,我不确定如何进行"握手",就像我上面提到的SSL连接一样.
通过查看一些正常的boost.asio示例,我假设我不必为非ssl连接执行此操作,并且只有在接受后才直接读取/写入套接字?

然后作为下一步,我尝试按照上面提到的主题进行了解释,我在会话中添加了一个布尔值ssl来检查它是否是SSL连接.
然后socket_我使用了而不是在异步函数中使用socket_.lowest_layer().

这是建议的代码:

if ( sslEnabled )
    boost::asio::async_write( secureSocket_ );
} else {
    boost::asio::async_write( secureSocket_.lowest_layer() );
}
Run Code Online (Sandbox Code Playgroud)

但是,似乎编译器不接受此解决方案.当我尝试使用具有socket_.lowest_layer()作为流的异步函数编译代码时,此错误显示出来(它仅用于async_read,async_write具有类似的错误):

boost\asio\impl\read.hpp(263): error C2039: 'async_read_some' : is not a member of 'boost::asio::basic_socket<Protocol,SocketService>'
          with
          [
              Protocol=boost::asio::ip::tcp,
              SocketService=boost::asio::stream_socket_service<boost::asio::ip::tcp>
          ]
          boost\asio\impl\read.hpp(255) : while compiling class template member function 'void boost::asio::detail::read_op<AsyncReadStream,MutableBufferSequence,CompletionCondition,ReadHandler>::operator ()(const boost::system::error_code &,size_t,int)'
          with
          [
            AsyncReadStream=boost::asio::basic_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp>>,
              MutableBufferSequence=boost::asio::mutable_buffers_1,
              CompletionCondition=boost::asio::detail::transfer_all_t,
              ReadHandler=boost::_bi::bind_t<void,boost::_mfi::mf2<void,CSocket,const boost::system::error_code &,size_t>,boost::_bi::list3<boost::_bi::value<CSocket *>,boost::arg<1>,boost::arg<2>>>
          ]
          boost\asio\impl\read.hpp(527) : see reference to class template instantiation 'boost::asio::detail::read_op<AsyncReadStream,MutableBufferSequence,CompletionCondition,ReadHandler>' being compiled
          with
          [
              AsyncReadStream=boost::asio::basic_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp>>,
              MutableBufferSequence=boost::asio::mutable_buffers_1,
              CompletionCondition=boost::asio::detail::transfer_all_t,
              ReadHandler=boost::_bi::bind_t<void,boost::_mfi::mf2<void,CSocket,const boost::system::error_code &,size_t>,boost::_bi::list3<boost::_bi::value<CSocket *>,boost::arg<1>,boost::arg<2>>>
          ]
          c:\asio_test\source\server.cpp(131) : see reference to function template instantiation 'void boost::asio::async_read<boost::asio::basic_socket<Protocol,SocketService>,boost::asio::mutable_buffers_1,boost::_bi::bind_t<R,F,L>>(AsyncReadStream &,const MutableBufferSequence &,const ReadHandler &)' being compiled
          with
          [
              Protocol=boost::asio::ip::tcp,
              SocketService=boost::asio::stream_socket_service<boost::asio::ip::tcp>,
              R=void,
              F=boost::_mfi::mf2<void,CSocket,const boost::system::error_code &,size_t>,
              L=boost::_bi::list3<boost::_bi::value<CSocket *>,boost::arg<1>,boost::arg<2>>,
              AsyncReadStream=boost::asio::basic_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp>>,
              MutableBufferSequence=boost::asio::mutable_buffers_1,
              ReadHandler=boost::_bi::bind_t<void,boost::_mfi::mf2<void,CSocket,const boost::system::error_code &,size_t>,boost::_bi::list3<boost::_bi::value<CSocket *>,boost::arg<1>,boost::arg<2>>>
          ]

Build FAILED.
Run Code Online (Sandbox Code Playgroud)

所以现在我几乎陷入困境,我真的希望你能帮助我.搜索错误没有带来什么,因为它应该起作用我不知道错误是什么...

use*_*111 9

其实我觉得我现在解决了.

1)非ssl上的"握手"确实没有必要,我接受后立即做async_reading

2)而不是async_read/write( socket_.lowest_layer(), ... )我必须使用socket_.next_layer().async_read_some( buffer, handler)
async_write( socket_.next_layer(), ... )

我仍然不知道为什么它不适用于socket_.lowest_layer()(如果有人知道,请解释)但至少它适用于上述方法.我希望这也能帮助其他有类似问题的人;-)