-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathrpc_basic.h
209 lines (171 loc) · 5.32 KB
/
rpc_basic.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright (c) 2020 Sogou, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __RPC_BASIC_H__
#define __RPC_BASIC_H__
#include <stdint.h>
#include <chrono>
#include <google/protobuf/message.h>
#include "rpc_buffer.h"
#include <vector>
#ifdef _WIN32
#include <workflow/PlatformSocket.h>
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#endif
namespace srpc
{
static constexpr const char *SRPC_SCHEME = "srpc";
static constexpr unsigned short SRPC_DEFAULT_PORT = 1412;
static constexpr const char *SRPC_SSL_SCHEME = "srpcs";
static constexpr unsigned short SRPC_SSL_DEFAULT_PORT = 6462;
static constexpr size_t RPC_BODY_SIZE_LIMIT = 2LL * 1024 * 1024 * 1024;
static constexpr int SRPC_MODULE_MAX = 5;
static constexpr size_t SRPC_SPANID_SIZE = 8;
static constexpr size_t SRPC_TRACEID_SIZE = 16;
#ifndef htonll
#if __BYTE_ORDER == __LITTLE_ENDIAN
static inline uint64_t htonll(uint64_t x)
{
return ((uint64_t)htonl(x & 0xFFFFFFFF) << 32) + htonl(x >> 32);
}
static inline uint64_t ntohll(uint64_t x)
{
return ((uint64_t)ntohl(x & 0xFFFFFFFF) << 32) + ntohl(x >> 32);
}
#elif __BYTE_ORDER == __BIG_ENDIAN
static inline uint64_t htonll(uint64_t x)
{
return x;
}
static inline uint64_t ntohll(uint64_t x)
{
return x;
}
#else
# error "unknown byte order"
#endif
#endif
#define SRPC_JSON_OPTION_ADD_WHITESPACE (1<<3)
#define SRPC_JSON_OPTION_ENUM_AS_INITS (1<<4)
#define SRPC_JSON_OPTION_PRESERVE_NAMES (1<<5)
#define SRPC_JSON_OPTION_FIELDS_NO_PRECENCE (1<<6)
using ProtobufIDLMessage = google::protobuf::Message;
using RPCLogVector = std::vector<std::pair<std::string, std::string>>;
enum RPCDataType
{
RPCDataUndefined = -1,
RPCDataProtobuf = 0,
RPCDataThrift = 1,
RPCDataJson = 2,
};
enum RPCStatusCode
{
RPCStatusUndefined = 0,
RPCStatusOK = 1,
RPCStatusServiceNotFound = 2,
RPCStatusMethodNotFound = 3,
RPCStatusMetaError = 4,
RPCStatusReqCompressSizeInvalid = 5,
RPCStatusReqDecompressSizeInvalid = 6,
RPCStatusReqCompressNotSupported = 7,
RPCStatusReqDecompressNotSupported = 8,
RPCStatusReqCompressError = 9,
RPCStatusReqDecompressError = 10,
RPCStatusReqSerializeError = 11,
RPCStatusReqDeserializeError = 12,
RPCStatusRespCompressSizeInvalid = 13,
RPCStatusRespDecompressSizeInvalid = 14,
RPCStatusRespCompressNotSupported = 15,
RPCStatusRespDecompressNotSupported = 16,
RPCStatusRespCompressError = 17,
RPCStatusRespDecompressError = 18,
RPCStatusRespSerializeError = 19,
RPCStatusRespDeserializeError = 20,
RPCStatusIDLSerializeNotSupported = 21,
RPCStatusIDLDeserializeNotSupported = 22,
RPCStatusModuleFilterFailed = 23,
RPCStatusURIInvalid = 30,
RPCStatusUpstreamFailed = 31,
RPCStatusSystemError = 100,
RPCStatusSSLError = 101,
RPCStatusDNSError = 102,
RPCStatusProcessTerminated = 103,
};
enum RPCCompressType
{
RPCCompressNone = 0,
RPCCompressSnappy = 1,
RPCCompressGzip = 2,
RPCCompressZlib = 3,
RPCCompressLz4 = 4,
RPCCompressMax = 5,
};
enum RPCModuleType
{
RPCModuleTypeEmpty = 0,
RPCModuleTypeTrace = 1,
RPCModuleTypeMetrics = 2,
RPCModuleTypeLog = 3,
RPCModuleTypeCustom = 4,
};
class RPCCommon
{
public:
static void log_format(std::string& key, std::string& value,
const RPCLogVector& fields);
static bool addr_to_string(const struct sockaddr *addr, char *ip_str,
socklen_t len, unsigned short *port);
};
static inline long long GET_CURRENT_MS()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
};
static inline long long GET_CURRENT_MS_STEADY()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
static inline unsigned long long GET_CURRENT_NS()
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
}
static inline void TRACE_ID_BIN_TO_HEX(const uint64_t trace_id[2],
char hex[SRPC_TRACEID_SIZE * 2 + 1])
{
sprintf(hex, "%016llx%016llx", (unsigned long long)ntohll(trace_id[0]),
(unsigned long long)ntohll(trace_id[1]));
}
static inline void SPAN_ID_BIN_TO_HEX(const uint64_t span_id[1],
char hex[SRPC_SPANID_SIZE * 2 + 1])
{
sprintf(hex, "%016llx", (unsigned long long)ntohll(span_id[0]));
}
static inline void TRACE_ID_HEX_TO_BIN(const char hex[SRPC_TRACEID_SIZE * 2 + 1],
uint64_t trace_id[2])
{
sscanf(hex, "%016llx%016llx", (unsigned long long *)&trace_id[0],
(unsigned long long *)&trace_id[1]);
trace_id[0] = ntohll(trace_id[0]);
trace_id[1] = ntohll(trace_id[1]);
}
static inline void SPAN_ID_HEX_TO_BIN(const char hex[SRPC_SPANID_SIZE * 2 + 1],
uint64_t span_id[1])
{
sscanf(hex, "%016llx", (unsigned long long *)&span_id[0]);
span_id[0] = ntohll(span_id[0]);
}
} // end namespace srpc
#endif