connect.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_CONNECT_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CONNECT_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/sansio/connection_state_data.hpp>
  15. #include <boost/mysql/impl/internal/sansio/handshake.hpp>
  16. namespace boost {
  17. namespace mysql {
  18. namespace detail {
  19. class connect_algo
  20. {
  21. int resume_point_{0};
  22. handshake_algo handshake_;
  23. error_code stored_ec_;
  24. public:
  25. connect_algo(connect_algo_params params) noexcept
  26. : handshake_({params.diag, params.hparams, params.secure_channel})
  27. {
  28. }
  29. next_action resume(connection_state_data& st, error_code ec)
  30. {
  31. next_action act;
  32. switch (resume_point_)
  33. {
  34. case 0:
  35. // Clear diagnostics
  36. handshake_.diag().clear();
  37. // Physical connect
  38. BOOST_MYSQL_YIELD(resume_point_, 1, next_action::connect())
  39. if (ec)
  40. return ec;
  41. // Handshake
  42. while (!(act = handshake_.resume(st, ec)).is_done())
  43. BOOST_MYSQL_YIELD(resume_point_, 2, act)
  44. // If handshake failed, close the stream ignoring the result
  45. // and return handshake's error code
  46. if (act.error())
  47. {
  48. stored_ec_ = act.error();
  49. BOOST_MYSQL_YIELD(resume_point_, 3, next_action::close())
  50. return stored_ec_;
  51. }
  52. // Done
  53. }
  54. return next_action();
  55. }
  56. };
  57. } // namespace detail
  58. } // namespace mysql
  59. } // namespace boost
  60. #endif