Skip to content

Commit

Permalink
Merge pull request #53 from holmes1412/trpc
Browse files Browse the repository at this point in the history
add trpc protocol
  • Loading branch information
Barenboim committed Mar 23, 2021
2 parents 7c83d15 + 87f9723 commit 857f46a
Show file tree
Hide file tree
Showing 12 changed files with 1,230 additions and 10 deletions.
1 change: 1 addition & 0 deletions CMakeLists_Headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(INCLUDE_HEADERS
src/message/rpc_message_srpc.h
src/message/rpc_message_thrift.h
src/message/rpc_message_brpc.h
src/message/rpc_message_trpc.h
src/thrift/rpc_thrift_buffer.h
src/thrift/rpc_thrift_enum.h
src/thrift/rpc_thrift_idl.h
Expand Down
4 changes: 3 additions & 1 deletion src/generator/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ void Generator::generate_srpc_file(const idl_info& cur_info)
rpc_list.push_back("SRPC");
rpc_list.push_back("SRPCHttp");
rpc_list.push_back("BRPC");
rpc_list.push_back("TRPC");
}

for (const auto& desc : cur_info.desc_list)
Expand Down Expand Up @@ -316,7 +317,8 @@ void Generator::generate_srpc_file(const idl_info& cur_info)

for (const auto& type : rpc_list)
{
this->printer.print_client_constructor(type, desc.block_name);
this->printer.print_client_constructor(type, desc.block_name,
cur_info.package_name);
this->printer.print_client_methods(type, desc.block_name, desc.rpcs);
this->printer.print_client_create_task(type, desc.block_name, desc.rpcs);
}
Expand Down
58 changes: 50 additions & 8 deletions src/generator/printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@ static inline std::string make_package_prefix(const std::vector<std::string>& pa
return "::" + package_str + "::" + param;
}

static inline std::string make_trpc_service_prefix(const std::vector<std::string>& package,
const std::string& service)
{
if (package.size() == 0)
return service;

std::string package_prefix = package[0] + ".";

for (size_t i = 1; i < package.size(); i++)
package_prefix = package_prefix + package[i] + ".";

return package_prefix + service;
}

static inline std::string make_trpc_method_prefix(const std::vector<std::string>& package,
const std::string& service,
const std::string& method)
{
std::string method_prefix = "/";

for (size_t i = 0; i < package.size(); i++)
method_prefix = method_prefix + package[i] + ".";

return method_prefix + service + "/" + method;
}

static inline bool is_simple_type(int8_t data_type)
{
return data_type == srpc::TDT_BOOL
Expand Down Expand Up @@ -635,42 +661,53 @@ class Printer
fprintf(this->out_file, "}");
}

void print_client_constructor(const std::string& type, const std::string& service)
void print_client_constructor(const std::string& type, const std::string& service,
const std::vector<std::string>& package)
{
bool is_srpc_thrift = (this->is_thrift && (type == "SRPC" || type == "SRPCHttp"));
const char *method_ip = is_srpc_thrift ? client_constructor_methods_ip_srpc_thrift_format.c_str() : "";
const char *method_params = is_srpc_thrift ? client_constructor_methods_params_srpc_thrift_format.c_str() : "";

std::string full_service = service;

if (type == "TRPC")
full_service = make_trpc_service_prefix(package, service);

fprintf(this->out_file, this->client_constructor_methods_format.c_str(),
type.c_str(), type.c_str(),
type.c_str(), service.c_str(),
type.c_str(), full_service.c_str(),
method_ip, type.c_str(),

type.c_str(), type.c_str(),
type.c_str(), service.c_str(),
type.c_str(), full_service.c_str(),
method_params, type.c_str());
}

void print_client_methods(const std::string& type,
const std::string& service,
const std::vector<rpc_descriptor>& rpcs)
const std::vector<rpc_descriptor>& rpcs,
const std::vector<std::string>& package)
{
for (const auto& rpc : rpcs)
{
std::string req = change_include_prefix(rpc.request_name);
std::string resp = change_include_prefix(rpc.response_name);

std::string full_method = rpc.method_name;
if (type == "TRPC")
full_method = make_trpc_method_prefix(package, service, rpc.method_name);

fprintf(this->out_file, this->client_method_format.c_str(),
type.c_str(), rpc.method_name.c_str(),
req.c_str(), rpc.method_name.c_str(),
rpc.method_name.c_str(),
full_method.c_str(),

type.c_str(), rpc.method_name.c_str(),
req.c_str(), resp.c_str(), rpc.method_name.c_str(),

resp.c_str(), type.c_str(),
rpc.method_name.c_str(), req.c_str(),
resp.c_str(), resp.c_str(), rpc.method_name.c_str(), resp.c_str());
resp.c_str(), resp.c_str(), full_method.c_str(), resp.c_str());
}

if (this->is_thrift)
Expand Down Expand Up @@ -740,13 +777,18 @@ class Printer
}

void print_client_create_task(const std::string& type, const std::string& service,
const std::vector<rpc_descriptor>& rpcs)
const std::vector<rpc_descriptor>& rpcs,
const std::vector<std::string>& package)
{
for (const auto& rpc : rpcs)
{
std::string full_method = rpc.method_name;
if (type == "TRPC")
full_method = make_trpc_method_prefix(package, service, rpc.method_name);

fprintf(this->out_file, this->client_create_task_format.c_str(),
type.c_str(), type.c_str(), rpc.method_name.c_str(),
rpc.method_name.c_str(), rpc.method_name.c_str());
rpc.method_name.c_str(), full_method.c_str());
/*
type.c_str(), service.c_str(),
type.c_str(), rpc.method_name.c_str(),
Expand Down
3 changes: 2 additions & 1 deletion src/message/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ project(message)

include_directories(${CMAKE_CURRENT_BINARY_DIR})

set(PROTO_LIST rpc_meta.proto rpc_meta_brpc.proto rpc_span.proto)
set(PROTO_LIST rpc_meta.proto rpc_meta_brpc.proto rpc_span.proto rpc_meta_trpc.proto)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_LIST})

set(SRC
rpc_message_brpc.cc
rpc_message_srpc.cc
rpc_message_thrift.cc
rpc_message_trpc.cc
${PROTO_SRCS} ${PROTO_HDRS}
)

Expand Down
Loading

0 comments on commit 857f46a

Please sign in to comment.