-
Notifications
You must be signed in to change notification settings - Fork 29
/
codec.h
161 lines (138 loc) · 4.37 KB
/
codec.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#pragma once
#include <any>
#include <string>
#include "envoy/buffer/buffer.h"
#include "envoy/common/optref.h"
#include "envoy/common/pure.h"
namespace Envoy {
namespace Extensions {
namespace NetworkFilters {
namespace MetaProtocolProxy {
enum class MessageType {
Request = 0,
Response = 1,
Oneway = 2,
Heartbeat = 3,
Error = 4,
};
enum class ResponseStatus {
Ok = 0,
Error = 1,
};
using AnyOptConstRef = OptRef<const std::any>;
class Properties {
public:
virtual ~Properties() = default;
/**
* Put a any type key:value pair in the metadata.
* Please note that the key:value pair put by this function will not be used for routing.
* Use putString function instead if the decoded key:value pair is intended for routing.
* @param key
* @param value
*/
virtual void put(std::string key, std::any value) PURE;
/**
* Get the value by key from the metadata.
* @param key
* @return
*/
virtual AnyOptConstRef get(std::string key) const PURE;
/**
* Put a string key:value pair in the metadata, the stored value will be used for routing.
* @param key
* @param value
*/
virtual void putString(std::string key, std::string value) PURE;
/**
* Get a string value from the metadata.
* @param key
* @return
*/
virtual std::string getString(std::string key) const PURE;
/**
* Get a bool value from the metadata.
* @param key
* @return
*/
virtual bool getBool(std::string key) const PURE;
};
class Metadata : public Properties {
public:
virtual ~Metadata() = default;
virtual void setOriginMessage(Buffer::Instance&) PURE;
virtual Buffer::Instance& getOriginMessage() PURE;
virtual void setMessageType(MessageType messageType) PURE;
virtual MessageType getMessageType() const PURE;
virtual void setResponseStatus(ResponseStatus responseStatus) PURE;
virtual ResponseStatus getResponseStatus() const PURE;
virtual void setRequestId(uint64_t requestId) PURE;
virtual uint64_t getRequestId() const PURE;
virtual size_t getMessageSize() const PURE;
virtual void setHeaderSize(size_t headerSize) PURE;
virtual size_t getHeaderSize() const PURE;
virtual void setBodySize(size_t bodySize) PURE;
virtual size_t getBodySize() const PURE;
};
using MetadataSharedPtr = std::shared_ptr<Metadata>;
class Mutation : public Properties {
public:
virtual ~Mutation() = default;
};
using MutationSharedPtr = std::shared_ptr<Mutation>;
enum class DecodeStatus {
WaitForData = 0,
Done = 1,
};
enum class ErrorType {
RouteNotFound = 0,
ClusterNotFound = 1,
NoHealthyUpstream = 2,
BadResponse = 3,
Unspecified = 4,
};
struct Error {
ErrorType type;
std::string message;
};
/**
* Codec is used to decode and encode messages of a specific protocol built on top of MetaProtocol.
*/
class Codec {
public:
virtual ~Codec() = default;
/*
* decodes the protocol message.
*
* @param buffer the currently buffered data.
* @param metadata saves the meta data of the current message.
* @return DecodeStatus::DONE if a complete message was successfully consumed,
* DecodeStatus::WaitForData if more data is required.
* @throws EnvoyException if the data is not valid for this protocol.
*/
virtual DecodeStatus decode(Buffer::Instance& buffer, Metadata& metadata) PURE;
/*
* encodes the protocol message.
*
* @param metadata the meta data produced in the decoding phase.
* @param mutation the mutation that needs to be encoded to the message.
* @param buffer save the encoded message.
* @throws EnvoyException if the metadata or mutation is not valid for this protocol.
*/
virtual void encode(const Metadata& metadata, const Mutation& mutation,
Buffer::Instance& buffer) PURE;
/*
* encodes an error message. The encoded error message is used for local reply, for example, envoy
* can't find the specified cluster, or there is no healthy endpoint.
*
* @param metadata the meta data produced in the decoding phase.
* @param error the error that needs to be encoded in the message.
* @param buffer save the encoded message.
* @throws EnvoyException if the metadata is not valid for this protocol.
*/
virtual void onError(const Metadata& metadata, const Error& error, Buffer::Instance& buffer) PURE;
};
using CodecPtr = std::unique_ptr<Codec>;
} // namespace MetaProtocolProxy
} // namespace NetworkFilters
} // namespace Extensions
} // namespace Envoy