2

首先,我会说我不是最称职的 C++ 程序员,但我正在学习,并享受 Thrift 的强大功能。

我已经实现了一个 Thrift 服务,其中包含一些返回 void、i32 和 list 的基本函数。我正在使用由 Django Web 应用程序控制的 Python 客户端进行 RPC 调用,它运行良好。生成的代码非常简单,除了列表返回:

.thrift 描述文件:

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()
}

从 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);

如您所见,生成的 ListAllFlows() 函数采用对字符串向量的引用。我想我希望它返回 .thrift 描述中列出的字符串向量。

我想知道我是否打算为函数提供一个要修改的字符串向量,然后尽管函数返回 void,但 Thrift 会处理将其返回给我的客户端。

我在 C++ 中绝对找不到 Thrift list<> 类型的资源或示例用法。任何指导将不胜感激。

4

2 回答 2

3

好的,我已经想通了。这真的很简单。

void ListAllFlows(std::vector<std::string> & _return)
{
    for(int x = 0; x < 5; x++)
    {
        _return.push_back(std::string("hi"));
    }
}

然后 Python 客户端就按照 .thrift 文件中的样子调用它:

result = client.ListAllFlows()
print result # ['hi', 'hi', 'hi', 'hi', 'hi']
于 2012-10-11T17:21:04.797 回答
0

直接返回容器类是可行的,但有一些缺点,我在此处更详细地概述了这些缺点。

于 2014-03-27T18:12:26.843 回答