diff options
author | Shauren <shauren.trinity@gmail.com> | 2023-01-13 18:25:52 +0100 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2023-01-13 18:25:52 +0100 |
commit | 68d079e4a32ae525b236f956e8fc88c35eb8e24b (patch) | |
tree | 9226a44caa0c6bb0d82f6d74d88b249eeddeb819 /contrib/protoc-bnet/BnetServiceGenerator.cpp | |
parent | 3f1816d4076a9dfc65adae3109bf504e35f21003 (diff) |
Contrib: Adjust protobuf generator for latest include cleaned up output
Diffstat (limited to 'contrib/protoc-bnet/BnetServiceGenerator.cpp')
-rw-r--r-- | contrib/protoc-bnet/BnetServiceGenerator.cpp | 160 |
1 files changed, 97 insertions, 63 deletions
diff --git a/contrib/protoc-bnet/BnetServiceGenerator.cpp b/contrib/protoc-bnet/BnetServiceGenerator.cpp index 149eee8fd7c..37604a2f3a6 100644 --- a/contrib/protoc-bnet/BnetServiceGenerator.cpp +++ b/contrib/protoc-bnet/BnetServiceGenerator.cpp @@ -42,6 +42,10 @@ void BnetServiceGenerator::GenerateInterface(pb::io::Printer* printer) " public:\n" "\n" " explicit $classname$(bool use_original_hash);\n" + " $classname$($classname$ const&) = delete;\n" + " $classname$($classname$&&) = delete;\n" + " $classname$& operator=($classname$ const&) = delete;\n" + " $classname$& operator=($classname$&&) = delete;\n" " virtual ~$classname$();\n" "\n" "$original_hash$" @@ -79,16 +83,21 @@ void BnetServiceGenerator::GenerateInterface(pb::io::Printer* printer) printer->Print("// server methods --------------------------------------------------\n"); GenerateServerMethodSignatures(printer); + + printer->Outdent(); + + printer->Print( + "\n" + " private:\n"); + + printer->Indent(); + + GenerateServerMethodParserSignatures(printer); } printer->Outdent(); printer->Print(vars_, - "\n" - " private:\n" - " uint32 service_hash_;\n" - "\n" - " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$);\n" "};\n"); } @@ -135,6 +144,23 @@ void BnetServiceGenerator::GenerateServerMethodSignatures(pb::io::Printer* print } } +void BnetServiceGenerator::GenerateServerMethodParserSignatures(pb::io::Printer* printer) +{ + for (int i = 0; i < descriptor_->method_count(); i++) + { + pb::MethodDescriptor const* method = descriptor_->method(i); + if (!method->options().HasExtension(Battlenet::method_options)) + continue; + + std::map<std::string, std::string> sub_vars; + sub_vars["name"] = method->name(); + sub_vars["input_type"] = pbcpp::ClassName(method->input_type(), true); + sub_vars["output_type"] = pbcpp::ClassName(method->output_type(), true); + + printer->Print(sub_vars, "void ParseAndHandle$name$(uint32 token, uint32 methodId, MessageBuffer& buffer);\n"); + } +} + // =================================================================== void BnetServiceGenerator::GenerateDescriptorInitializer(pb::io::Printer* printer, int index) @@ -151,7 +177,7 @@ void BnetServiceGenerator::GenerateDescriptorInitializer(pb::io::Printer* printe void BnetServiceGenerator::GenerateImplementation(pb::io::Printer* printer) { printer->Print(vars_, - "$classname$::$classname$(bool use_original_hash) : service_hash_(use_original_hash ? OriginalHash::value : NameHash::value) {\n" + "$classname$::$classname$(bool use_original_hash) : ServiceBase(use_original_hash ? OriginalHash::value : NameHash::value) {\n" "}\n" "\n" "$classname$::~$classname$() {\n" @@ -170,13 +196,13 @@ void BnetServiceGenerator::GenerateImplementation(pb::io::Printer* printer) { GenerateServerCallMethod(printer); GenerateServerImplementations(printer); + GenerateServerMethodParserImplementations(printer); } else { printer->Print(vars_, - "void $classname$::CallServerMethod(uint32 token, uint32 methodId, MessageBuffer /*buffer*/) {\n" - " TC_LOG_ERROR(\"service.protobuf\", \"%s Server tried to call server method %u\",\n" - " GetCallerInfo().c_str(), methodId);\n" + "void $classname$::CallServerMethod(uint32 /*token*/, uint32 methodId, MessageBuffer /*buffer*/) {\n" + " LogDisallowedMethod(methodId);\n" "}\n" "\n"); } @@ -203,8 +229,7 @@ void BnetServiceGenerator::GenerateClientMethodImplementations(pb::io::Printer* { printer->Print(sub_vars, "void $classname$::$name$($input_type$ const* request, std::function<void($output_type$ const*)> responseCallback, bool client /*= false*/, bool server /*= false*/) {\n" - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Server called client method $full_name$($input_type_name${ %s })\",\n" - " GetCallerInfo().c_str(), request->ShortDebugString().c_str());\n" + " LogCallClientMethod(\"$full_name$\", \"$input_type_name$\", request);\n" " std::function<void(MessageBuffer)> callback = [responseCallback](MessageBuffer buffer) -> void {\n" " $output_type$ response;\n" " if (response.ParseFromArray(buffer.GetReadPointer(), buffer.GetActiveSize()))\n" @@ -218,8 +243,7 @@ void BnetServiceGenerator::GenerateClientMethodImplementations(pb::io::Printer* { printer->Print(sub_vars, "void $classname$::$name$($input_type$ const* request, bool client /*= false*/, bool server /*= false*/) {\n" - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Server called client method $full_name$($input_type_name${ %s })\",\n" - " GetCallerInfo().c_str(), request->ShortDebugString().c_str());\n" + " LogCallClientMethod(\"$full_name$\", \"$input_type_name$\", request);\n" " SendRequest(service_hash_, $method_id$ | (client ? 0x40000000 : 0) | (server ? 0x80000000 : 0), request);\n" "}\n" "\n"); @@ -250,55 +274,14 @@ void BnetServiceGenerator::GenerateServerCallMethod(pb::io::Printer* printer) sub_vars["output_type_name"] = method->output_type()->full_name(); printer->Print(sub_vars, - " case $method_id$: {\n" - " $input_type$ request;\n" - " if (!request.ParseFromArray(buffer.GetReadPointer(), buffer.GetActiveSize())) {\n" - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Failed to parse request for $full_name$ server method call.\", GetCallerInfo().c_str());\n" - " SendResponse(service_hash_, methodId, token, ERROR_RPC_MALFORMED_REQUEST);\n" - " return;\n" - " }\n" - ); - - if (method->output_type()->name() != "NO_RESPONSE") - { - printer->Print(sub_vars, - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Client called server method $full_name$($input_type_name${ %s }).\",\n" - " GetCallerInfo().c_str(), request.ShortDebugString().c_str());\n" - " std::function<void(ServiceBase*, uint32, ::google::protobuf::Message const*)> continuation = [token, methodId](ServiceBase* service, uint32 status, ::google::protobuf::Message const* response)\n" - " {\n" - " ASSERT(response->GetDescriptor() == $output_type$::descriptor());\n" - " $classname$* self = static_cast<$classname$*>(service);\n" - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Client called server method $full_name$() returned $output_type_name${ %s } status %u.\",\n" - " self->GetCallerInfo().c_str(), response->ShortDebugString().c_str(), status);\n" - " if (!status)\n" - " self->SendResponse(self->service_hash_, methodId, token, response);\n" - " else\n" - " self->SendResponse(self->service_hash_, methodId, token, status);\n" - " };\n" - " $output_type$ response;\n" - " uint32 status = Handle$name$(&request, &response, continuation);\n" - " if (continuation)\n" - " continuation(this, status, &response);\n" - ); - } - else - { - printer->Print(sub_vars, - " uint32 status = Handle$name$(&request);\n" - " TC_LOG_DEBUG(\"service.protobuf\", \"%s Client called server method $full_name$($input_type_name${ %s }) status %u.\",\n" - " GetCallerInfo().c_str(), request.ShortDebugString().c_str(), status);\n" - " if (status)\n" - " SendResponse(service_hash_, methodId, token, status);\n"); - } - - printer->Print(sub_vars, - " break;\n" - " }\n"); + " case $method_id$:\n" + " ParseAndHandle$name$(token, methodId, buffer);\n" + " break;\n"); } printer->Print(vars_, " default:\n" - " TC_LOG_ERROR(\"service.protobuf\", \"Bad method id %u.\", methodId);\n" + " LogInvalidMethod(methodId);\n" " SendResponse(service_hash_, methodId, token, ERROR_RPC_INVALID_METHOD);\n" " break;\n" " }\n" @@ -323,9 +306,8 @@ void BnetServiceGenerator::GenerateServerImplementations(pb::io::Printer* printe if (method->output_type()->name() != "NO_RESPONSE") { - printer->Print(sub_vars, "uint32 $classname$::Handle$name$($input_type$ const* request, $output_type$* response, std::function<void(ServiceBase*, uint32, ::google::protobuf::Message const*)>& continuation) {\n" - " TC_LOG_ERROR(\"service.protobuf\", \"%s Client tried to call not implemented method $full_name$({ %s })\",\n" - " GetCallerInfo().c_str(), request->ShortDebugString().c_str());\n" + printer->Print(sub_vars, "uint32 $classname$::Handle$name$($input_type$ const* request, $output_type$* /*response*/, std::function<void(ServiceBase*, uint32, ::google::protobuf::Message const*)>& /*continuation*/) {\n" + " LogUnimplementedServerMethod(\"$full_name$\", request);\n" " return ERROR_RPC_NOT_IMPLEMENTED;\n" "}\n" "\n"); @@ -333,8 +315,7 @@ void BnetServiceGenerator::GenerateServerImplementations(pb::io::Printer* printe else { printer->Print(sub_vars, "uint32 $classname$::Handle$name$($input_type$ const* request) {\n" - " TC_LOG_ERROR(\"service.protobuf\", \"%s Client tried to call not implemented method $full_name$({ %s })\",\n" - " GetCallerInfo().c_str(), request->ShortDebugString().c_str());\n" + " LogUnimplementedServerMethod(\"$full_name$\", request);\n" " return ERROR_RPC_NOT_IMPLEMENTED;\n" "}\n" "\n"); @@ -342,6 +323,59 @@ void BnetServiceGenerator::GenerateServerImplementations(pb::io::Printer* printe } } +void BnetServiceGenerator::GenerateServerMethodParserImplementations(pb::io::Printer* printer) +{ + for (int i = 0; i < descriptor_->method_count(); i++) + { + pb::MethodDescriptor const* method = descriptor_->method(i); + if (!method->options().HasExtension(Battlenet::method_options)) + continue; + + std::map<std::string, std::string> sub_vars; + sub_vars["classname"] = vars_["classname"]; + sub_vars["name"] = method->name(); + sub_vars["full_name"] = descriptor_->name() + "." + method->name(); + sub_vars["input_type"] = pbcpp::ClassName(method->input_type(), true); + sub_vars["output_type"] = pbcpp::ClassName(method->output_type(), true); + sub_vars["input_type_name"] = method->input_type()->full_name(); + sub_vars["output_type_name"] = method->output_type()->full_name(); + + printer->Print(sub_vars, + "void $classname$::ParseAndHandle$name$(uint32 token, uint32 methodId, MessageBuffer& buffer) {\n" + " $input_type$ request;\n" + " if (!request.ParseFromArray(buffer.GetReadPointer(), buffer.GetActiveSize())) {\n" + " LogFailedParsingRequest(\"$full_name$\");\n" + " SendResponse(service_hash_, methodId, token, ERROR_RPC_MALFORMED_REQUEST);\n" + " return;\n" + " }\n" + ); + + if (method->output_type()->name() != "NO_RESPONSE") + { + printer->Print(sub_vars, + " LogCallServerMethod(\"$full_name$\", \"$input_type_name$\", &request);\n" + " std::function<void(ServiceBase*, uint32, ::google::protobuf::Message const*)> continuation = CreateServerContinuation(token, methodId, \"$full_name$\", $output_type$::descriptor());\n" + " $output_type$ response;\n" + " uint32 status = Handle$name$(&request, &response, continuation);\n" + " if (continuation)\n" + " continuation(this, status, &response);\n" + ); + } + else + { + printer->Print(sub_vars, + " uint32 status = Handle$name$(&request);\n" + " LogCallServerMethod(\"$full_name$\", \"$input_type_name$\", &request);\n" + " if (status)\n" + " SendResponse(service_hash_, methodId, token, status);\n"); + } + + printer->Print(sub_vars, + "}\n" + "\n"); + } +} + std::uint32_t BnetServiceGenerator::HashServiceName(std::string const& name) { std::uint32_t hash = 0x811C9DC5; |