forked from aeraki-mesh/meta-protocol-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializer.h
136 lines (115 loc) · 4.48 KB
/
serializer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma once
#include <string>
#include "envoy/buffer/buffer.h"
#include "envoy/config/typed_config.h"
#include "source/common/common/assert.h"
#include "source/common/config/utility.h"
#include "source/common/singleton/const_singleton.h"
#include "src/application_protocols/dubbo/message.h"
#include "src/application_protocols/dubbo/metadata.h"
#include "src/application_protocols/dubbo/protocol_constants.h"
namespace Envoy {
namespace Extensions {
namespace NetworkFilters {
namespace MetaProtocolProxy {
namespace Dubbo {
class Serializer {
public:
virtual ~Serializer() = default;
/**
* Return this Serializer's name
*
* @return std::string containing the serialization name.
*/
virtual const std::string& name() const PURE;
/**
* @return SerializationType the serializer type
*/
virtual SerializationType type() const PURE;
/**
* deserialize an rpc call
* If successful, the RpcInvocation removed from the buffer
*
* @param buffer the currently buffered dubbo data
* @param context context information for RPC messages
* @return a pair containing the deserialized result of the message and the deserialized
* invocation information.
* @throws EnvoyException if the data is not valid for this serialization
*/
virtual std::pair<RpcInvocationSharedPtr, bool>
deserializeRpcInvocation(Buffer::Instance& buffer, ContextSharedPtr context) PURE;
/**
* serialize an rpc call
* If successful, the output_buffer is written to the serialized data
*
* @param output_buffer store the serialized data
* @param content the rpc response content
* @return size_t the length of the serialized content
*/
virtual size_t serializeRpcInvocation(Buffer::Instance& output_buffer) PURE;
/**
* deserialize result of an rpc call
*
* @param buffer the currently buffered dubbo data
* @param context context information for RPC messages
* @return a pair containing the deserialized result of the message and the deserialized
* result information.
* @throws EnvoyException if the data is not valid for this serialization
*/
virtual std::pair<RpcResultSharedPtr, bool> deserializeRpcResult(Buffer::Instance& buffer,
ContextSharedPtr context) PURE;
/**
* serialize result of an rpc call
* If successful, the output_buffer is written to the serialized data
*
* @param output_buffer store the serialized data
* @param content the rpc response content
* @param type the rpc response type
* @return size_t the length of the serialized content
*/
virtual size_t serializeRpcResult(Buffer::Instance& output_buffer, const std::string& content,
RpcResponseType type) PURE;
};
using SerializerPtr = std::unique_ptr<Serializer>;
/**
* Implemented by each Dubbo serialize and registered via Registry::registerFactory or the
* convenience class RegisterFactory.
*/
class NamedSerializerConfigFactory : public Config::UntypedFactory {
public:
~NamedSerializerConfigFactory() override = default;
/**
* Create a particular Dubbo serializer.
* @return SerializerPtr the transport
*/
virtual SerializerPtr createSerializer() PURE;
std::string category() const override { return "envoy.dubbo_proxy.serializers"; }
/**
* Convenience method to lookup a factory by type.
* @param TransportType the transport type
* @return NamedSerializerConfigFactory& for the TransportType
*/
static NamedSerializerConfigFactory& getFactory(ProtocolType protocol_type,
SerializationType type) {
const std::string& name = ProtocolSerializerNames::get().fromType(protocol_type, type);
return Envoy::Config::Utility::getAndCheckFactoryByName<NamedSerializerConfigFactory>(name);
}
};
/**
* SerializerFactoryBase provides a template for a trivial NamedSerializerConfigFactory.
*/
template <class SerializerImpl> class SerializerFactoryBase : public NamedSerializerConfigFactory {
public:
SerializerPtr createSerializer() override { return std::make_unique<SerializerImpl>(); }
std::string name() const override { return name_; }
protected:
SerializerFactoryBase(ProtocolType protocol_type, SerializationType type)
: name_(ProtocolSerializerNames::get().fromType(protocol_type, type)) {}
private:
const std::string name_;
};
} // namespace Dubbo
} // namespace MetaProtocolProxy
} // namespace NetworkFilters
} // namespace Extensions
} // namespace Envoy