Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-17 09:00:18

0001 //
0002 // Copyright (c) 2025 Marcelo Zimbres Silva (mzimbres@gmail.com),
0003 // Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
0004 //
0005 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 
0009 #ifndef BOOST_REDIS_WRITER_FSM_HPP
0010 #define BOOST_REDIS_WRITER_FSM_HPP
0011 
0012 #include <boost/asio/cancellation_type.hpp>
0013 #include <boost/assert.hpp>
0014 #include <boost/system/error_code.hpp>
0015 
0016 #include <chrono>
0017 #include <cstddef>
0018 
0019 // Sans-io algorithm for the writer task, as a finite state machine
0020 
0021 namespace boost::redis::detail {
0022 
0023 // Forward decls
0024 struct connection_state;
0025 
0026 // What should we do next?
0027 enum class writer_action_type
0028 {
0029    done,        // Call the final handler
0030    write_some,  // Issue a write on the stream
0031    wait,        // Wait until there is data to be written
0032 };
0033 
0034 class writer_action {
0035    writer_action_type type_;
0036    union {
0037       system::error_code ec_;
0038       std::chrono::steady_clock::duration timeout_;
0039    };
0040 
0041    writer_action(writer_action_type type, std::chrono::steady_clock::duration t) noexcept
0042    : type_{type}
0043    , timeout_{t}
0044    { }
0045 
0046 public:
0047    writer_action_type type() const { return type_; }
0048 
0049    writer_action(system::error_code ec) noexcept
0050    : type_{writer_action_type::done}
0051    , ec_{ec}
0052    { }
0053 
0054    static writer_action write_some(std::chrono::steady_clock::duration timeout)
0055    {
0056       return {writer_action_type::write_some, timeout};
0057    }
0058 
0059    static writer_action wait(std::chrono::steady_clock::duration timeout)
0060    {
0061       return {writer_action_type::wait, timeout};
0062    }
0063 
0064    system::error_code error() const
0065    {
0066       BOOST_ASSERT(type_ == writer_action_type::done);
0067       return ec_;
0068    }
0069 
0070    std::chrono::steady_clock::duration timeout() const
0071    {
0072       BOOST_ASSERT(type_ == writer_action_type::write_some || type_ == writer_action_type::wait);
0073       return timeout_;
0074    }
0075 };
0076 
0077 class writer_fsm {
0078    int resume_point_{0};
0079 
0080 public:
0081    writer_fsm() = default;
0082 
0083    writer_action resume(
0084       connection_state& st,
0085       system::error_code ec,
0086       std::size_t bytes_written,
0087       asio::cancellation_type_t cancel_state);
0088 };
0089 
0090 }  // namespace boost::redis::detail
0091 
0092 #endif  // BOOST_REDIS_CONNECTOR_HPP