next_action.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_DETAIL_NEXT_ACTION_HPP
  8. #define BOOST_MYSQL_DETAIL_NEXT_ACTION_HPP
  9. #include <boost/mysql/error_code.hpp>
  10. #include <boost/assert.hpp>
  11. #include <boost/core/span.hpp>
  12. #include <cstdint>
  13. namespace boost {
  14. namespace mysql {
  15. namespace detail {
  16. enum class next_action_type
  17. {
  18. none,
  19. write,
  20. read,
  21. ssl_handshake,
  22. ssl_shutdown,
  23. connect,
  24. close,
  25. };
  26. class next_action
  27. {
  28. public:
  29. struct read_args_t
  30. {
  31. span<std::uint8_t> buffer;
  32. bool use_ssl;
  33. };
  34. struct write_args_t
  35. {
  36. span<const std::uint8_t> buffer;
  37. bool use_ssl;
  38. };
  39. next_action(error_code ec = {}) noexcept : type_(next_action_type::none), data_(ec) {}
  40. // Type
  41. next_action_type type() const noexcept { return type_; }
  42. bool is_done() const noexcept { return type_ == next_action_type::none; }
  43. bool success() const noexcept { return is_done() && !data_.ec; }
  44. // Arguments
  45. error_code error() const noexcept
  46. {
  47. BOOST_ASSERT(is_done());
  48. return data_.ec;
  49. }
  50. read_args_t read_args() const noexcept
  51. {
  52. BOOST_ASSERT(type_ == next_action_type::read);
  53. return data_.read_args;
  54. }
  55. write_args_t write_args() const noexcept
  56. {
  57. BOOST_ASSERT(type_ == next_action_type::write);
  58. return data_.write_args;
  59. }
  60. static next_action connect() noexcept { return next_action(next_action_type::connect, data_t()); }
  61. static next_action read(read_args_t args) noexcept { return next_action(next_action_type::read, args); }
  62. static next_action write(write_args_t args) noexcept
  63. {
  64. return next_action(next_action_type::write, args);
  65. }
  66. static next_action ssl_handshake() noexcept
  67. {
  68. return next_action(next_action_type::ssl_handshake, data_t());
  69. }
  70. static next_action ssl_shutdown() noexcept
  71. {
  72. return next_action(next_action_type::ssl_shutdown, data_t());
  73. }
  74. static next_action close() noexcept { return next_action(next_action_type::close, data_t()); }
  75. private:
  76. next_action_type type_{next_action_type::none};
  77. union data_t
  78. {
  79. error_code ec;
  80. read_args_t read_args;
  81. write_args_t write_args;
  82. data_t() noexcept : ec(error_code()) {}
  83. data_t(error_code ec) noexcept : ec(ec) {}
  84. data_t(read_args_t args) noexcept : read_args(args) {}
  85. data_t(write_args_t args) noexcept : write_args(args) {}
  86. } data_;
  87. next_action(next_action_type t, data_t data) noexcept : type_(t), data_(data) {}
  88. };
  89. } // namespace detail
  90. } // namespace mysql
  91. } // namespace boost
  92. #endif