close_connection.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_CLOSE_CONNECTION_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CLOSE_CONNECTION_HPP
  9. #include <boost/mysql/detail/algo_params.hpp>
  10. #include <boost/mysql/detail/next_action.hpp>
  11. #include <boost/mysql/impl/internal/coroutine.hpp>
  12. #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
  13. #include <boost/mysql/impl/internal/sansio/quit_connection.hpp>
  14. namespace boost {
  15. namespace mysql {
  16. namespace detail {
  17. class close_connection_algo
  18. {
  19. int resume_point_{0};
  20. quit_connection_algo quit_;
  21. error_code stored_ec_;
  22. public:
  23. close_connection_algo(close_connection_algo_params params) noexcept : quit_({params.diag}) {}
  24. next_action resume(connection_state_data& st, error_code ec)
  25. {
  26. next_action act;
  27. switch (resume_point_)
  28. {
  29. case 0:
  30. // Clear diagnostics
  31. quit_.diag().clear();
  32. // If we're not connected, we're done
  33. if (!st.is_connected)
  34. return next_action();
  35. // Attempt quit
  36. while (!(act = quit_.resume(st, ec)).is_done())
  37. BOOST_MYSQL_YIELD(resume_point_, 1, act)
  38. stored_ec_ = act.error();
  39. // Close the transport
  40. BOOST_MYSQL_YIELD(resume_point_, 2, next_action::close())
  41. // If quit resulted in an error, keep that error.
  42. // Otherwise, return any error derived from close
  43. return stored_ec_ ? stored_ec_ : ec;
  44. }
  45. return next_action();
  46. }
  47. };
  48. } // namespace detail
  49. } // namespace mysql
  50. } // namespace boost
  51. #endif