forked from aeraki-mesh/meta-protocol-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_impl.h
144 lines (107 loc) · 4.68 KB
/
message_impl.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
137
138
139
140
141
142
143
144
#pragma once
#include "envoy/http/header_map.h"
#include "src/application_protocols/dubbo/hessian_utils.h"
#include "src/application_protocols/dubbo/message.h"
namespace Envoy {
namespace Extensions {
namespace NetworkFilters {
namespace MetaProtocolProxy {
namespace Dubbo {
class ContextImpl : public Context {
public:
// MetaProtocolProxy::Context
size_t headerSize() const override { return header_size_; }
size_t bodySize() const override { return body_size_; }
bool isHeartbeat() const override { return is_heartbeat_; }
void setHeaderSize(size_t size) { header_size_ = size; }
void setBodySize(size_t size) { body_size_ = size; }
void setHeartbeat(bool heartbeat) { is_heartbeat_ = heartbeat; }
private:
size_t header_size_{0};
size_t body_size_{0};
bool is_heartbeat_{false};
};
class RpcInvocationBase : public RpcInvocation {
public:
~RpcInvocationBase() override = default;
void setServiceName(const std::string& name) { service_name_ = name; }
const std::string& serviceName() const override { return service_name_; }
void setMethodName(const std::string& name) { method_name_ = name; }
const std::string& methodName() const override { return method_name_; }
void setServiceVersion(const std::string& version) { service_version_ = version; }
const absl::optional<std::string>& serviceVersion() const override { return service_version_; }
void setServiceGroup(const std::string& group) { group_ = group; }
const absl::optional<std::string>& serviceGroup() const override { return group_; }
protected:
std::string service_name_;
std::string method_name_;
absl::optional<std::string> service_version_;
absl::optional<std::string> group_;
};
class RpcInvocationImpl : public RpcInvocationBase {
public:
// Each parameter consists of a parameter binary size and Hessian2::Object.
using Parameters = std::vector<Hessian2::ObjectPtr>;
using ParametersPtr = std::unique_ptr<Parameters>;
class Attachment {
public:
using Map = Hessian2::UntypedMapObject;
using MapPtr = std::unique_ptr<Hessian2::UntypedMapObject>;
using String = Hessian2::StringObject;
Attachment(MapPtr&& value, size_t offset);
const Map& attachment() const { return *attachment_; }
void insert(const std::string& key, const std::string& value);
void remove(const std::string& key);
const std::string* lookup(const std::string& key) const;
// Http::HeaderMap wrapper to attachment.
const Http::HeaderMap& headers() const { return *headers_; }
// Whether the attachment should be re-serialized.
bool attachmentUpdated() const { return attachment_updated_; }
size_t attachmentOffset() const { return attachment_offset_; }
private:
bool attachment_updated_{false};
MapPtr attachment_;
// The binary offset of attachment in the original message. Retaining this value can help
// subsequent re-serialization of the attachment without re-serializing the parameters.
size_t attachment_offset_{};
// To reuse the HeaderMatcher API and related tools provided by Envoy, we store the key/value
// pair of the string type in the attachment in the Http::HeaderMap. This introduces additional
// overhead and ignores the case of the key in the attachment. But for now, it's acceptable.
Http::HeaderMapPtr headers_;
};
using AttachmentPtr = std::unique_ptr<Attachment>;
using AttachmentLazyCallback = std::function<AttachmentPtr()>;
using ParametersLazyCallback = std::function<ParametersPtr()>;
bool hasParameters() const { return parameters_ != nullptr; }
const Parameters& parameters() const;
ParametersPtr& mutableParameters() const;
bool hasAttachment() const { return attachment_ != nullptr; }
Attachment& attachment() const;
AttachmentPtr& mutableAttachment() const;
void setParametersLazyCallback(ParametersLazyCallback&& callback) {
parameters_lazy_callback_ = std::move(callback);
}
void setAttachmentLazyCallback(AttachmentLazyCallback&& callback) {
attachment_lazy_callback_ = std::move(callback);
}
const absl::optional<std::string>& serviceGroup() const override;
private:
void assignParametersIfNeed() const;
void assignAttachmentIfNeed() const;
AttachmentLazyCallback attachment_lazy_callback_;
ParametersLazyCallback parameters_lazy_callback_;
mutable ParametersPtr parameters_{};
mutable AttachmentPtr attachment_{};
};
class RpcResultImpl : public RpcResult {
public:
bool hasException() const override { return has_exception_; }
void setException(bool has_exception) { has_exception_ = has_exception; }
private:
bool has_exception_{false};
};
} // namespace Dubbo
} // namespace MetaProtocolProxy
} // namespace NetworkFilters
} // namespace Extensions
} // namespace Envoy