Skip to content

Commit

Permalink
Misc: Update protobuf code generator plugin for 4c85198
Browse files Browse the repository at this point in the history
  • Loading branch information
Shauren committed May 14, 2020
1 parent d3782c2 commit d3e1e4b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 35 deletions.
81 changes: 53 additions & 28 deletions contrib/protoc-bnet/BnetServiceGenerator.cpp
Expand Up @@ -22,10 +22,12 @@ BnetServiceGenerator::BnetServiceGenerator(pb::ServiceDescriptor const* descript

if (descriptor_->options().HasExtension(Battlenet::service_options))
vars_["original_hash"] = " typedef std::integral_constant<uint32, 0x" + pb::ToUpper(pb::ToHex(HashServiceName(descriptor_->options().GetExtension(Battlenet::service_options).descriptor_name()))) + "u> OriginalHash;\n";
else
vars_["original_hash"] = "";
vars_["name_hash"] = " typedef std::integral_constant<uint32, 0x" + pb::ToUpper(pb::ToHex(HashServiceName(descriptor_->full_name()))) + "u> NameHash;\n";
}

BnetServiceGenerator::~BnetServiceGenerator() { }
BnetServiceGenerator::~BnetServiceGenerator() = default;

void BnetServiceGenerator::GenerateDeclarations(pb::io::Printer* printer)
{
Expand All @@ -50,25 +52,34 @@ void BnetServiceGenerator::GenerateInterface(pb::io::Printer* printer)
printer->Print(vars_,
"\n"
"static google::protobuf::ServiceDescriptor const* descriptor();\n"
"\n"
"// client methods --------------------------------------------------\n"
"\n");

GenerateClientMethodSignatures(printer);
if (!descriptor_->options().HasExtension(Battlenet::sdk_service_options) || descriptor_->options().GetExtension(Battlenet::sdk_service_options).inbound())
{
printer->Print(vars_,
"// client methods --------------------------------------------------\n");

printer->Print(
"// server methods --------------------------------------------------\n"
"\n"
"void CallServerMethod(uint32 token, uint32 methodId, MessageBuffer buffer) override final;\n"
"\n");
GenerateClientMethodSignatures(printer);

printer->Outdent();
printer->Print(vars_, "\n");
}

printer->Print(" protected:\n ");
printer->Print("void CallServerMethod(uint32 token, uint32 methodId, MessageBuffer buffer) final;\n");

printer->Indent();
if (!descriptor_->options().HasExtension(Battlenet::sdk_service_options) || descriptor_->options().GetExtension(Battlenet::sdk_service_options).outbound())
{
printer->Outdent();

printer->Print(
"\n"
" protected:\n ");

GenerateServerMethodSignatures(printer);
printer->Indent();

printer->Print("// server methods --------------------------------------------------\n");

GenerateServerMethodSignatures(printer);
}

printer->Outdent();

Expand Down Expand Up @@ -98,9 +109,9 @@ void BnetServiceGenerator::GenerateClientMethodSignatures(pb::io::Printer* print
sub_vars["input_type_name"] = method->input_type()->full_name();

if (method->output_type()->name() != "NO_RESPONSE")
printer->Print(sub_vars, "void $name$($input_type$ const* request, std::function<void($output_type$ const*)> responseCallback);\n");
printer->Print(sub_vars, "void $name$($input_type$ const* request, std::function<void($output_type$ const*)> responseCallback, bool client = false, bool server = false);\n");
else
printer->Print(sub_vars, "void $name$($input_type$ const* request);\n");
printer->Print(sub_vars, "void $name$($input_type$ const* request, bool client = false, bool server = false);\n");
}
}

Expand Down Expand Up @@ -152,9 +163,23 @@ void BnetServiceGenerator::GenerateImplementation(pb::io::Printer* printer)
"}\n"
"\n");

GenerateClientMethodImplementations(printer);
GenerateServerCallMethod(printer);
GenerateServerImplementations(printer);
if (!descriptor_->options().HasExtension(Battlenet::sdk_service_options) || descriptor_->options().GetExtension(Battlenet::sdk_service_options).inbound())
GenerateClientMethodImplementations(printer);

if (!descriptor_->options().HasExtension(Battlenet::sdk_service_options) || descriptor_->options().GetExtension(Battlenet::sdk_service_options).outbound())
{
GenerateServerCallMethod(printer);
GenerateServerImplementations(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"
"}\n"
"\n");
}
}

