CppGenCallback.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <google/protobuf/compiler/code_generator.h>
  6. #include <google/protobuf/compiler/plugin.h>
  7. #include <google/protobuf/compiler/plugin.pb.h>
  8. #include <google/protobuf/descriptor.h>
  9. #include "CppGenCallback.h"
  10. static std::string GenCallbackMethod(const ::google::protobuf::MethodDescriptor* method, const std::string& pkg, int indent) {
  11. std::string out;
  12. out.reserve(8 * 1024);
  13. out = "virtual void callback_" + method->name() + "(const " + ToCppNamespace(method->output_type()->full_name()) + "& ret)" + LineFeed(indent);
  14. out += "{ throw std::runtime_error(\"callback_" + method->name() + " override incorrect.\"); }" + LineFeed(indent);
  15. out += "virtual void callback_" + method->name() + "_exception(tars::Int32 ret)" + LineFeed(indent);
  16. out += "{ throw std::runtime_error(\"callback_" + method->name() + "_exception() override incorrect.\"); }" + LineFeed(indent);
  17. return out;
  18. }
  19. static std::string GenResponseContext(int indent) {
  20. std::string out;
  21. out.reserve(8 * 1024);
  22. out += "virtual const map<std::string, std::string> & getResponseContext() const";
  23. out += LineFeed(indent);
  24. out += "{";
  25. out += LineFeed(++indent);
  26. out += "CallbackThreadData * pCbtd = CallbackThreadData::getData();";
  27. out += LineFeed(indent);
  28. out += LineFeed(indent);
  29. out += "assert(pCbtd != NULL);";
  30. out += LineFeed(indent);
  31. out += "if (!pCbtd->getContextValid())";
  32. out += LineFeed(indent);
  33. out += "{";
  34. out += LineFeed(++indent);
  35. out += "throw TC_Exception(\"can't get response context\");";
  36. out += LineFeed(--indent);
  37. out += "}";
  38. out += LineFeed(indent);
  39. out += "return pCbtd->getResponseContext();";
  40. out += LineFeed(--indent);
  41. out += "}";
  42. return out;
  43. }
  44. std::string GenPrxCallback(const ::google::protobuf::ServiceDescriptor* desc, int indent) {
  45. std::string out;
  46. out.reserve(8 * 1024);
  47. const auto& name = desc->name();
  48. const auto& pkg = desc->file()->package();
  49. out += "/* callback of async proxy for client */";
  50. out += LineFeed(indent);
  51. out += "class " + name + "PrxCallback : public tars::ServantProxyCallback" + LineFeed(indent);
  52. out += "{";
  53. out += LineFeed(indent);
  54. out += "public:" + LineFeed(++indent) + "virtual ~" + name + "PrxCallback() {}";
  55. out += LineFeed(indent);
  56. out += LineFeed(indent);
  57. //sort by method name
  58. std::map<std::string, const ::google::protobuf::MethodDescriptor*> m_method;
  59. for (int i = 0; i < desc->method_count(); ++i)
  60. {
  61. m_method[desc->method(i)->name()] = desc->method(i);
  62. }
  63. for(auto it = m_method.begin(); it != m_method.end(); ++it)
  64. {
  65. auto method = it->second;
  66. out += GenCallbackMethod(method, pkg, indent);
  67. }
  68. out += LineFeed(indent);
  69. out += LineFeed(indent);
  70. out += GenResponseContext(indent);
  71. // gen onDispatch
  72. out += LineFeed(indent);
  73. out += LineFeed(indent);
  74. out += "virtual int onDispatch(tars::ReqMessagePtr msg)";
  75. out += LineFeed(indent);
  76. out += "{";
  77. out += LineFeed(++indent);
  78. out += "static ::std::string __all[] = ";
  79. out += "{";
  80. out += LineFeed(++indent);
  81. for(auto it = m_method.begin(); it != m_method.end(); ++it)
  82. {
  83. auto method = it->second;
  84. out += "\"" + method->name() + "\",";
  85. out += LineFeed(indent);
  86. }
  87. out += LineFeed(--indent);
  88. out += "};" + LineFeed(indent);
  89. out += "pair<string*, string*> r = equal_range(__all, __all + " + std::to_string((long long)desc->method_count()) + ", " + "std::string(msg->request.sFuncName));";
  90. out += LineFeed(indent);
  91. out += "if(r.first == r.second) return tars::TARSSERVERNOFUNCERR;" + LineFeed(indent);
  92. out += "switch(r.first - __all)" + LineFeed(indent);
  93. out += "{";
  94. out += LineFeed(++indent);
  95. int i = 0;
  96. for(auto it = m_method.begin(); it != m_method.end(); ++it)
  97. {
  98. auto method = it->second;
  99. out += LineFeed(indent);
  100. out += "case " + std::to_string((long long)i) + ":" + LineFeed(indent);
  101. out += "{" + LineFeed(++indent);
  102. out += "if (msg->response->iRet != tars::TARSSERVERSUCCESS)" + LineFeed(indent);
  103. out += "{" + LineFeed(++indent);
  104. out += "callback_" + method->name() + "_exception(msg->response->iRet);" + LineFeed(indent);
  105. out += "return msg->response->iRet;" + LineFeed(--indent) + "}";
  106. out += LineFeed(indent);
  107. out += ToCppNamespace(method->output_type()->full_name()) + " _ret;" + LineFeed(indent);
  108. out += "_ret.ParseFromArray(msg->response->sBuffer.data(), msg->response->sBuffer.size());" + LineFeed(indent);
  109. out += "CallbackThreadData * pCbtd = CallbackThreadData::getData();" + LineFeed(indent);
  110. out += "assert(pCbtd != NULL);" + LineFeed(indent);
  111. out += LineFeed(indent);
  112. out += "pCbtd->setResponseContext(msg->response->context);" + LineFeed(indent);
  113. out += "callback_" + method->name() + "(_ret);" + LineFeed(indent);
  114. out += "pCbtd->delResponseContext();" + LineFeed(indent);
  115. out += LineFeed(indent);
  116. out += "return tars::TARSSERVERSUCCESS;";
  117. out += LineFeed(--indent);
  118. out += "}";
  119. ++i;
  120. }
  121. // end switch
  122. out += LineFeed(--indent);
  123. out += "}";
  124. out += LineFeed(indent);
  125. out += LineFeed(indent);
  126. out += "return tars::TARSSERVERNOFUNCERR;";
  127. out += LineFeed(--indent);
  128. out += "}"; // end of onDispatch
  129. out += LineFeed(--indent);
  130. out += "};"; // end of class CallbackPrx
  131. out += LineFeed(indent);
  132. out += "typedef tars::TC_AutoPtr<" + name + "PrxCallback> " + name + "PrxCallbackPtr;";
  133. out += LineFeed(indent);
  134. return out;
  135. }