Boost Asio how to read/write on a SSL socket that doesnt use SSL?

余生颓废 提交于 2019-12-05 00:16:40

问题


The title is my question.

I already found a topic related to this here -> Using SSL sockets and non-SSL sockets simultaneously in Boost.Asio?
and basically I'm in the same situation but for some reason I couldn't comment there and/or contact the questioner directly so I'm doing this as a new question.

I have a set up ssl socket ssl::stream<ip::tcp::socket> socket_; where clients can connect just fine with

socket_.async_handshake(ssl::stream_base::server, session::handle_handshake)

and then read/write with

async_write(socket_, buffer(send_data, send_length), session::handle_read)
socket_.async_read_some(buffer(recieved_data, max_length), session::handle_read)

However now I want to use the same type of socket to build up connections that dont use SSL.

First of all I'm unsure how to do the "handshaking" as it is done with a SSL connection as I mentioned above.
By looking at some normal boost.asio example I assume that I dont have to do that for non-ssl connections and just directly read/write to the socket once it has been accepted?

Then as a next step I tried to do it as decsribed in the topic I mentioned above, I added a boolean ssl to session to check whether its a SSL connection or not.
Then instead of using socket_ in the async functions, I used socket_.lowest_layer().

Here's the suggested code:

if ( sslEnabled )
    boost::asio::async_write( secureSocket_ );
} else {
    boost::asio::async_write( secureSocket_.lowest_layer() );
}

However it seems that the compiler doesnt accept this solution. When I try to compile the code with an async function that has socket_.lowest_layer() as a stream this error shows up (its only for async_read, async_write has a similar one):

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.

So now I'm pretty much stuck and I really hope you can help me. Searching for the error brought up nothing and since it should supposedly work I dont know what the mistake could be...


回答1:


Actually I think I solved it now.

1) The "handshaking" on a non-ssl is indeed not necessary, I immediately do async_reading after the accept

2) Instead of async_read/write( socket_.lowest_layer(), ... ) I had to use socket_.next_layer().async_read_some( buffer, handler) and
async_write( socket_.next_layer(), ... )

I still dont know why it doesnt work with socket_.lowest_layer() (if someone knows, please explain) but at least it works just fine with the above methods. And I hope this will help other people with a similar problem too ;-)



来源:https://stackoverflow.com/questions/9943541/boost-asio-how-to-read-write-on-a-ssl-socket-that-doesnt-use-ssl