Loading...
Searching...
No Matches
exec_fsm.hpp
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
9#ifndef BOOST_REDIS_EXEC_FSM_HPP
10#define BOOST_REDIS_EXEC_FSM_HPP
11
12#include <boost/redis/detail/multiplexer.hpp>
13
14#include <boost/asio/cancellation_type.hpp>
15#include <boost/system/error_code.hpp>
16
17#include <cstddef>
18#include <memory>
19
20// Sans-io algorithm for async_exec, as a finite state machine
21
22namespace boost::redis::detail {
23
24// What should we do next?
25enum class exec_action_type
26{
27 setup_cancellation, // Set up the cancellation types supported by the composed operation
28 immediate, // Invoke asio::async_immediate to avoid re-entrancy problems
29 done, // Call the final handler
30 notify_writer, // Notify the writer task
31 wait_for_response, // Wait to be notified
32 cancel_run, // Cancel the connection's run operation
33};
34
35class exec_action {
36 exec_action_type type_;
37 system::error_code ec_;
38 std::size_t bytes_read_;
39
40public:
41 exec_action(exec_action_type type) noexcept
42 : type_{type}
43 { }
44
45 exec_action(system::error_code ec, std::size_t bytes_read = 0u) noexcept
46 : type_{exec_action_type::done}
47 , ec_{ec}
48 , bytes_read_{bytes_read}
49 { }
50
51 exec_action_type type() const { return type_; }
52 system::error_code error() const { return ec_; }
53 std::size_t bytes_read() const { return bytes_read_; }
54};
55
56class exec_fsm {
57 int resume_point_{0};
58 multiplexer* mpx_{nullptr};
59 std::shared_ptr<multiplexer::elem> elem_;
60
61public:
62 exec_fsm(multiplexer& mpx, std::shared_ptr<multiplexer::elem> elem) noexcept
63 : mpx_(&mpx)
64 , elem_(std::move(elem))
65 { }
66
67 exec_action resume(bool connection_is_open, asio::cancellation_type_t cancel_state);
68};
69
70} // namespace boost::redis::detail
71
72#endif // BOOST_REDIS_CONNECTOR_HPP