Skip to content

Commit

Permalink
refactored ssl state
Browse files Browse the repository at this point in the history
  • Loading branch information
anarthal committed Oct 24, 2023
1 parent 87a3244 commit 845ad78
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 443 deletions.
2 changes: 1 addition & 1 deletion include/boost/mysql/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class connection
*
* \returns Whether the connection is using SSL.
*/
bool uses_ssl() const noexcept { return impl_.stream().ssl_active(); }
bool uses_ssl() const noexcept { return impl_.ssl_active(); }

/**
* \brief Returns the current metadata mode that this connection is using.
Expand Down
49 changes: 10 additions & 39 deletions include/boost/mysql/detail/any_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,17 @@ namespace boost {
namespace mysql {
namespace detail {

enum class ssl_state
{
unsupported,
inactive,
active,
torn_down,
};

class any_stream
{
ssl_state ssl_state_;
bool supports_ssl_;

public:
any_stream(bool supports_ssl) noexcept
: ssl_state_(supports_ssl ? ssl_state::inactive : ssl_state::unsupported)
{
}
ssl_state get_ssl_state() const noexcept { return ssl_state_; }
bool supports_ssl() const noexcept { return ssl_state_ != ssl_state::unsupported; }
bool ssl_active() const noexcept { return ssl_state_ == ssl_state::active; }
void set_ssl_active() noexcept { ssl_state_ = ssl_state::active; }
void set_ssl_torn_down() noexcept { ssl_state_ = ssl_state::torn_down; }
void reset_ssl_active() noexcept
{
if (ssl_state_ != ssl_state::unsupported)
ssl_state_ = ssl_state::inactive;
}

using executor_type = asio::any_io_executor;

any_stream(bool supports_ssl) noexcept : supports_ssl_(supports_ssl) {}

bool supports_ssl() const noexcept { return supports_ssl_; }

virtual ~any_stream() {}
virtual executor_type get_executor() = 0;

Expand All @@ -61,29 +42,19 @@ class any_stream
virtual void async_shutdown(asio::any_completion_handler<void(error_code)>) = 0;

// Reading
virtual std::size_t read_some(asio::mutable_buffer, error_code& ec) = 0;
virtual void async_read_some(asio::mutable_buffer, asio::any_completion_handler<void(error_code, std::size_t)>) = 0;
virtual std::size_t read_some(asio::mutable_buffer, bool use_ssl, error_code& ec) = 0;
virtual void async_read_some(asio::mutable_buffer, bool use_ssl, asio::any_completion_handler<void(error_code, std::size_t)>) = 0;

// Writing
virtual std::size_t write_some(asio::const_buffer, error_code& ec) = 0;
virtual void async_write_some(asio::const_buffer, asio::any_completion_handler<void(error_code, std::size_t)>) = 0;
virtual std::size_t write_some(asio::const_buffer, bool use_ssl, error_code& ec) = 0;
virtual void async_write_some(asio::const_buffer, bool use_ssl, asio::any_completion_handler<void(error_code, std::size_t)>) = 0;

// Connect and close. TODO: we should probably split this class
// Connect and close
virtual void connect(const void* endpoint, error_code& ec) = 0;
virtual void async_connect(const void* endpoint, asio::any_completion_handler<void(error_code)>) = 0;
virtual void close(error_code& ec) = 0;
};

class any_stream_v2 : public any_stream
{
public:
using any_stream::any_stream;
void connect(const void*, error_code&) final override {}
void async_connect(const void*, asio::any_completion_handler<void(error_code)>) final override {}
virtual void connect_v2(string_view address, string_view port, error_code& ec) = 0;
virtual void async_connect_v2(string_view address, string_view port, asio::any_completion_handler<void(error_code)>) = 0;
};

} // namespace detail
} // namespace mysql
} // namespace boost
Expand Down
29 changes: 21 additions & 8 deletions include/boost/mysql/detail/any_stream_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/config.hpp>
#include <boost/core/ignore_unused.hpp>

#include <memory>
#include <type_traits>
Expand Down Expand Up @@ -130,28 +131,38 @@ class any_stream_impl final : public any_stream
}

// Reading
std::size_t read_some(boost::asio::mutable_buffer buff, error_code& ec) final override
std::size_t read_some(boost::asio::mutable_buffer buff, bool use_ssl, error_code& ec) final override
{
BOOST_ASSERT(!use_ssl);
boost::ignore_unused(use_ssl);
return stream_.read_some(buff, ec);
}
void async_read_some(
boost::asio::mutable_buffer buff,
bool use_ssl,
asio::any_completion_handler<void(error_code, std::size_t)> handler
) final override
{
BOOST_ASSERT(!use_ssl);
boost::ignore_unused(use_ssl);
return stream_.async_read_some(buff, std::move(handler));
}

// Writing
std::size_t write_some(boost::asio::const_buffer buff, error_code& ec) final override
std::size_t write_some(boost::asio::const_buffer buff, bool use_ssl, error_code& ec) final override
{
BOOST_ASSERT(!use_ssl);
boost::ignore_unused(use_ssl);
return stream_.write_some(buff, ec);
}
void async_write_some(
boost::asio::const_buffer buff,
bool use_ssl,
asio::any_completion_handler<void(error_code, std::size_t)> handler
) final override
{
BOOST_ASSERT(!use_ssl);
boost::ignore_unused(use_ssl);
return stream_.async_write_some(buff, std::move(handler));
}

Expand Down Expand Up @@ -197,9 +208,9 @@ class any_stream_impl<asio::ssl::stream<Stream>> final : public any_stream
}

// Reading
std::size_t read_some(boost::asio::mutable_buffer buff, error_code& ec) override final
std::size_t read_some(boost::asio::mutable_buffer buff, bool use_ssl, error_code& ec) override final
{
if (ssl_active())
if (use_ssl)
{
return stream_.read_some(buff, ec);
}
Expand All @@ -210,10 +221,11 @@ class any_stream_impl<asio::ssl::stream<Stream>> final : public any_stream
}
void async_read_some(
boost::asio::mutable_buffer buff,
bool use_ssl,
asio::any_completion_handler<void(error_code, std::size_t)> handler
) override final
{
if (ssl_active())
if (use_ssl)
{
return stream_.async_read_some(buff, std::move(handler));
}
Expand All @@ -224,9 +236,9 @@ class any_stream_impl<asio::ssl::stream<Stream>> final : public any_stream
}

// Writing
std::size_t write_some(boost::asio::const_buffer buff, error_code& ec) override final
std::size_t write_some(boost::asio::const_buffer buff, bool use_ssl, error_code& ec) override final
{
if (ssl_active())
if (use_ssl)
{
return stream_.write_some(buff, ec);
}
Expand All @@ -237,10 +249,11 @@ class any_stream_impl<asio::ssl::stream<Stream>> final : public any_stream
}
void async_write_some(
boost::asio::const_buffer buff,
bool use_ssl,
asio::any_completion_handler<void(error_code, std::size_t)> handler
) override final
{
if (ssl_active())
if (use_ssl)
{
stream_.async_write_some(buff, std::move(handler));
}
Expand Down
Loading

0 comments on commit 845ad78

Please sign in to comment.