CppGenProxy.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Generates C++ tars service interface out of Protobuf IDL.
  2. //
  3. // This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto
  4. // and net/proto2/compiler/public/plugin.h for more information on plugins.
  5. #include <string>
  6. #include <google/protobuf/compiler/code_generator.h>
  7. #include <google/protobuf/compiler/plugin.h>
  8. #include <google/protobuf/compiler/plugin.pb.h>
  9. #include <google/protobuf/descriptor.h>
  10. #include "CppGenCallback.h"
  11. static std::string GenSyncCall(const ::google::protobuf::MethodDescriptor* method, const std::string& pkg, int indent) {
  12. std::string out;
  13. out.reserve(8 * 1024);
  14. out += ToCppNamespace(method->output_type()->full_name()) + " " + method->name() + "(const " +
  15. ToCppNamespace(method->input_type()->full_name()) + "& req, const std::map<std::string, std::string>& context = TARS_CONTEXT(), " +
  16. "std::map<std::string, std::string>* pResponseContext = NULL)";
  17. out += LineFeed(indent);
  18. out += "{" + LineFeed(++indent);
  19. out += "std::string _os;" + LineFeed(indent) +
  20. "req.SerializeToString(&_os);" + LineFeed(indent) +
  21. "std::vector<char> _vc(_os.begin(), _os.end());" + LineFeed(indent);
  22. out += LineFeed(indent);
  23. out += "std::map<std::string, std::string> _mStatus;" + LineFeed(indent);
  24. out += "shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL, \"" + method->name() + "\", _vc, context, _mStatus);" + LineFeed(indent);
  25. out += "if (pResponseContext)" + LineFeed(++indent);
  26. out += "*pResponseContext = rep->context;" + LineFeed(--indent);
  27. out += LineFeed(indent);
  28. out += ToCppNamespace(method->output_type()->full_name()) + " _ret;" + LineFeed(indent);
  29. out += "_ret.ParseFromArray(rep->sBuffer.data(), rep->sBuffer.size());" + LineFeed(indent) +
  30. "return _ret;";
  31. out += LineFeed(--indent) + "}";
  32. out += LineFeed(indent);
  33. out += LineFeed(indent);
  34. return out;
  35. }
  36. static std::string GenAsyncCall(const ::google::protobuf::MethodDescriptor* method,
  37. const std::string& name,
  38. const std::string& pkg,
  39. int indent) {
  40. std::string out;
  41. out.reserve(8 * 1024);
  42. out += "void async_" + method->name() + "(" + name + "PrxCallbackPtr callback, const " +
  43. ToCppNamespace(method->input_type()->full_name()) + "& req, const std::map<std::string, std::string>& context = TARS_CONTEXT())" + LineFeed(indent);
  44. out += "{" + LineFeed(++indent);
  45. out += "std::string _os;" + LineFeed(indent) +
  46. "req.SerializeToString(&_os);" + LineFeed(indent) +
  47. "std::vector<char> _vc(_os.begin(), _os.end());" + LineFeed(indent);
  48. out += "std::map<std::string, std::string> _mStatus;" + LineFeed(indent);
  49. out += "tars_invoke_async(tars::TARSNORMAL, \"" + method->name() + "\", _vc, context, _mStatus, callback);";
  50. out += LineFeed(--indent) + "}";
  51. out += LineFeed(indent);
  52. return out;
  53. }
  54. std::string GenPrx(const ::google::protobuf::ServiceDescriptor* desc, int indent) {
  55. std::string out;
  56. out.reserve(8 * 1024);
  57. const auto& name = desc->name();
  58. const auto& pkg = desc->file()->package();
  59. const auto& proxy = name + "Proxy";
  60. out += LineFeed(indent);
  61. out += "/* proxy for client */";
  62. out += LineFeed(indent);
  63. out += "class " + proxy + " : public tars::ServantProxy";
  64. out += LineFeed(indent);
  65. out += "{";
  66. out += LineFeed(indent);
  67. out += "public:";
  68. out += LineFeed(++indent);
  69. out += "typedef std::map<std::string, std::string> TARS_CONTEXT;" + LineFeed(indent);
  70. //sort by method name
  71. std::map<std::string, const ::google::protobuf::MethodDescriptor*> m_method;
  72. for (int i = 0; i < desc->method_count(); ++i)
  73. {
  74. m_method[desc->method(i)->name()] = desc->method(i);
  75. }
  76. // gen methods
  77. for(auto it = m_method.begin(); it != m_method.end(); ++it)
  78. {
  79. auto method = it->second;
  80. out += LineFeed(indent);
  81. // sync method call
  82. out += GenSyncCall(method, pkg, indent);
  83. // async method call
  84. out += GenAsyncCall(method, name, pkg, indent);
  85. }
  86. // hash call
  87. out += LineFeed(indent);
  88. out += proxy + "* tars_hash(int64_t key)";
  89. out += "{" + LineFeed(++indent);
  90. out += "return (" + proxy + "*)ServantProxy::tars_hash(key);";
  91. out += LineFeed(--indent) + "}";
  92. out += LineFeed(indent);
  93. // consist hash
  94. out += LineFeed(indent);
  95. out += proxy + "* tars_consistent_hash(int64_t key)";
  96. out += "{" + LineFeed(++indent);
  97. out += "return (" + proxy + "*)ServantProxy::tars_consistent_hash(key);";
  98. out += LineFeed(--indent) + "}";
  99. out += LineFeed(indent);
  100. // set_timeout
  101. out += LineFeed(indent);
  102. out += proxy + "* tars_set_timeout(int msecond)";
  103. out += "{" + LineFeed(++indent);
  104. out += "return (" + proxy + "*)ServantProxy::tars_set_timeout(msecond);";
  105. out += LineFeed(--indent) + "}";
  106. out += LineFeed(--indent) + "}; // end class " + proxy;
  107. out += LineFeed(indent); // end class
  108. out += LineFeed(indent);
  109. out += "typedef tars::TC_AutoPtr<" + proxy + "> " + name + "Prx;";
  110. out += LineFeed(indent);
  111. return out;
  112. }