问题
First off, I'll say I'm not the most competent C++ programmer, but I'm learning, and enjoying the power of Thrift.
I've implemented a Thrift Service with some basic functions that return void, i32, and list. I'm using a Python client controlled by a Django web app to make RPC calls and it works pretty well. The generated code is pretty straight forward, except for list returns:
.thrift description file:
namespace cpp Remote
enum N_PROTO {
N_TCP,
N_UDP,
N_ANY
}
service Rcon {
i32 ping()
i32 KillFlows()
i32 RestartDispatch()
i32 PrintActiveFlows()
i32 PrintActiveListeners(1:i32 proto)
list<string> ListAllFlows()
}
The generated signatures from Rcon.h:
int32_t ping();
int32_t KillFlows();
int32_t RestartDispatch();
int32_t PrintActiveFlows();
int32_t PrintActiveListeners(const int32_t proto);
int64_t ListenerBytesReceived(const int32_t id);
void ListAllFlows(std::vector<std::string> & _return);
As you see, the ListAllFlows() function generated takes a reference to a vector of strings. I guess I expect it to return a vector of strings as laid out in the .thrift description.
I'm wondering if I am meant to provide the function a vector of strings to modify and then Thrift will handle returning it to my client despite the function returning void.
I can find absolutely no resources or example usages of Thrift list<> types in C++. Any guidance would be appreciated.
回答1:
Ok, I've figured it out. It's pretty simple really.
void ListAllFlows(std::vector<std::string> & _return)
{
for(int x = 0; x < 5; x++)
{
_return.push_back(std::string("hi"));
}
}
Then the Python client just calls it as it looks in the .thrift file:
result = client.ListAllFlows()
print result # ['hi', 'hi', 'hi', 'hi', 'hi']
回答2:
Returning a container class directly works, but has a few drawbacks which I outlined in more detail over here.
来源:https://stackoverflow.com/questions/12828772/handling-apache-thrift-list-map-return-types-in-c