close_connection.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_CLOSE_CONNECTION_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CLOSE_CONNECTION_HPP
  9. #include <boost/mysql/diagnostics.hpp>
  10. #include <boost/mysql/detail/algo_params.hpp>
  11. #include <boost/mysql/detail/next_action.hpp>
  12. #include <boost/mysql/impl/internal/coroutine.hpp>
  13. #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
  14. #include <boost/mysql/impl/internal/sansio/quit_connection.hpp>
  15. namespace boost {
  16. namespace mysql {
  17. namespace detail {
  18. class close_connection_algo
  19. {
  20. int resume_point_{0};
  21. quit_connection_algo quit_{{}};
  22. error_code stored_ec_;
  23. public:
  24. close_connection_algo(close_connection_algo_params) noexcept {}
  25. next_action resume(connection_state_data& st, diagnostics& diag, error_code ec)
  26. {
  27. next_action act;
  28. switch (resume_point_)
  29. {
  30. case 0:
  31. // If we're not connected, we're done.
  32. // If we're in a multi-function operation, it's safe to proceed.
  33. if (st.status == connection_status::not_connected)
  34. return next_action();
  35. // Attempt quit
  36. while (!(act = quit_.resume(st, diag, 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. if (stored_ec_)
  44. ec = stored_ec_;
  45. }
  46. return ec;
  47. }
  48. };
  49. } // namespace detail
  50. } // namespace mysql
  51. } // namespace boost
  52. #endif