Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 08:33:51

0001 //
0002 // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CONNECTION_STATE_DATA_HPP
0009 #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CONNECTION_STATE_DATA_HPP
0010 
0011 #include <boost/mysql/character_set.hpp>
0012 #include <boost/mysql/diagnostics.hpp>
0013 #include <boost/mysql/field_view.hpp>
0014 #include <boost/mysql/metadata_mode.hpp>
0015 
0016 #include <boost/mysql/detail/next_action.hpp>
0017 #include <boost/mysql/detail/pipeline.hpp>
0018 
0019 #include <boost/mysql/impl/internal/protocol/capabilities.hpp>
0020 #include <boost/mysql/impl/internal/protocol/db_flavor.hpp>
0021 #include <boost/mysql/impl/internal/protocol/serialization.hpp>
0022 #include <boost/mysql/impl/internal/sansio/message_reader.hpp>
0023 
0024 #include <array>
0025 #include <cstddef>
0026 #include <cstdint>
0027 #include <vector>
0028 
0029 namespace boost {
0030 namespace mysql {
0031 namespace detail {
0032 
0033 enum class ssl_state
0034 {
0035     unsupported,
0036     inactive,
0037     active,
0038     torn_down,
0039 };
0040 
0041 struct connection_state_data
0042 {
0043     // Is the connection actually connected? Set by handshake
0044     bool is_connected{false};
0045 
0046     // Are we talking to MySQL or MariaDB?
0047     db_flavor flavor{db_flavor::mysql};
0048 
0049     // What are the connection's capabilities?
0050     capabilities current_capabilities;
0051 
0052     // Used by async ops without output diagnostics params, to avoid allocations
0053     diagnostics shared_diag;
0054 
0055     // Temporary field storage, re-used by several ops
0056     std::vector<field_view> shared_fields;
0057 
0058     // Temporary pipeline stage storage, re-used by several ops
0059     std::array<pipeline_request_stage, 2> shared_pipeline_stages;
0060 
0061     // Do we want to retain metadata strings or not? Used to save allocations
0062     metadata_mode meta_mode{metadata_mode::minimal};
0063 
0064     // Is SSL supported/enabled for the current connection?
0065     ssl_state ssl;
0066 
0067     // Do backslashes represent escape sequences? By default they do, but they can
0068     // be disabled using a variable. OK packets include a flag with this info.
0069     bool backslash_escapes{true};
0070 
0071     // The current character set, or a default-constructed character set (will all nullptrs) if unknown
0072     character_set current_charset{};
0073 
0074     // The write buffer
0075     std::vector<std::uint8_t> write_buffer;
0076 
0077     // Reader
0078     message_reader reader;
0079 
0080     std::size_t max_buffer_size() const { return reader.max_buffer_size(); }
0081     bool ssl_active() const { return ssl == ssl_state::active; }
0082     bool supports_ssl() const { return ssl != ssl_state::unsupported; }
0083 
0084     connection_state_data(
0085         std::size_t read_buffer_size,
0086         std::size_t max_buff_size = static_cast<std::size_t>(-1),
0087         bool transport_supports_ssl = false
0088     )
0089         : ssl(transport_supports_ssl ? ssl_state::inactive : ssl_state::unsupported),
0090           reader(read_buffer_size, max_buff_size)
0091     {
0092     }
0093 
0094     void reset()
0095     {
0096         is_connected = false;
0097         flavor = db_flavor::mysql;
0098         current_capabilities = capabilities();
0099         // Metadata mode does not get reset on handshake
0100         reader.reset();
0101         // Writer does not need reset, since every write clears previous state
0102         if (supports_ssl())
0103             ssl = ssl_state::inactive;
0104         backslash_escapes = true;
0105         current_charset = character_set{};
0106     }
0107 
0108     // Reads an OK packet from the reader. This operation is repeated in several places.
0109     error_code deserialize_ok(diagnostics& diag)
0110     {
0111         return deserialize_ok_response(reader.message(), flavor, diag, backslash_escapes);
0112     }
0113 
0114     // Helpers for sans-io algorithms
0115     next_action read(std::uint8_t& seqnum, bool keep_parsing_state = false)
0116     {
0117         // buffer is attached by top_level_algo
0118         reader.prepare_read(seqnum, keep_parsing_state);
0119         return next_action::read({});
0120     }
0121 
0122     template <class Serializable>
0123     next_action write(const Serializable& msg, std::uint8_t& seqnum)
0124     {
0125         // use_ssl is attached by top_level_algo
0126         write_buffer.clear();
0127         seqnum = serialize_top_level(msg, write_buffer, seqnum);
0128         return next_action::write({write_buffer, false});
0129     }
0130 };
0131 
0132 }  // namespace detail
0133 }  // namespace mysql
0134 }  // namespace boost
0135 
0136 #endif