connect.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_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. const void* server_address_;
  23. handshake_algo handshake_;
  24. error_code stored_ec_;
  25. public:
  26. connect_algo(connect_algo_params params) noexcept
  27. : server_address_(params.server_address), handshake_({params.hparams, params.secure_channel})
  28. {
  29. }
  30. next_action resume(connection_state_data& st, diagnostics& diag, error_code ec)
  31. {
  32. next_action act;
  33. switch (resume_point_)
  34. {
  35. case 0:
  36. // Handshake and connect wipe out state, so no state checks are performed.
  37. // Physical connect
  38. BOOST_MYSQL_YIELD(resume_point_, 1, next_action::connect(server_address_))
  39. if (ec)
  40. return ec;
  41. // Handshake
  42. while (!(act = handshake_.resume(st, diag, 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