123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- // Generates C++ tars service interface out of Protobuf IDL.
- //
- // This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto
- // and net/proto2/compiler/public/plugin.h for more information on plugins.
- #include <string>
- #include <google/protobuf/compiler/code_generator.h>
- #include <google/protobuf/compiler/plugin.h>
- #include <google/protobuf/compiler/plugin.pb.h>
- #include <google/protobuf/descriptor.h>
- #include "CppGenCallback.h"
- static std::string GenSyncCall(const ::google::protobuf::MethodDescriptor* method, const std::string& pkg, int indent) {
- std::string out;
- out.reserve(8 * 1024);
- out += ToCppNamespace(method->output_type()->full_name()) + " " + method->name() + "(const " +
- ToCppNamespace(method->input_type()->full_name()) + "& req, const std::map<std::string, std::string>& context = TARS_CONTEXT(), " +
- "std::map<std::string, std::string>* pResponseContext = NULL)";
- out += LineFeed(indent);
- out += "{" + LineFeed(++indent);
- out += "std::string _os;" + LineFeed(indent) +
- "req.SerializeToString(&_os);" + LineFeed(indent) +
- "std::vector<char> _vc(_os.begin(), _os.end());" + LineFeed(indent);
- out += LineFeed(indent);
- out += "std::map<std::string, std::string> _mStatus;" + LineFeed(indent);
- out += "shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL, \"" + method->name() + "\", _vc, context, _mStatus);" + LineFeed(indent);
- out += "if (pResponseContext)" + LineFeed(++indent);
- out += "*pResponseContext = rep->context;" + LineFeed(--indent);
- out += LineFeed(indent);
- out += ToCppNamespace(method->output_type()->full_name()) + " _ret;" + LineFeed(indent);
- out += "_ret.ParseFromArray(rep->sBuffer.data(), rep->sBuffer.size());" + LineFeed(indent) +
- "return _ret;";
- out += LineFeed(--indent) + "}";
- out += LineFeed(indent);
- out += LineFeed(indent);
- return out;
- }
- static std::string GenAsyncCall(const ::google::protobuf::MethodDescriptor* method,
- const std::string& name,
- const std::string& pkg,
- int indent) {
- std::string out;
- out.reserve(8 * 1024);
- out += "void async_" + method->name() + "(" + name + "PrxCallbackPtr callback, const " +
- ToCppNamespace(method->input_type()->full_name()) + "& req, const std::map<std::string, std::string>& context = TARS_CONTEXT())" + LineFeed(indent);
- out += "{" + LineFeed(++indent);
- out += "std::string _os;" + LineFeed(indent) +
- "req.SerializeToString(&_os);" + LineFeed(indent) +
- "std::vector<char> _vc(_os.begin(), _os.end());" + LineFeed(indent);
- out += "std::map<std::string, std::string> _mStatus;" + LineFeed(indent);
- out += "tars_invoke_async(tars::TARSNORMAL, \"" + method->name() + "\", _vc, context, _mStatus, callback);";
- out += LineFeed(--indent) + "}";
- out += LineFeed(indent);
- return out;
- }
- std::string GenPrx(const ::google::protobuf::ServiceDescriptor* desc, int indent) {
- std::string out;
- out.reserve(8 * 1024);
- const auto& name = desc->name();
- const auto& pkg = desc->file()->package();
- const auto& proxy = name + "Proxy";
- out += LineFeed(indent);
- out += "/* proxy for client */";
- out += LineFeed(indent);
- out += "class " + proxy + " : public tars::ServantProxy";
- out += LineFeed(indent);
- out += "{";
- out += LineFeed(indent);
- out += "public:";
- out += LineFeed(++indent);
- out += "typedef std::map<std::string, std::string> TARS_CONTEXT;" + LineFeed(indent);
- //sort by method name
- std::map<std::string, const ::google::protobuf::MethodDescriptor*> m_method;
- for (int i = 0; i < desc->method_count(); ++i)
- {
- m_method[desc->method(i)->name()] = desc->method(i);
- }
- // gen methods
- for(auto it = m_method.begin(); it != m_method.end(); ++it)
- {
- auto method = it->second;
- out += LineFeed(indent);
- // sync method call
- out += GenSyncCall(method, pkg, indent);
- // async method call
- out += GenAsyncCall(method, name, pkg, indent);
- }
- // hash call
- out += LineFeed(indent);
- out += proxy + "* tars_hash(int64_t key)";
- out += "{" + LineFeed(++indent);
- out += "return (" + proxy + "*)ServantProxy::tars_hash(key);";
- out += LineFeed(--indent) + "}";
- out += LineFeed(indent);
- // consist hash
- out += LineFeed(indent);
- out += proxy + "* tars_consistent_hash(int64_t key)";
- out += "{" + LineFeed(++indent);
- out += "return (" + proxy + "*)ServantProxy::tars_consistent_hash(key);";
- out += LineFeed(--indent) + "}";
- out += LineFeed(indent);
- // set_timeout
- out += LineFeed(indent);
- out += proxy + "* tars_set_timeout(int msecond)";
- out += "{" + LineFeed(++indent);
- out += "return (" + proxy + "*)ServantProxy::tars_set_timeout(msecond);";
- out += LineFeed(--indent) + "}";
- out += LineFeed(--indent) + "}; // end class " + proxy;
- out += LineFeed(indent); // end class
- out += LineFeed(indent);
- out += "typedef tars::TC_AutoPtr<" + proxy + "> " + name + "Prx;";
- out += LineFeed(indent);
- return out;
- }
|