libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
RpcEncoder.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef RPCENCODER_H_
32 #define RPCENCODER_H_
33 
34 #include "RpcHeader.h"
35 #include "../Variable.h"
36 #include "BinaryEncoder.h"
37 
38 #include <memory>
39 #include <cstring>
40 #include <list>
41 
42 namespace BaseLib {
43 
44 class SharedObjects;
45 
46 namespace Rpc {
47 
48 class RpcEncoder {
49  public:
50  RpcEncoder();
51  RpcEncoder(bool forceInteger64, bool encodeVoid);
52 
56  explicit RpcEncoder(BaseLib::SharedObjects *baseLib);
57 
61  RpcEncoder(BaseLib::SharedObjects *baseLib, bool forceInteger64, bool encodeVoid);
62 
63  ~RpcEncoder() = default;
64 
65  static void insertHeader(std::vector<char> &packet, const RpcHeader &header);
66  static void insertHeader(std::vector<uint8_t> &packet, const RpcHeader &header);
67  void encodeRequest(const std::string &methodName, const std::shared_ptr<std::list<std::shared_ptr<Variable>>> &parameters, std::vector<char> &encodedData, const std::shared_ptr<RpcHeader> &header = nullptr);
68  void encodeRequest(const std::string &methodName, const std::shared_ptr<std::list<std::shared_ptr<Variable>>> &parameters, std::vector<uint8_t> &encodedData, const std::shared_ptr<RpcHeader> &header = nullptr);
69  void encodeRequest(const std::string &methodName, const PArray &parameters, std::vector<char> &encodedData, const std::shared_ptr<RpcHeader> &header = nullptr);
70  void encodeRequest(const std::string &methodName, const PArray &parameters, std::vector<uint8_t> &encodedData, const std::shared_ptr<RpcHeader> &header = nullptr);
71  void encodeResponse(const std::shared_ptr<Variable> &variable, std::vector<char> &encodedData);
72  void encodeResponse(const std::shared_ptr<Variable> &variable, std::vector<uint8_t> &encodedData);
73  private:
74  bool _forceInteger64 = false;
75  bool _encodeVoid = false;
76  char _packetStartRequest[4];
77  char _packetStartResponse[5];
78  char _packetStartError[5];
79 
80  static void expandPacket(std::vector<char> &packet, size_t sizeToInsert);
81  static void expandPacket(std::vector<uint8_t> &packet, size_t sizeToInsert);
82  static uint32_t encodeHeader(std::vector<char> &packet, const RpcHeader &header);
83  static uint32_t encodeHeader(std::vector<uint8_t> &packet, const RpcHeader &header);
84  void encodeVariable(std::vector<char> &packet, const std::shared_ptr<Variable> &variable);
85  void encodeVariable(std::vector<uint8_t> &packet, const std::shared_ptr<Variable> &variable);
86  static void encodeInteger(std::vector<char> &packet, const std::shared_ptr<Variable> &variable);
87  static void encodeInteger(std::vector<uint8_t> &packet, const std::shared_ptr<Variable> &variable);
88  static void encodeInteger64(std::vector<char> &packet, const std::shared_ptr<Variable> &variable);
89  static void encodeInteger64(std::vector<uint8_t> &packet, const std::shared_ptr<Variable> &variable);
90  static void encodeFloat(std::vector<char> &packet, const std::shared_ptr<Variable> &variable);
91  static void encodeFloat(std::vector<uint8_t> &packet, const std::shared_ptr<Variable> &variable);
92  static void encodeBoolean(std::vector<char> &packet, const std::shared_ptr<Variable> &variable);
93  static void encodeBoolean(std::vector<uint8_t> &packet, const std::shared_ptr<Variable> &variable);
94  static void encodeType(std::vector<char> &packet, VariableType type);
95  static void encodeType(std::vector<uint8_t> &packet, VariableType type);
96  static void encodeString(std::vector<char> &packet, const std::shared_ptr<Variable> &variable);
97  static void encodeString(std::vector<uint8_t> &packet, const std::shared_ptr<Variable> &variable);
98  static void encodeBase64(std::vector<char> &packet, const std::shared_ptr<Variable> &variable);
99  static void encodeBase64(std::vector<uint8_t> &packet, const std::shared_ptr<Variable> &variable);
100  static void encodeBinary(std::vector<char> &packet, const std::shared_ptr<Variable> &variable);
101  static void encodeBinary(std::vector<uint8_t> &packet, const std::shared_ptr<Variable> &variable);
102  void encodeVoid(std::vector<char> &packet);
103  void encodeVoid(std::vector<uint8_t> &packet);
104  void encodeStruct(std::vector<char> &packet, const std::shared_ptr<Variable> &variable);
105  void encodeStruct(std::vector<uint8_t> &packet, const std::shared_ptr<Variable> &variable);
106  void encodeArray(std::vector<char> &packet, const std::shared_ptr<Variable> &variable);
107  void encodeArray(std::vector<uint8_t> &packet, const std::shared_ptr<Variable> &variable);
108 };
109 }
110 }
111 #endif
static void insertHeader(std::vector< char > &packet, const RpcHeader &header)
Definition: RpcEncoder.cpp:189
This is the base library main class.
Definition: BaseLib.h:95
Definition: BaseLib.cpp:34
void encodeRequest(const std::string &methodName, const std::shared_ptr< std::list< std::shared_ptr< Variable >>> &parameters, std::vector< char > &encodedData, const std::shared_ptr< RpcHeader > &header=nullptr)
Definition: RpcEncoder.cpp:59
std::shared_ptr< Array > PArray
Definition: Variable.h:72
VariableType
Definition: Variable.h:49
Definition: RpcEncoder.h:48
Definition: RpcHeader.h:38
RpcEncoder()
Definition: RpcEncoder.cpp:37
void encodeResponse(const std::shared_ptr< Variable > &variable, std::vector< char > &encodedData)
Definition: RpcEncoder.cpp:159