rpc_types.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_TYPE_H__
  14. #define __RPC_TYPE_H__
  15. #include "rpc_basic.h"
  16. #include "rpc_message.h"
  17. #include "rpc_message_srpc.h"
  18. #include "rpc_message_thrift.h"
  19. #include "rpc_message_brpc.h"
  20. #include "rpc_message_trpc.h"
  21. namespace srpc
  22. {
  23. struct RPCTYPESRPC
  24. {
  25. using REQ = SRPCStdRequest;
  26. using RESP = SRPCStdResponse;
  27. static constexpr RPCDataType default_data_type = RPCDataProtobuf;
  28. static inline void server_reply_init(const REQ *req, RESP *resp)
  29. {
  30. resp->set_data_type(req->get_data_type());
  31. }
  32. };
  33. struct RPCTYPESRPCHttp
  34. {
  35. using REQ = SRPCHttpRequest;
  36. using RESP = SRPCHttpResponse;
  37. static constexpr RPCDataType default_data_type = RPCDataJson;
  38. static inline void server_reply_init(const REQ *req, RESP *resp)
  39. {
  40. resp->set_data_type(req->get_data_type());
  41. }
  42. };
  43. struct RPCTYPEGRPC
  44. {
  45. //using REQ = GRPCHttp2Request;
  46. //using RESP = GRPCHttp2Response;
  47. static constexpr RPCDataType default_data_type = RPCDataProtobuf;
  48. };
  49. struct RPCTYPEBRPC
  50. {
  51. using REQ = BRPCStdRequest;
  52. using RESP = BRPCStdResponse;
  53. static constexpr RPCDataType default_data_type = RPCDataProtobuf;
  54. static inline void server_reply_init(const REQ *req, RESP *resp)
  55. {
  56. resp->set_correlation_id(req->get_correlation_id());
  57. }
  58. };
  59. struct RPCTYPEThrift
  60. {
  61. using REQ = ThriftStdRequest;
  62. using RESP = ThriftStdResponse;
  63. static constexpr RPCDataType default_data_type = RPCDataThrift;
  64. static inline void server_reply_init(const REQ *req, RESP *resp)
  65. {
  66. resp->set_data_type(req->get_data_type());
  67. auto *req_meta = req->get_meta();
  68. auto *resp_meta = resp->get_meta();
  69. resp_meta->is_strict = req_meta->is_strict;
  70. resp_meta->seqid = req_meta->seqid;
  71. resp_meta->method_name = req_meta->method_name;
  72. }
  73. };
  74. struct RPCTYPEThriftHttp
  75. {
  76. using REQ = ThriftHttpRequest;
  77. using RESP = ThriftHttpResponse;
  78. static constexpr RPCDataType default_data_type = RPCDataThrift;
  79. static inline void server_reply_init(const REQ *req, RESP *resp)
  80. {
  81. resp->set_data_type(req->get_data_type());
  82. auto *req_meta = req->get_meta();
  83. auto *resp_meta = resp->get_meta();
  84. resp_meta->is_strict = req_meta->is_strict;
  85. resp_meta->seqid = req_meta->seqid;
  86. resp_meta->method_name = req_meta->method_name;
  87. }
  88. };
  89. struct RPCTYPETRPC
  90. {
  91. using REQ = TRPCStdRequest;
  92. using RESP = TRPCStdResponse;
  93. static constexpr RPCDataType default_data_type = RPCDataProtobuf;
  94. static inline void server_reply_init(const REQ *req, RESP *resp)
  95. {
  96. const_cast<REQ *>(req)->trim_method_prefix();
  97. resp->set_request_id(req->get_request_id());
  98. }
  99. };
  100. struct RPCTYPETRPCHttp
  101. {
  102. using REQ = TRPCHttpRequest;
  103. using RESP = TRPCHttpResponse;
  104. static constexpr RPCDataType default_data_type = RPCDataJson;
  105. static inline void server_reply_init(const REQ *req, RESP *resp)
  106. {
  107. resp->set_data_type(req->get_data_type());
  108. const_cast<REQ *>(req)->trim_method_prefix();
  109. resp->set_request_id(req->get_request_id());
  110. }
  111. };
  112. } // end namespace srpc
  113. #endif