connection_state.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // Copyright (c) 2019-2025 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CONNECTION_STATE_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CONNECTION_STATE_HPP
  9. #include <boost/mysql/client_errc.hpp>
  10. #include <boost/mysql/diagnostics.hpp>
  11. #include <boost/mysql/error_code.hpp>
  12. #include <boost/mysql/handshake_params.hpp>
  13. #include <boost/mysql/statement.hpp>
  14. #include <boost/mysql/detail/algo_params.hpp>
  15. #include <boost/mysql/detail/any_resumable_ref.hpp>
  16. #include <boost/mysql/detail/next_action.hpp>
  17. #include <boost/mysql/impl/internal/sansio/close_connection.hpp>
  18. #include <boost/mysql/impl/internal/sansio/close_statement.hpp>
  19. #include <boost/mysql/impl/internal/sansio/connect.hpp>
  20. #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
  21. #include <boost/mysql/impl/internal/sansio/execute.hpp>
  22. #include <boost/mysql/impl/internal/sansio/handshake.hpp>
  23. #include <boost/mysql/impl/internal/sansio/ping.hpp>
  24. #include <boost/mysql/impl/internal/sansio/prepare_statement.hpp>
  25. #include <boost/mysql/impl/internal/sansio/quit_connection.hpp>
  26. #include <boost/mysql/impl/internal/sansio/read_resultset_head.hpp>
  27. #include <boost/mysql/impl/internal/sansio/read_some_rows.hpp>
  28. #include <boost/mysql/impl/internal/sansio/read_some_rows_dynamic.hpp>
  29. #include <boost/mysql/impl/internal/sansio/reset_connection.hpp>
  30. #include <boost/mysql/impl/internal/sansio/run_pipeline.hpp>
  31. #include <boost/mysql/impl/internal/sansio/set_character_set.hpp>
  32. #include <boost/mysql/impl/internal/sansio/start_execution.hpp>
  33. #include <boost/mysql/impl/internal/sansio/top_level_algo.hpp>
  34. #include <boost/variant2/variant.hpp>
  35. #include <cstddef>
  36. namespace boost {
  37. namespace mysql {
  38. namespace detail {
  39. // clang-format off
  40. template <class AlgoParams> struct get_algo;
  41. template <> struct get_algo<connect_algo_params> { using type = connect_algo; };
  42. template <> struct get_algo<handshake_algo_params> { using type = handshake_algo; };
  43. template <> struct get_algo<execute_algo_params> { using type = execute_algo; };
  44. template <> struct get_algo<start_execution_algo_params> { using type = start_execution_algo; };
  45. template <> struct get_algo<read_resultset_head_algo_params> { using type = read_resultset_head_algo; };
  46. template <> struct get_algo<read_some_rows_algo_params> { using type = read_some_rows_algo; };
  47. template <> struct get_algo<read_some_rows_dynamic_algo_params> { using type = read_some_rows_dynamic_algo; };
  48. template <> struct get_algo<prepare_statement_algo_params> { using type = prepare_statement_algo; };
  49. template <> struct get_algo<set_character_set_algo_params> { using type = set_character_set_algo; };
  50. template <> struct get_algo<quit_connection_algo_params> { using type = quit_connection_algo; };
  51. template <> struct get_algo<close_connection_algo_params> { using type = close_connection_algo; };
  52. template <> struct get_algo<run_pipeline_algo_params> { using type = run_pipeline_algo; };
  53. template <class AlgoParams> using get_algo_t = typename get_algo<AlgoParams>::type;
  54. // clang-format on
  55. class connection_state
  56. {
  57. // Helper
  58. template <class... Algos>
  59. using make_any_algo_type = variant2::variant<top_level_algo<Algos>...>;
  60. using any_algo = make_any_algo_type<
  61. connect_algo,
  62. handshake_algo,
  63. execute_algo,
  64. start_execution_algo,
  65. read_resultset_head_algo,
  66. read_some_rows_algo,
  67. read_some_rows_dynamic_algo,
  68. prepare_statement_algo,
  69. set_character_set_algo,
  70. quit_connection_algo,
  71. close_connection_algo,
  72. run_pipeline_algo>;
  73. connection_state_data st_data_;
  74. any_algo algo_;
  75. // A function compatible with any_resumable_ref that always fails immediately
  76. // with operation_in_progress. We can't change algo_ while an algorithm is running,
  77. // so we need an any_resumable_ref that doesn't point into algo_
  78. static next_action fail_op_in_progress(void*, error_code, std::size_t)
  79. {
  80. return error_code(client_errc::operation_in_progress);
  81. }
  82. public:
  83. // We initialize the algo state with a dummy value. This will be overwritten
  84. // by setup() before the first algorithm starts running. Doing this avoids
  85. // the need for a special null algo
  86. connection_state(std::size_t read_buffer_size, std::size_t max_buffer_size, bool transport_supports_ssl)
  87. : st_data_(read_buffer_size, max_buffer_size, transport_supports_ssl),
  88. algo_(top_level_algo<quit_connection_algo>(
  89. st_data_,
  90. st_data_.shared_diag,
  91. quit_connection_algo_params{}
  92. ))
  93. {
  94. }
  95. const connection_state_data& data() const { return st_data_; }
  96. connection_state_data& data() { return st_data_; }
  97. template <class AlgoParams>
  98. any_resumable_ref setup(diagnostics& diag, AlgoParams params)
  99. {
  100. // Clear diagnostics
  101. diag.clear();
  102. // If there is an operation in progress, don't change anything, just fail
  103. if (st_data_.op_in_progress)
  104. return any_resumable_ref(nullptr, &fail_op_in_progress);
  105. // Emplace the algorithm
  106. using algo_type = top_level_algo<get_algo_t<AlgoParams>>;
  107. return any_resumable_ref(algo_.emplace<algo_type>(st_data_, diag, params));
  108. }
  109. any_resumable_ref setup(diagnostics& diag, close_statement_algo_params params)
  110. {
  111. return setup(diag, setup_close_statement_pipeline(st_data_, params));
  112. }
  113. any_resumable_ref setup(diagnostics& diag, reset_connection_algo_params)
  114. {
  115. return setup(diag, setup_reset_connection_pipeline(st_data_));
  116. }
  117. any_resumable_ref setup(diagnostics& diag, ping_algo_params)
  118. {
  119. return setup(diag, setup_ping_pipeline(st_data_));
  120. }
  121. template <typename AlgoParams>
  122. typename AlgoParams::result_type result() const
  123. {
  124. return variant2::get<top_level_algo<get_algo_t<AlgoParams>>>(algo_).inner_algo().result(st_data_);
  125. }
  126. };
  127. } // namespace detail
  128. } // namespace mysql
  129. } // namespace boost
  130. #endif