exec_fsm.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // Copyright (c) 2025 Marcelo Zimbres Silva (mzimbres@gmail.com),
  3. // Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifndef BOOST_REDIS_EXEC_FSM_HPP
  9. #define BOOST_REDIS_EXEC_FSM_HPP
  10. #include <boost/redis/detail/multiplexer.hpp>
  11. #include <boost/asio/cancellation_type.hpp>
  12. #include <boost/system/error_code.hpp>
  13. #include <cstddef>
  14. #include <memory>
  15. // Sans-io algorithm for async_exec, as a finite state machine
  16. namespace boost::redis::detail {
  17. // What should we do next?
  18. enum class exec_action_type
  19. {
  20. setup_cancellation, // Set up the cancellation types supported by the composed operation
  21. immediate, // Invoke asio::async_immediate to avoid re-entrancy problems
  22. done, // Call the final handler
  23. notify_writer, // Notify the writer task
  24. wait_for_response, // Wait to be notified
  25. };
  26. class exec_action {
  27. exec_action_type type_;
  28. system::error_code ec_;
  29. std::size_t bytes_read_;
  30. public:
  31. exec_action(exec_action_type type) noexcept
  32. : type_{type}
  33. { }
  34. exec_action(system::error_code ec, std::size_t bytes_read = 0u) noexcept
  35. : type_{exec_action_type::done}
  36. , ec_{ec}
  37. , bytes_read_{bytes_read}
  38. { }
  39. exec_action_type type() const { return type_; }
  40. system::error_code error() const { return ec_; }
  41. std::size_t bytes_read() const { return bytes_read_; }
  42. };
  43. class exec_fsm {
  44. int resume_point_{0};
  45. multiplexer* mpx_{nullptr};
  46. std::shared_ptr<multiplexer::elem> elem_;
  47. public:
  48. exec_fsm(multiplexer& mpx, std::shared_ptr<multiplexer::elem> elem) noexcept
  49. : mpx_(&mpx)
  50. , elem_(std::move(elem))
  51. { }
  52. exec_action resume(bool connection_is_open, asio::cancellation_type_t cancel_state);
  53. };
  54. } // namespace boost::redis::detail
  55. #endif // BOOST_REDIS_CONNECTOR_HPP