quit_connection.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_QUIT_CONNECTION_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_QUIT_CONNECTION_HPP
  9. #include <boost/mysql/diagnostics.hpp>
  10. #include <boost/mysql/error_code.hpp>
  11. #include <boost/mysql/detail/algo_params.hpp>
  12. #include <boost/mysql/detail/next_action.hpp>
  13. #include <boost/mysql/impl/internal/coroutine.hpp>
  14. #include <boost/mysql/impl/internal/protocol/serialization.hpp>
  15. #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
  16. namespace boost {
  17. namespace mysql {
  18. namespace detail {
  19. class quit_connection_algo
  20. {
  21. int resume_point_{0};
  22. std::uint8_t sequence_number_{0};
  23. bool should_perform_shutdown_{};
  24. public:
  25. quit_connection_algo(quit_connection_algo_params) noexcept {}
  26. next_action resume(connection_state_data& st, diagnostics&, error_code ec)
  27. {
  28. switch (resume_point_)
  29. {
  30. case 0:
  31. // This can only be top-level in connection, and never in any_connection.
  32. // State checks are not worthy here - already handled by close.
  33. // Mark the session as finished
  34. should_perform_shutdown_ = st.tls_active;
  35. st.status = connection_status::not_connected;
  36. st.tls_active = false;
  37. // Send quit message
  38. BOOST_MYSQL_YIELD(resume_point_, 1, st.write(quit_command(), sequence_number_))
  39. if (ec)
  40. return ec;
  41. // If there was no error and TLS is active, attempt TLS shutdown.
  42. // MySQL usually just closes the socket, instead of
  43. // sending the close_notify message required by the shutdown, so we ignore this error.
  44. if (should_perform_shutdown_)
  45. {
  46. BOOST_MYSQL_YIELD(resume_point_, 2, next_action::ssl_shutdown())
  47. }
  48. }
  49. return next_action();
  50. }
  51. };
  52. } // namespace detail
  53. } // namespace mysql
  54. } // namespace boost
  55. #endif /* INCLUDE_BOOST_MYSQL_DETAIL_NETWORK_ALGORITHMS_QUIT_CONNECTION_HPP_ */