Skip to content

Commit

Permalink
THRIFT-5442 Separate client service calls into send/recv methods and …
Browse files Browse the repository at this point in the history
…make them public

Client: netstd
Patch: Jens Geyer
  • Loading branch information
Jens-G committed Jul 23, 2021
1 parent fb1d50d commit 8a135fd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
64 changes: 44 additions & 20 deletions compiler/cpp/src/thrift/generate/t_netstd_generator.cc
Expand Up @@ -2014,9 +2014,10 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi
<< indent() << "{" << endl
<< indent() << "}" << endl
<< endl
<< indent() << "public Client(TProtocol inputProtocol, TProtocol outputProtocol) : base(inputProtocol, outputProtocol)"
<< indent() << "public Client(TProtocol inputProtocol, TProtocol outputProtocol) : base(inputProtocol, outputProtocol)" << endl
<< indent() << "{" << endl
<< indent() << "}" << endl;
<< indent() << "}" << endl
<< endl;

vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::const_iterator functions_iterator;
Expand All @@ -2030,6 +2031,23 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi
out << indent() << "public async " << function_signature_async(*functions_iterator, "") << endl
<< indent() << "{" << endl;
indent_up();
out << indent() << "await send_" << function_name << "(";
string call_args = argument_list((*functions_iterator)->get_arglist(),false);
if(! call_args.empty()) {
out << call_args << ", ";
}
out << "cancellationToken);" << endl;
if(! (*functions_iterator)->is_oneway()) {
out << indent() << ((*functions_iterator)->get_returntype()->is_void() ? "" : "return ")
<< "await recv_" << function_name << "(cancellationToken);" << endl;
}
indent_down();
out << indent() << "}" << endl << endl;

// async send
out << indent() << "public async " << function_signature_async(*functions_iterator, "send_", MODE_NO_RETURN) << endl
<< indent() << "{" << endl;
indent_up();

string tmpvar = tmp("tmp");
string argsname = (*functions_iterator)->get_name() + "Args";
Expand Down Expand Up @@ -2061,8 +2079,16 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi
<< indent() << "await OutputProtocol.WriteMessageEndAsync(cancellationToken);" << endl
<< indent() << "await OutputProtocol.Transport.FlushAsync(cancellationToken);" << endl;

indent_down();
out << indent() << "}" << endl << endl;

if (!(*functions_iterator)->is_oneway())
{
// async recv
out << indent() << "public async " << function_signature_async(*functions_iterator, "recv_", MODE_NO_ARGS) << endl
<< indent() << "{" << endl;
indent_up();

string resultname = (*functions_iterator)->get_name() + "Result";
t_struct noargs(program_);
t_struct* xs = (*functions_iterator)->get_xceptions();
Expand Down Expand Up @@ -2111,11 +2137,7 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi
out << indent() << "}" << endl;
}

if ((*functions_iterator)->get_returntype()->is_void())
{
out << indent() << "return;" << endl;
}
else
if (!(*functions_iterator)->get_returntype()->is_void())
{
out << indent() << "throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, \""
<< function_name << " failed: unknown result\");" << endl;
Expand All @@ -2125,11 +2147,6 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi
indent_down();
out << indent() << "}" << endl << endl;
}
else
{
indent_down();
out << indent() << "}" << endl;
}

cleanup_member_name_mapping(arg_struct);
}
Expand Down Expand Up @@ -3384,28 +3401,30 @@ string t_netstd_generator::function_signature(t_function* tfunction, string pref
return type_name(ttype) + " " + func_name(normalize_name(prefix + tfunction->get_name())) + "(" + argument_list(tfunction->get_arglist()) + ")";
}

string t_netstd_generator::function_signature_async(t_function* tfunction, string prefix)
string t_netstd_generator::function_signature_async(t_function* tfunction, string prefix, int mode)
{
t_type* ttype = tfunction->get_returntype();
string task = "global::System.Threading.Tasks.Task";
if (!ttype->is_void())
if ((!ttype->is_void()) && ((mode & MODE_NO_RETURN) == 0))
{
task += "<" + type_name(ttype) + ">";
}

string result = task + " " + func_name(normalize_name(prefix + tfunction->get_name()) + (add_async_postfix ? "Async" : "")) + "(";
string args = argument_list(tfunction->get_arglist());
result += args;
if (!args.empty())
{
result += ", ";
if((mode & MODE_NO_ARGS) == 0) {
result += args;
if (!args.empty())
{
result += ", ";
}
}
result += "CancellationToken cancellationToken = default)";

return result;
}

string t_netstd_generator::argument_list(t_struct* tstruct)
string t_netstd_generator::argument_list(t_struct* tstruct, bool with_types)
{
string result = "";
const vector<t_field*>& fields = tstruct->get_members();
Expand All @@ -3421,7 +3440,12 @@ string t_netstd_generator::argument_list(t_struct* tstruct)
{
result += ", ";
}
result += type_name((*f_iter)->get_type()) + " " + normalize_name((*f_iter)->get_name());

if( with_types) {
result += type_name((*f_iter)->get_type()) + " ";
}

result += normalize_name((*f_iter)->get_name());
}
return result;
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/cpp/src/thrift/generate/t_netstd_generator.h
Expand Up @@ -131,12 +131,16 @@ class t_netstd_generator : public t_oop_generator
string netstd_type_usings() const;
string netstd_thrift_usings() const;

static const int MODE_FULL_DECL = 0x00;
static const int MODE_NO_RETURN = 0x01;
static const int MODE_NO_ARGS = 0x02;

string type_name(t_type* ttype);
string base_type_name(t_base_type* tbase);
string declare_field(t_field* tfield, bool init = false, string prefix = "");
string function_signature_async(t_function* tfunction, string prefix = "");
string function_signature_async(t_function* tfunction, string prefix = "", int mode = MODE_FULL_DECL);
string function_signature(t_function* tfunction, string prefix = "");
string argument_list(t_struct* tstruct);
string argument_list(t_struct* tstruct, bool with_types = true);
string type_to_enum(t_type* ttype);
string prop_name(t_field* tfield, bool suppress_mapping = false);
string func_name(t_function* tfunc, bool suppress_mapping = false);
Expand Down

0 comments on commit 8a135fd

Please sign in to comment.