gda2004
gda2004

Reputation: 746

pass secure socket as normal socket in boost

I have these typedefs the problem is I need pass a secure socket as TSocket will a direct cast from TSecureSocket to TSocket work ? or is there another solution? depending on the port I will make the socket secure and in others I will not. I just need the return type to be 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;

I have looked at this boost::asio convert socket to secure

Upvotes: 1

Views: 366

Answers (1)

Caribou
Caribou

Reputation: 2081

I have these typedefs

  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;

the problem is I need pass a secure socket as TSocket. Will a direct cast from TSecureSocket to TSocket work ?

Short Answer No. Because boost::asio::ssl::stream<TBoostSocket> wraps the socket.

or is there another solution? depending on the port I will make the socket secure and in others I will not I just need the return type to be TSocket.

However TSLLSocket (or boost::asio::ssl::stream rather) provides a method for retrieving the socket from an instance:

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

Where next_layer_type is defined by the following 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

Since you define your template with TBoostSocket that is what you should get when you call next_layer() or lowest_layer()

Of course this returns a reference not a pointer, and that reference is to an instance not owned by you. So you will now need to somehow wrap this in a shared_ptr, and that may not be that easy since you can't allow it to be deleted.

Upvotes: 1

Related Questions