rpc_basic.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Copyright (c) 2020 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __RPC_BASIC_H__
  14. #define __RPC_BASIC_H__
  15. #include <stdint.h>
  16. #include <chrono>
  17. #include <google/protobuf/message.h>
  18. #include "rpc_buffer.h"
  19. #include <vector>
  20. #ifdef _WIN32
  21. #include <workflow/PlatformSocket.h>
  22. #else
  23. #include <arpa/inet.h>
  24. #include <sys/socket.h>
  25. #endif
  26. namespace srpc
  27. {
  28. static constexpr const char *SRPC_SCHEME = "srpc";
  29. static constexpr unsigned short SRPC_DEFAULT_PORT = 1412;
  30. static constexpr const char *SRPC_SSL_SCHEME = "srpcs";
  31. static constexpr unsigned short SRPC_SSL_DEFAULT_PORT = 6462;
  32. static constexpr size_t RPC_BODY_SIZE_LIMIT = 2LL * 1024 * 1024 * 1024;
  33. static constexpr int SRPC_MODULE_MAX = 5;
  34. static constexpr size_t SRPC_SPANID_SIZE = 8;
  35. static constexpr size_t SRPC_TRACEID_SIZE = 16;
  36. #ifndef htonll
  37. #if __BYTE_ORDER == __LITTLE_ENDIAN
  38. static inline uint64_t htonll(uint64_t x)
  39. {
  40. return ((uint64_t)htonl(x & 0xFFFFFFFF) << 32) + htonl(x >> 32);
  41. }
  42. static inline uint64_t ntohll(uint64_t x)
  43. {
  44. return ((uint64_t)ntohl(x & 0xFFFFFFFF) << 32) + ntohl(x >> 32);
  45. }
  46. #elif __BYTE_ORDER == __BIG_ENDIAN
  47. static inline uint64_t htonll(uint64_t x)
  48. {
  49. return x;
  50. }
  51. static inline uint64_t ntohll(uint64_t x)
  52. {
  53. return x;
  54. }
  55. #else
  56. # error "unknown byte order"
  57. #endif
  58. #endif
  59. #define SRPC_JSON_OPTION_ADD_WHITESPACE (1<<3)
  60. #define SRPC_JSON_OPTION_ENUM_AS_INITS (1<<4)
  61. #define SRPC_JSON_OPTION_PRESERVE_NAMES (1<<5)
  62. #define SRPC_JSON_OPTION_PRINT_PRIMITIVE (1<<6)
  63. using ProtobufIDLMessage = google::protobuf::Message;
  64. using RPCLogVector = std::vector<std::pair<std::string, std::string>>;
  65. enum RPCDataType
  66. {
  67. RPCDataUndefined = -1,
  68. RPCDataProtobuf = 0,
  69. RPCDataThrift = 1,
  70. RPCDataJson = 2,
  71. };
  72. enum RPCStatusCode
  73. {
  74. RPCStatusUndefined = 0,
  75. RPCStatusOK = 1,
  76. RPCStatusServiceNotFound = 2,
  77. RPCStatusMethodNotFound = 3,
  78. RPCStatusMetaError = 4,
  79. RPCStatusReqCompressSizeInvalid = 5,
  80. RPCStatusReqDecompressSizeInvalid = 6,
  81. RPCStatusReqCompressNotSupported = 7,
  82. RPCStatusReqDecompressNotSupported = 8,
  83. RPCStatusReqCompressError = 9,
  84. RPCStatusReqDecompressError = 10,
  85. RPCStatusReqSerializeError = 11,
  86. RPCStatusReqDeserializeError = 12,
  87. RPCStatusRespCompressSizeInvalid = 13,
  88. RPCStatusRespDecompressSizeInvalid = 14,
  89. RPCStatusRespCompressNotSupported = 15,
  90. RPCStatusRespDecompressNotSupported = 16,
  91. RPCStatusRespCompressError = 17,
  92. RPCStatusRespDecompressError = 18,
  93. RPCStatusRespSerializeError = 19,
  94. RPCStatusRespDeserializeError = 20,
  95. RPCStatusIDLSerializeNotSupported = 21,
  96. RPCStatusIDLDeserializeNotSupported = 22,
  97. RPCStatusURIInvalid = 30,
  98. RPCStatusUpstreamFailed = 31,
  99. RPCStatusSystemError = 100,
  100. RPCStatusSSLError = 101,
  101. RPCStatusDNSError = 102,
  102. RPCStatusProcessTerminated = 103,
  103. };
  104. enum RPCCompressType
  105. {
  106. RPCCompressNone = 0,
  107. RPCCompressSnappy = 1,
  108. RPCCompressGzip = 2,
  109. RPCCompressZlib = 3,
  110. RPCCompressLz4 = 4,
  111. RPCCompressMax = 5,
  112. };
  113. enum RPCModuleType
  114. {
  115. RPCModuleTypeEmpty = 0,
  116. RPCModuleTypeTrace = 1,
  117. RPCModuleTypeMetrics = 2,
  118. RPCModuleTypeLog = 3,
  119. };
  120. class RPCCommon
  121. {
  122. public:
  123. static void log_format(std::string& key, std::string& value,
  124. const RPCLogVector& fields);
  125. static bool addr_to_string(const struct sockaddr *addr, char *ip_str,
  126. socklen_t len, unsigned short *port);
  127. };
  128. static inline long long GET_CURRENT_MS()
  129. {
  130. return std::chrono::duration_cast<std::chrono::milliseconds>(
  131. std::chrono::system_clock::now().time_since_epoch()).count();
  132. };
  133. static inline long long GET_CURRENT_MS_STEADY()
  134. {
  135. return std::chrono::duration_cast<std::chrono::milliseconds>(
  136. std::chrono::steady_clock::now().time_since_epoch()).count();
  137. }
  138. static inline unsigned long long GET_CURRENT_NS()
  139. {
  140. return std::chrono::duration_cast<std::chrono::nanoseconds>(
  141. std::chrono::system_clock::now().time_since_epoch()).count();
  142. }
  143. static inline void TRACE_ID_BIN_TO_HEX(const uint64_t trace_id[2],
  144. char hex[SRPC_TRACEID_SIZE * 2 + 1])
  145. {
  146. sprintf(hex, "%016llx%016llx", (unsigned long long)ntohll(trace_id[0]),
  147. (unsigned long long)ntohll(trace_id[1]));
  148. }
  149. static inline void SPAN_ID_BIN_TO_HEX(const uint64_t span_id[1],
  150. char hex[SRPC_SPANID_SIZE * 2 + 1])
  151. {
  152. sprintf(hex, "%016llx", (unsigned long long)ntohll(span_id[0]));
  153. }
  154. static inline void TRACE_ID_HEX_TO_BIN(const char hex[SRPC_TRACEID_SIZE * 2 + 1],
  155. uint64_t trace_id[2])
  156. {
  157. sscanf(hex, "%016llx%016llx", (unsigned long long *)&trace_id[0],
  158. (unsigned long long *)&trace_id[1]);
  159. trace_id[0] = ntohll(trace_id[0]);
  160. trace_id[1] = ntohll(trace_id[1]);
  161. }
  162. static inline void SPAN_ID_HEX_TO_BIN(const char hex[SRPC_SPANID_SIZE * 2 + 1],
  163. uint64_t span_id[1])
  164. {
  165. sscanf(hex, "%016llx", (unsigned long long *)&span_id[0]);
  166. span_id[0] = ntohll(span_id[0]);
  167. }
  168. } // end namespace srpc
  169. #endif