void BnetServiceGenerator::GenerateClientMethodImplementations(pb::io::Printer* printer)
Expand All @@ -177,25 +202,25 @@ void BnetServiceGenerator::GenerateClientMethodImplementations(pb::io::Printer*
if (method->output_type()->name() != "NO_RESPONSE")
{
printer->Print(sub_vars,
"void $classname$::$name$($input_type$ const* request, std::function<void($output_type$ const*)> responseCallback) {\n"
"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"
" std::function<void(MessageBuffer)> callback = [responseCallback](MessageBuffer buffer) -> void {\n"
" $output_type$ response;\n"
" if (response.ParseFromArray(buffer.GetReadPointer(), buffer.GetActiveSize()))\n"
" responseCallback(&response);\n"
" };\n"
" SendRequest(service_hash_, $method_id$, request, std::move(callback));\n"
" SendRequest(service_hash_, $method_id$ | (client ? 0x40000000 : 0) | (server ? 0x80000000 : 0), request, std::move(callback));\n"
"}\n"
"\n");
}
else
{
printer->Print(sub_vars,
"void $classname$::$name$($input_type$ const* request) {\n"
"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"
" SendRequest(service_hash_, $method_id$, request);\n"
" SendRequest(service_hash_, $method_id$ | (client ? 0x40000000 : 0) | (server ? 0x80000000 : 0), request);\n"
"}\n"
"\n");
}
Expand All @@ -206,7 +231,7 @@ void BnetServiceGenerator::GenerateServerCallMethod(pb::io::Printer* printer)
{
printer->Print(vars_,
"void $classname$::CallServerMethod(uint32 token, uint32 methodId, MessageBuffer buffer) {\n"
" switch(methodId) {\n");
" switch(methodId & 0x3FFFFFFF) {\n");

for (int i = 0; i < descriptor_->method_count(); i++)
{
Expand All @@ -229,7 +254,7 @@ void BnetServiceGenerator::GenerateServerCallMethod(pb::io::Printer* printer)
" $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_, $method_id$, token, ERROR_RPC_MALFORMED_REQUEST);\n"
" SendResponse(service_hash_, methodId, token, ERROR_RPC_MALFORMED_REQUEST);\n"
" return;\n"
" }\n"
);
Expand All @@ -239,16 +264,16 @@ void BnetServiceGenerator::GenerateServerCallMethod(pb::io::Printer* printer)
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](ServiceBase* service, uint32 status, ::google::protobuf::Message const* response)\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_, $method_id$, token, response);\n"
" self->SendResponse(self->service_hash_, methodId, token, response);\n"
" else\n"
" self->SendResponse(self->service_hash_, $method_id$, token, status);\n"
" self->SendResponse(self->service_hash_, methodId, token, status);\n"
" };\n"
" $output_type$ response;\n"
" uint32 status = Handle$name$(&request, &response, continuation);\n"
Expand All @@ -263,7 +288,7 @@ void BnetServiceGenerator::GenerateServerCallMethod(pb::io::Printer* printer)
" 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_, $method_id$, token, status);\n");
" SendResponse(service_hash_, methodId, token, status);\n");
}

printer->Print(sub_vars,
Expand Down
25 changes: 18 additions & 7 deletions contrib/protoc-bnet/CMakeLists.txt
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.8)
project(protoc_bnet)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CXX_EXTENSIONS OFF)

find_package(Protobuf REQUIRED)

Expand All @@ -19,14 +19,25 @@ set(SOURCE_FILES
include_directories(${CMAKE_SOURCE_DIR} ${PROTOBUF_INCLUDE_DIRS})

add_executable(protoc-gen-bnet ${SOURCE_FILES})
target_compile_features(protoc-gen-bnet
PUBLIC
cxx_std_17
cxx_alias_templates
cxx_auto_type
cxx_constexpr
cxx_decltype
cxx_decltype_auto
cxx_final
cxx_lambdas
cxx_generic_lambdas
cxx_variadic_templates
cxx_defaulted_functions
cxx_nullptr
cxx_trailing_return_types
cxx_return_type_deduction)

target_link_libraries(protoc-gen-bnet ${PROTOBUF_PROTOC_LIBRARIES} ${PROTOBUF_LIBRARIES})

set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR})

add_custom_target(install_plugin
make install
DEPENDS protoc-gen-bnet
COMMENT "Installing protoc_bnet")

install(TARGETS protoc-gen-bnet DESTINATION bin)

0 comments on commit d3e1e4b

Please sign in to comment.