rpc_message.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_MESSAGE_H__
  14. #define __RPC_MESSAGE_H__
  15. #include <string.h>
  16. #include <string>
  17. #include <workflow/ProtocolMessage.h>
  18. #include "rpc_basic.h"
  19. #include "rpc_filter.h"
  20. #include "rpc_thrift_idl.h"
  21. namespace srpc
  22. {
  23. class RPCRequest
  24. {
  25. public:
  26. virtual bool serialize_meta() = 0;
  27. virtual bool deserialize_meta() = 0;
  28. virtual const std::string& get_service_name() const = 0;
  29. virtual const std::string& get_method_name() const = 0;
  30. virtual void set_service_name(const std::string& service_name) = 0;
  31. virtual void set_method_name(const std::string& method_name) = 0;
  32. virtual bool get_meta_module_data(RPCModuleData& data) const = 0;
  33. virtual bool set_meta_module_data(const RPCModuleData& data) = 0;
  34. virtual void set_seqid(long long seqid) {}
  35. public:
  36. virtual ~RPCRequest() { }
  37. };
  38. class RPCResponse
  39. {
  40. public:
  41. virtual bool serialize_meta() = 0;
  42. virtual bool deserialize_meta() = 0;
  43. virtual int get_status_code() const = 0;
  44. virtual int get_error() const = 0;
  45. virtual const char *get_errmsg() const = 0;
  46. virtual void set_status_code(int code) = 0;
  47. virtual void set_error(int error) = 0;
  48. virtual bool set_http_code(int code) { return false; }
  49. public:
  50. virtual ~RPCResponse() { }
  51. };
  52. class RPCMessage
  53. {
  54. public:
  55. virtual void set_data_type(int type) = 0;
  56. virtual void set_compress_type(int type) = 0;
  57. virtual int get_compress_type() const = 0;
  58. virtual int get_data_type() const = 0;
  59. public:
  60. //return RPCStatus
  61. virtual int compress() = 0;
  62. virtual int decompress() = 0;
  63. virtual bool get_meta_module_data(RPCModuleData& data) const = 0;
  64. virtual bool set_meta_module_data(const RPCModuleData& data) = 0;
  65. virtual bool set_http_header(const std::string& name,
  66. const std::string& value)
  67. {
  68. return false;
  69. }
  70. virtual bool add_http_header(const std::string& name,
  71. const std::string& value)
  72. {
  73. return false;
  74. }
  75. virtual bool get_http_header(const std::string& name,
  76. std::string& value) const
  77. {
  78. return false;
  79. }
  80. virtual void set_json_add_whitespace(bool on);
  81. virtual bool get_json_add_whitespace() const;
  82. virtual void set_json_enums_as_ints(bool on);
  83. virtual bool get_json_enums_as_ints() const;
  84. virtual void set_json_preserve_names(bool on);
  85. virtual bool get_json_preserve_names() const;
  86. virtual void set_json_print_primitive(bool on);
  87. virtual bool get_json_print_primitive() const;
  88. public:
  89. //pb
  90. virtual int serialize(const ProtobufIDLMessage *idl_msg)
  91. {
  92. return RPCStatusIDLSerializeNotSupported;
  93. }
  94. virtual int deserialize(ProtobufIDLMessage *idl_msg)
  95. {
  96. return RPCStatusIDLDeserializeNotSupported;
  97. }
  98. public:
  99. //thrift
  100. virtual int serialize(const ThriftIDLMessage *idl_msg)
  101. {
  102. return RPCStatusIDLSerializeNotSupported;
  103. }
  104. virtual int deserialize(ThriftIDLMessage *idl_msg)
  105. {
  106. return RPCStatusIDLDeserializeNotSupported;
  107. }
  108. public:
  109. RPCMessage() { this->flags = 0; }
  110. virtual ~RPCMessage() { }
  111. protected:
  112. uint32_t flags;
  113. };
  114. // implementation
  115. inline void RPCMessage::set_json_add_whitespace(bool on)
  116. {
  117. if (on)
  118. this->flags |= SRPC_JSON_OPTION_ADD_WHITESPACE;
  119. else
  120. this->flags &= ~SRPC_JSON_OPTION_ADD_WHITESPACE;
  121. }
  122. inline bool RPCMessage::get_json_add_whitespace() const
  123. {
  124. return this->flags & SRPC_JSON_OPTION_ADD_WHITESPACE;
  125. }
  126. inline void RPCMessage::set_json_enums_as_ints(bool on)
  127. {
  128. if (on)
  129. this->flags |= SRPC_JSON_OPTION_ENUM_AS_INITS;
  130. else
  131. this->flags &= ~SRPC_JSON_OPTION_ENUM_AS_INITS;
  132. }
  133. inline bool RPCMessage::get_json_enums_as_ints() const
  134. {
  135. return this->flags & SRPC_JSON_OPTION_ENUM_AS_INITS;
  136. }
  137. inline void RPCMessage::set_json_preserve_names(bool on)
  138. {
  139. if (on)
  140. this->flags |= SRPC_JSON_OPTION_PRESERVE_NAMES;
  141. else
  142. this->flags &= ~SRPC_JSON_OPTION_PRESERVE_NAMES;
  143. }
  144. inline bool RPCMessage::get_json_preserve_names() const
  145. {
  146. return this->flags & SRPC_JSON_OPTION_PRESERVE_NAMES;
  147. }
  148. inline void RPCMessage::set_json_print_primitive(bool on)
  149. {
  150. if (on)
  151. this->flags |= SRPC_JSON_OPTION_PRINT_PRIMITIVE;
  152. else
  153. this->flags &= ~SRPC_JSON_OPTION_PRINT_PRIMITIVE;
  154. }
  155. inline bool RPCMessage::get_json_print_primitive() const
  156. {
  157. return this->flags & SRPC_JSON_OPTION_PRINT_PRIMITIVE;
  158. }
  159. } // namespace srpc
  160. #endif