From 2e96142ebbfb964bee9097e901582ffc3093f471 Mon Sep 17 00:00:00 2001 From: ruben Date: Fri, 24 Apr 2020 12:35:06 +0100 Subject: [PATCH] Split socket-specific methods to socket_connection --- include/boost/mysql/connection.hpp | 42 ++++--- .../detail/network_algorithms/connect.hpp | 8 +- .../network_algorithms/impl/connect.hpp | 13 ++- include/boost/mysql/impl/connection.hpp | 110 +++++++++--------- test/integration/close_connection.cpp | 4 +- test/integration/connect.cpp | 4 +- test/integration/integration_test_common.hpp | 2 +- test/integration/network_functions.hpp | 2 +- 8 files changed, 100 insertions(+), 85 deletions(-) diff --git a/include/boost/mysql/connection.hpp b/include/boost/mysql/connection.hpp index 8409e2ab..1a08ee38 100644 --- a/include/boost/mysql/connection.hpp +++ b/include/boost/mysql/connection.hpp @@ -65,6 +65,8 @@ class connection Stream next_layer_; channel_type channel_; +protected: + channel_type& get_channel() noexcept { return channel_; } public: /** * \brief Initializing constructor. @@ -113,20 +115,6 @@ public: BOOST_MYSQL_INITFN_RESULT_TYPE(CompletionToken, handshake_signature) async_handshake(const connection_params& params, CompletionToken&& token, error_info* info = nullptr); - template - void connect(const EndpointType& endpoint, const connection_params& params, - error_code& ec, error_info& info); - - template - void connect(const EndpointType& endpoint, const connection_params& params); - - using connect_signature = void(error_code); - - template - BOOST_MYSQL_INITFN_RESULT_TYPE(CompletionToken, connect_signature) - async_connect(const EndpointType& endpoint, const connection_params& params, - CompletionToken&& token, error_info* output_info=nullptr); - /** * \brief Executes a SQL text query (sync with error code version). * \details Does not perform the actual retrieval of the data; use the various @@ -208,6 +196,28 @@ public: template BOOST_MYSQL_INITFN_RESULT_TYPE(CompletionToken, quit_signature) async_quit(CompletionToken&& token, error_info* info=nullptr); +}; + +template< + typename Stream +> +class socket_connection : public connection +{ +public: + using connection::connection; + using endpoint_type = typename Stream::endpoint_type; + + void connect(const endpoint_type& endpoint, const connection_params& params, + error_code& ec, error_info& info); + + void connect(const endpoint_type& endpoint, const connection_params& params); + + using connect_signature = void(error_code); + + template + BOOST_MYSQL_INITFN_RESULT_TYPE(CompletionToken, connect_signature) + async_connect(const endpoint_type& endpoint, const connection_params& params, + CompletionToken&& token, error_info* output_info=nullptr); /** * \brief Closes the connection (sync with error code version). @@ -241,7 +251,7 @@ public: * \ingroup connection * \brief A connection to MySQL over a TCP socket. */ -using tcp_connection = connection; +using tcp_connection = socket_connection; #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) || defined(BOOST_MYSQL_DOXYGEN) @@ -249,7 +259,7 @@ using tcp_connection = connection; * \ingroup connection * \brief A connection to MySQL over a UNIX domain socket. */ -using unix_connection = connection; +using unix_connection = socket_connection; #endif diff --git a/include/boost/mysql/detail/network_algorithms/connect.hpp b/include/boost/mysql/detail/network_algorithms/connect.hpp index 28310c5e..a4f1600e 100644 --- a/include/boost/mysql/detail/network_algorithms/connect.hpp +++ b/include/boost/mysql/detail/network_algorithms/connect.hpp @@ -15,10 +15,10 @@ namespace boost { namespace mysql { namespace detail { -template +template void connect( channel& chan, - const EndpointType& endpoint, + const typename StreamType::endpoint_type& endpoint, const connection_params& params, error_code& err, error_info& info @@ -26,11 +26,11 @@ void connect( using connect_signature = empty_signature; -template +template BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, connect_signature) async_connect( channel& chan, - const EndpointType& endpoint, + const typename StreamType::endpoint_type& endpoint, const connection_params& params, CompletionToken&& token, error_info* info diff --git a/include/boost/mysql/detail/network_algorithms/impl/connect.hpp b/include/boost/mysql/detail/network_algorithms/impl/connect.hpp index 9c428ae2..8a57a44d 100644 --- a/include/boost/mysql/detail/network_algorithms/impl/connect.hpp +++ b/include/boost/mysql/detail/network_algorithms/impl/connect.hpp @@ -10,10 +10,10 @@ #include "boost/mysql/detail/network_algorithms/handshake.hpp" -template +template void boost::mysql::detail::connect( channel& chan, - const EndpointType& endpoint, + const typename StreamType::endpoint_type& endpoint, const connection_params& params, error_code& err, error_info& info @@ -33,29 +33,30 @@ void boost::mysql::detail::connect( } } -template +template BOOST_ASIO_INITFN_RESULT_TYPE( CompletionToken, boost::mysql::detail::connect_signature ) boost::mysql::detail::async_connect( channel& chan, - const EndpointType& endpoint, + const typename StreamType::endpoint_type& endpoint, const connection_params& params, CompletionToken&& token, error_info* info ) { + using endpoint_type = typename StreamType::endpoint_type; struct op : async_op { - const EndpointType& ep_; + const endpoint_type& ep_; // No need for a copy, as we will call it in the first operator() call connection_params params_; op( boost::asio::async_completion& completion, channel& chan, error_info* output_info, - const EndpointType& ep, + const endpoint_type& ep, const connection_params& params ) : async_op(completion, chan, output_info), diff --git a/include/boost/mysql/impl/connection.hpp b/include/boost/mysql/impl/connection.hpp index babb0dd6..974bd81b 100644 --- a/include/boost/mysql/impl/connection.hpp +++ b/include/boost/mysql/impl/connection.hpp @@ -61,50 +61,6 @@ boost::mysql::connection::async_handshake( ); } -// connect -template -template -void boost::mysql::connection::connect( - const EndpointType& endpoint, - const connection_params& params, - error_code& ec, - error_info& info -) -{ - detail::clear_errors(ec, info); - detail::connect(channel_, endpoint, params, ec, info); -} - -template -template -void boost::mysql::connection::connect( - const EndpointType& endpoint, - const connection_params& params -) -{ - detail::error_block blk; - detail::connect(channel_, endpoint, params, blk.err, blk.info); - blk.check(); -} - -template -template -BOOST_MYSQL_INITFN_RESULT_TYPE( - CompletionToken, - typename boost::mysql::connection::connect_signature -) -boost::mysql::connection::async_connect( - const EndpointType& endpoint, - const connection_params& params, - CompletionToken&& token, - error_info* output_info -) -{ - detail::conditional_clear(output_info); - detail::check_completion_token(); - return detail::async_connect(channel_, endpoint, params, std::forward(token), output_info); -} - // Query template boost::mysql::resultset boost::mysql::connection::query( @@ -238,21 +194,27 @@ boost::mysql::connection::async_quit( ); } +// socket_connection: connect template -void boost::mysql::connection::close( - error_code& err, +void boost::mysql::socket_connection::connect( + const endpoint_type& endpoint, + const connection_params& params, + error_code& ec, error_info& info ) { - detail::clear_errors(err, info); - detail::close_connection(channel_, err, info); + detail::clear_errors(ec, info); + detail::connect(this->get_channel(), endpoint, params, ec, info); } template -void boost::mysql::connection::close() +void boost::mysql::socket_connection::connect( + const endpoint_type& endpoint, + const connection_params& params +) { detail::error_block blk; - detail::close_connection(channel_, blk.err, blk.info); + detail::connect(this->get_channel(), endpoint, params, blk.err, blk.info); blk.check(); } @@ -260,9 +222,51 @@ template template BOOST_MYSQL_INITFN_RESULT_TYPE( CompletionToken, - typename boost::mysql::connection::close_signature + typename boost::mysql::socket_connection::connect_signature ) -boost::mysql::connection::async_close( +boost::mysql::socket_connection::async_connect( + const endpoint_type& endpoint, + const connection_params& params, + CompletionToken&& token, + error_info* output_info +) +{ + detail::conditional_clear(output_info); + detail::check_completion_token(); + return detail::async_connect( + this->get_channel(), + endpoint, + params, + std::forward(token), + output_info + ); +} + +template +void boost::mysql::socket_connection::close( + error_code& err, + error_info& info +) +{ + detail::clear_errors(err, info); + detail::close_connection(this->get_channel(), err, info); +} + +template +void boost::mysql::socket_connection::close() +{ + detail::error_block blk; + detail::close_connection(this->get_channel(), blk.err, blk.info); + blk.check(); +} + +template +template +BOOST_MYSQL_INITFN_RESULT_TYPE( + CompletionToken, + typename boost::mysql::socket_connection::close_signature +) +boost::mysql::socket_connection::async_close( CompletionToken&& token, error_info* info ) @@ -270,7 +274,7 @@ boost::mysql::connection::async_close( detail::conditional_clear(info); detail::check_completion_token(); return detail::async_close_connection( - channel_, + this->get_channel(), std::forward(token), info ); diff --git a/test/integration/close_connection.cpp b/test/integration/close_connection.cpp index d74ff9ed..daa39223 100644 --- a/test/integration/close_connection.cpp +++ b/test/integration/close_connection.cpp @@ -9,7 +9,7 @@ using namespace boost::mysql::test; using boost::mysql::error_code; -using boost::mysql::connection; +using boost::mysql::socket_connection; namespace { @@ -57,7 +57,7 @@ struct CloseConnectionTest : public NetworkTest network_functions* net = this->GetParam().net; // An unconnected connection - connection not_opened_conn (this->ctx); + socket_connection not_opened_conn (this->ctx); // Close auto result = net->close(not_opened_conn); diff --git a/test/integration/connect.cpp b/test/integration/connect.cpp index 8cd1a555..2f1136a3 100644 --- a/test/integration/connect.cpp +++ b/test/integration/connect.cpp @@ -8,7 +8,7 @@ #include "integration_test_common.hpp" using namespace boost::mysql::test; -using boost::mysql::connection; +using boost::mysql::socket_connection; using boost::mysql::errc; using boost::mysql::error_code; @@ -19,7 +19,7 @@ template struct ConnectTest : NetworkTest { network_functions* net; - connection other_conn; + socket_connection other_conn; ConnectTest(): net(this->GetParam().net), diff --git a/test/integration/integration_test_common.hpp b/test/integration/integration_test_common.hpp index 83607697..1d5f704f 100644 --- a/test/integration/integration_test_common.hpp +++ b/test/integration/integration_test_common.hpp @@ -45,7 +45,7 @@ struct IntegTest : testing::Test "boost_mysql_integtests" }; boost::asio::io_context ctx; - mysql::connection conn {ctx}; + mysql::socket_connection conn {ctx}; boost::asio::executor_work_guard guard { ctx.get_executor() }; std::thread runner {[this]{ ctx.run(); } }; diff --git a/test/integration/network_functions.hpp b/test/integration/network_functions.hpp index 58d5db8e..46dcf000 100644 --- a/test/integration/network_functions.hpp +++ b/test/integration/network_functions.hpp @@ -112,7 +112,7 @@ template class network_functions { public: - using connection_type = connection; + using connection_type = socket_connection; using prepared_statement_type = prepared_statement; using resultset_type = resultset;