MySQL 8.3.0
Source Code Documentation
classic_protocol_codec_frame.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2023, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQL_ROUTER_CLASSIC_PROTOCOL_CODEC_FRAME_H_
26#define MYSQL_ROUTER_CLASSIC_PROTOCOL_CODEC_FRAME_H_
27
28#include <cstddef> // size_t
29#include <cstdint> // uint8_t
30#include <memory>
31#include <system_error> // error_code
32#include <utility> // move
33
40
41namespace classic_protocol {
42
43/**
44 * Codec of a Frame Header.
45 */
46template <>
47class Codec<frame::Header> : public impl::EncodeBase<Codec<frame::Header>> {
48 template <class Accumulator>
49 constexpr auto accumulate_fields(Accumulator &&accu) const {
50 return accu.step(wire::FixedInt<3>(v_.payload_size()))
51 .step(wire::FixedInt<1>(v_.seq_id()))
52 .result();
53 }
54
55 public:
58
59 friend __base;
60
62 : __base(caps), v_{std::move(v)} {}
63
64 static constexpr size_t max_size() noexcept { return 4; }
65
69
70 auto payload_size_res = accu.template step<wire::FixedInt<3>>();
71 auto seq_id_res = accu.template step<wire::FixedInt<1>>();
72
73 if (!accu.result()) return stdx::make_unexpected(accu.result().error());
74
75 return std::make_pair(
76 accu.result().value(),
77 value_type(payload_size_res->value(), seq_id_res->value()));
78 }
79
80 private:
82};
83
84/**
85 * Codec of Compressed Header.
86 */
87template <>
88class Codec<frame::CompressedHeader>
89 : public impl::EncodeBase<Codec<frame::CompressedHeader>> {
90 template <class Accumulator>
91 auto accumulate_fields(Accumulator &&accu) const {
92 return accu.step(wire::FixedInt<3>(v_.payload_size()))
93 .step(wire::FixedInt<1>(v_.seq_id()))
94 .step(wire::FixedInt<3>(v_.uncompressed_size()))
95 .result();
96 }
97
98 public:
101
102 friend __base;
103
105 : __base(caps), v_{std::move(v)} {}
106
107 static size_t max_size() noexcept { return 7; }
108
112
113 auto payload_size_res = accu.template step<wire::FixedInt<3>>();
114 auto seq_id_res = accu.template step<wire::FixedInt<1>>();
115 auto uncompressed_size_res = accu.template step<wire::FixedInt<3>>();
116
117 if (!accu.result()) return stdx::make_unexpected(accu.result().error());
118
119 return std::make_pair(
120 accu.result().value(),
121 value_type(payload_size_res->value(), seq_id_res->value(),
122 uncompressed_size_res->value()));
123 }
124
125 private:
127};
128
129/**
130 * Codec for a Frame.
131 *
132 * Frame is
133 *
134 * - header
135 * - payload
136 */
137template <class PayloadType>
138class Codec<frame::Frame<PayloadType>>
139 : public impl::EncodeBase<Codec<frame::Frame<PayloadType>>> {
140 template <class Accumulator>
141 constexpr auto accumulate_fields(Accumulator &&accu) const {
142 return accu
143 .step(frame::Header(
144 Codec<PayloadType>(v_.payload(), this->caps()).size(), v_.seq_id()))
145 .step(PayloadType(v_.payload()))
146 .result();
147 }
148
149 public:
152
153 friend __base;
154
156 : __base(caps), v_{std::move(v)} {}
157
161
162 auto header_res = accu.template step<frame::Header>();
163 if (!accu.result()) return stdx::make_unexpected(accu.result().error());
164
165 constexpr const size_t header_size{Codec<frame::Header>::max_size()};
166
167 // check the payload is at least what we expect.
168 if (buffer.size() < header_size + header_res->payload_size()) {
171 }
172
173 auto payload_res =
174 accu.template step<PayloadType>(header_res->payload_size());
175
176 if (!accu.result()) return stdx::make_unexpected(accu.result().error());
177
178 return std::make_pair(
179 accu.result().value(),
180 value_type(header_res->seq_id(), payload_res.value()));
181 }
182
183 private:
185};
186} // namespace classic_protocol
187
188#endif
const value_type v_
Definition: classic_protocol_codec_frame.h:126
constexpr Codec(value_type v, capabilities::value_type caps)
Definition: classic_protocol_codec_frame.h:104
static stdx::expected< std::pair< size_t, value_type >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps)
Definition: classic_protocol_codec_frame.h:109
auto accumulate_fields(Accumulator &&accu) const
Definition: classic_protocol_codec_frame.h:91
friend __base
Definition: classic_protocol_codec_frame.h:102
static size_t max_size() noexcept
Definition: classic_protocol_codec_frame.h:107
constexpr Codec(value_type v, capabilities::value_type caps)
Definition: classic_protocol_codec_frame.h:155
friend __base
Definition: classic_protocol_codec_frame.h:153
const value_type v_
Definition: classic_protocol_codec_frame.h:184
static stdx::expected< std::pair< size_t, value_type >, std::error_code > decode(net::const_buffer buffer, capabilities::value_type caps)
Definition: classic_protocol_codec_frame.h:158
constexpr auto accumulate_fields(Accumulator &&accu) const
Definition: classic_protocol_codec_frame.h:141
const value_type v_
Definition: classic_protocol_codec_frame.h:81
constexpr Codec(value_type v, capabilities::value_type caps)
Definition: classic_protocol_codec_frame.h:61
constexpr auto accumulate_fields(Accumulator &&accu) const
Definition: classic_protocol_codec_frame.h:49
friend __base
Definition: classic_protocol_codec_frame.h:59
static constexpr size_t max_size() noexcept
Definition: classic_protocol_codec_frame.h:64
static stdx::expected< std::pair< size_t, value_type >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps)
Definition: classic_protocol_codec_frame.h:66
Codec for a type.
Definition: classic_protocol_codec_base.h:71
Definition: classic_protocol_wire.h:102
header of a compressed frame.
Definition: classic_protocol_frame.h:57
Definition: classic_protocol_frame.h:84
Definition: classic_protocol_frame.h:35
Generator of decoded Types of a buffer.
Definition: classic_protocol_codec_base.h:152
result_type result() const
get result of the step().
Definition: classic_protocol_codec_base.h:218
CRTP base for the Codec's encode part.
Definition: classic_protocol_codec_base.h:374
Definition: buffer.h:134
size_t size() const noexcept
Definition: buffer.h:119
constexpr const value_type & value() const &
Definition: expected.h:687
constexpr const error_type & error() const &
Definition: expected.h:736
Definition: expected.h:943
std::bitset< 32 > value_type
Definition: classic_protocol_constants.h:72
Definition: classic_protocol_binary.h:38
std::error_code make_error_code(codec_errc e) noexcept
Definition: classic_protocol_codec_error.h:85
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:417
Definition: varlen_sort.h:174
constexpr auto make_unexpected(E &&e) -> unexpected< std::decay_t< E > >
Definition: expected.h:124