1 /*
2 * Copyright (c) 2016, 2021, 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, version 2.0, 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
23 * 02110-1301  USA
24 */
25 
26 #ifndef _NGS_MESSAGE_BUILDER_H_
27 #define _NGS_MESSAGE_BUILDER_H_
28 
29 #include "m_ctype.h"
30 #include "ngs_common/protocol_protobuf.h"
31 #include "ngs/memory.h"
32 
33 namespace ngs
34 {
35   class Output_buffer;
36 
37   class Message_builder
38   {
39   public:
40     Message_builder();
41     ~Message_builder();
42 
43     void encode_empty_message(Output_buffer* out_buffer, uint8 type);
44 
45   protected:
46     typedef ::google::protobuf::io::CodedOutputStream CodedOutputStream;
47     void start_message(Output_buffer* out_buffer, uint8 type);
48     void end_message();
49 
50     Output_buffer     *m_out_buffer;
51     ngs::Memory_instrumented<CodedOutputStream>::Unique_ptr m_out_stream;
52 
53     int m_size_addr2_size;
54 
55     int m_field_number;
56 
57     void encode_int32(int32 value, bool write = true);
58     void encode_uint32(uint32 value, bool write = true);
59     void encode_uint64(uint64 value, bool write = true);
60     void encode_string(const char* value, size_t len, bool write = true);
61   private:
62     // at what byte offset of the m_out_buffer the current row starts
63     uint32 m_start_from;
64 
65     // address of the buffer part where we need to write row size when it's ready
66     google::protobuf::uint8*  m_size_addr1;
67     int m_size_addr1_size;
68     // address of the second buffer part where we need to write row size when it's ready (if it does not fit one page)
69     google::protobuf::uint8*  m_size_addr2;
70   };
71 }
72 
73 
74 #endif //  _NGS_MESSAGE_BUILDER_H_
75