| 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 | |
| 8 | #ifndef BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CONNECTION_STATE_DATA_HPP |
| 9 | #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_CONNECTION_STATE_DATA_HPP |
| 10 | |
| 11 | #include <boost/mysql/character_set.hpp> |
| 12 | #include <boost/mysql/diagnostics.hpp> |
| 13 | #include <boost/mysql/field_view.hpp> |
| 14 | #include <boost/mysql/metadata_mode.hpp> |
| 15 | |
| 16 | #include <boost/mysql/impl/internal/protocol/capabilities.hpp> |
| 17 | #include <boost/mysql/impl/internal/protocol/db_flavor.hpp> |
| 18 | #include <boost/mysql/impl/internal/protocol/protocol.hpp> |
| 19 | #include <boost/mysql/impl/internal/sansio/message_reader.hpp> |
| 20 | #include <boost/mysql/impl/internal/sansio/message_writer.hpp> |
| 21 | |
| 22 | #include <cstddef> |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace boost { |
| 26 | namespace mysql { |
| 27 | namespace detail { |
| 28 | |
| 29 | enum class ssl_state |
| 30 | { |
| 31 | unsupported, |
| 32 | inactive, |
| 33 | active, |
| 34 | torn_down, |
| 35 | }; |
| 36 | |
| 37 | struct connection_state_data |
| 38 | { |
| 39 | // Is the connection actually connected? Set by handshake |
| 40 | bool is_connected{false}; |
| 41 | |
| 42 | // Are we talking to MySQL or MariaDB? |
| 43 | db_flavor flavor{db_flavor::mysql}; |
| 44 | |
| 45 | // What are the connection's capabilities? |
| 46 | capabilities current_capabilities; |
| 47 | |
| 48 | // Used by async ops without output diagnostics params, to avoid allocations |
| 49 | diagnostics shared_diag; |
| 50 | |
| 51 | // Temporary field storage, re-used by several ops |
| 52 | std::vector<field_view> shared_fields; |
| 53 | |
| 54 | // Do we want to retain metadata strings or not? Used to save allocations |
| 55 | metadata_mode meta_mode{metadata_mode::minimal}; |
| 56 | |
| 57 | // Is SSL supported/enabled for the current connection? |
| 58 | ssl_state ssl; |
| 59 | |
| 60 | // Do backslashes represent escape sequences? By default they do, but they can |
| 61 | // be disabled using a variable. OK packets include a flag with this info. |
| 62 | bool backslash_escapes{true}; |
| 63 | |
| 64 | // The current character set, or a default-constructed character set (will all nullptrs) if unknown |
| 65 | character_set current_charset{}; |
| 66 | |
| 67 | // Reader and writer |
| 68 | message_reader reader; |
| 69 | message_writer writer; |
| 70 | |
| 71 | bool ssl_active() const noexcept { return ssl == ssl_state::active; } |
| 72 | bool supports_ssl() const noexcept { return ssl != ssl_state::unsupported; } |
| 73 | |
| 74 | const character_set* charset_ptr() const noexcept |
| 75 | { |
| 76 | return current_charset.name.empty() ? nullptr : ¤t_charset; |
| 77 | } |
| 78 | |
| 79 | connection_state_data(std::size_t read_buffer_size, bool transport_supports_ssl = false) |
| 80 | : ssl(transport_supports_ssl ? ssl_state::inactive : ssl_state::unsupported), reader(read_buffer_size) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | void reset() |
| 85 | { |
| 86 | is_connected = false; |
| 87 | flavor = db_flavor::mysql; |
| 88 | current_capabilities = capabilities(); |
| 89 | // Metadata mode does not get reset on handshake |
| 90 | reader.reset(); |
| 91 | // Writer does not need reset, since every write clears previous state |
| 92 | if (supports_ssl()) |
| 93 | ssl = ssl_state::inactive; |
| 94 | backslash_escapes = true; |
| 95 | current_charset = character_set{}; |
| 96 | } |
| 97 | |
| 98 | // Reads an OK packet from the reader. This operation is repeated in several places. |
| 99 | error_code deserialize_ok(diagnostics& diag) noexcept |
| 100 | { |
| 101 | return deserialize_ok_response(message: reader.message(), flavor, diag, backslash_escapes); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | } // namespace detail |
| 106 | } // namespace mysql |
| 107 | } // namespace boost |
| 108 | |
| 109 | #endif |
| 110 | |