CppGenServant.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "CppGenServant.h"
  10. static std::string GenMethods(const ::google::protobuf::MethodDescriptor* method,
  11. const std::string& pkg,
  12. int indent) {
  13. std::string out;
  14. out.reserve(8 * 1024);
  15. out += "virtual " + ToCppNamespace(method->output_type()->full_name()) + " " + method->name() +
  16. "(const " + ToCppNamespace(method->input_type()->full_name()) + "& , tars::TarsCurrentPtr current) = 0;" + LineFeed(indent);
  17. out += "static void async_response_" + method->name() + "(tars::TarsCurrentPtr current, const " + ToCppNamespace(method->output_type()->full_name()) + "&_ret)" + LineFeed(indent);
  18. out += "{" + LineFeed(++indent);
  19. out += "std::string _os;" + LineFeed(indent) +
  20. " _ret.SerializeToString(&_os);" + LineFeed(indent) +
  21. "vector<char> _vc(_os.begin(), _os.end());" + LineFeed(indent) +
  22. "current->sendResponse(tars::TARSSERVERSUCCESS, _vc);";
  23. out += LineFeed(--indent) + "}";
  24. out += LineFeed(indent);
  25. out += LineFeed(indent);
  26. return out;
  27. }
  28. static std::string GenDispatchCase(const ::google::protobuf::MethodDescriptor* method,
  29. const std::string& pkg,
  30. int indent) {
  31. std::string out;
  32. out.reserve(8 * 1024);
  33. out += "{";
  34. out += LineFeed(++indent);
  35. out += "tars::TarsInputStream<tars::BufferReader> _is;" + LineFeed(indent) +
  36. "_is.setBuffer(_current->getRequestBuffer());" + LineFeed(indent);
  37. out += LineFeed(indent);
  38. out += ToCppNamespace(method->input_type()->full_name()) + " req;" + LineFeed(indent);
  39. out += "req.ParseFromArray(&_current->getRequestBuffer()[0], _current->getRequestBuffer().size());" + LineFeed(indent);
  40. out += LineFeed(indent);
  41. out += ToCppNamespace(method->output_type()->full_name()) + " _ret = " + method->name() + "(req, _current);" + LineFeed(indent);
  42. out += "if (_current->isResponse())" + LineFeed(indent);
  43. out += "{" + LineFeed(++indent);
  44. out += "std::string _os;" + LineFeed(indent);
  45. out += "_ret.SerializeToString(&_os);" + LineFeed(indent);
  46. out += "std::vector<char> _vc(_os.begin(), _os.end());" + LineFeed(indent);
  47. out += "_sResponseBuffer.assign(_os.begin(), _os.end());";
  48. out += LineFeed(--indent) + "}";
  49. out += LineFeed(indent);
  50. out += "return tars::TARSSERVERSUCCESS;";
  51. out += LineFeed(--indent);
  52. out += "}";
  53. return out;
  54. }
  55. std::string GenServant(const ::google::protobuf::ServiceDescriptor* desc, int indent) {
  56. std::string out;
  57. out.reserve(8 * 1024);
  58. const auto& name = desc->name();
  59. const auto& pkg = desc->file()->package();
  60. const auto& servant = name;
  61. out += LineFeed(indent);
  62. out += "/* servant for server */";
  63. out += LineFeed(indent);
  64. out += "class " + servant + " : public tars::Servant";
  65. out += LineFeed(indent);
  66. out += "{";
  67. out += LineFeed(indent);
  68. out += "public:";
  69. out += LineFeed(++indent);
  70. out += "virtual ~" + servant + "() {}";
  71. out += LineFeed(indent);
  72. //sort by method name
  73. std::map<std::string, const ::google::protobuf::MethodDescriptor*> m_method;
  74. for (int i = 0; i < desc->method_count(); ++i)
  75. {
  76. m_method[desc->method(i)->name()] = desc->method(i);
  77. }
  78. for(auto it = m_method.begin(); it != m_method.end(); ++it)
  79. {
  80. out += GenMethods(it->second, pkg, indent);
  81. }
  82. // gen onDispatch
  83. out += LineFeed(indent);
  84. out += "int onDispatch(tars::TarsCurrentPtr _current, std::vector<char>& _sResponseBuffer)";
  85. out += LineFeed(indent);
  86. out += "{";
  87. out += LineFeed(++indent);
  88. out += "static ::std::string __all[] = ";
  89. out += "{";
  90. out += LineFeed(++indent);
  91. for(auto it = m_method.begin(); it != m_method.end(); ++it)
  92. {
  93. auto method = it->second;
  94. out += "\"" + method->name() + "\",";
  95. out += LineFeed(indent);
  96. }
  97. out += LineFeed(--indent);
  98. out += "};";
  99. out += LineFeed(indent);
  100. out += "pair<string*, string*> r = equal_range(__all, __all + " + std::to_string((long long)desc->method_count()) + ", " + "_current->getFuncName());";
  101. out += LineFeed(indent);
  102. out += "if(r.first == r.second) return tars::TARSSERVERNOFUNCERR;";
  103. out += LineFeed(indent);
  104. out += "switch(r.first - __all)";
  105. out += LineFeed(indent);
  106. out += "{";
  107. out += LineFeed(++indent);
  108. int i = 0;
  109. for(auto it = m_method.begin(); it != m_method.end(); ++it)
  110. {
  111. auto method = it->second;
  112. out += LineFeed(indent);
  113. out += "case " + std::to_string((long long)i) + ":";
  114. out += LineFeed(indent);
  115. out += GenDispatchCase(method, pkg, indent);
  116. ++i;
  117. }
  118. // end switch
  119. out += LineFeed(--indent);
  120. out += "} // end switch";
  121. out += LineFeed(indent);
  122. out += "return tars::TARSSERVERNOFUNCERR;";
  123. out += LineFeed(--indent);
  124. out += "}"; // end of onDispatch
  125. out += LineFeed(indent);
  126. out += LineFeed(--indent) + "}; // end class " + servant;
  127. out += LineFeed(indent); // end class
  128. out += LineFeed(indent);
  129. return out;
  130. }