quit_connection.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // Copyright (c) 2019-2024 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. diagnostics* diag_;
  23. std::uint8_t sequence_number_{0};
  24. public:
  25. quit_connection_algo(quit_connection_algo_params params) noexcept : diag_(params.diag) {}
  26. diagnostics& diag() { return *diag_; }
  27. next_action resume(connection_state_data& st, error_code ec)
  28. {
  29. switch (resume_point_)
  30. {
  31. case 0:
  32. // Clear diagnostics
  33. diag_->clear();
  34. // Send quit message
  35. BOOST_MYSQL_YIELD(resume_point_, 1, st.write(quit_command(), sequence_number_))
  36. // Mark the session as finished, regardless of the write result
  37. st.is_connected = false;
  38. // If write resulted in an error, return
  39. if (ec)
  40. return ec;
  41. // Shutdown SSL. MySQL doesn't always shut down SSL correctly, so we ignore this error.
  42. if (st.ssl == ssl_state::active)
  43. {
  44. BOOST_MYSQL_YIELD(resume_point_, 2, next_action::ssl_shutdown())
  45. st.ssl = ssl_state::torn_down;
  46. }
  47. // Done
  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_ */