I\'m in the process of converting a library to Boost.Asio (which has worked very well so far), but I\'ve hit something of a stumbling block with regards to a design decision
There's a couple of ways you can do this. In the past, I've done something like
if ( sslEnabled )
boost::asio::async_write( secureSocket_ );
} else {
boost::asio::async_write( secureSocket_.lowest_layer() );
}
Which can get messy pretty quickly with a lot of if/else
statements. You could also create an abstract class (pseudo code - oversimplified)
class Socket
{
public:
virtual void connect( ... );
virtual void accept( ... );
virtual void async_write( ... );
virtual void async_read( ... );
private:
boost::asio::ip::tcp::socket socket_;
};
Then create a derived class SecureSocket
to operate on a secureSocket_
instead of socket_
. I don't think it would be duplicating a lot of code, and it's probably cleaner than if/else
whenever you need to async_read
or async_write
.