【问题标题】:Handling Apache Thrift list/map Return Types in C++在 C++ 中处理 Apache Thrift 列表/映射返回类型
【发布时间】:2012-10-01 11:32:25
【问题描述】:

首先,我会说我不是最有能力的 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 类型的资源或示例用法。任何指导将不胜感激。

【问题讨论】:

    标签: c++ list rpc thrift


    【解决方案1】:

    直接返回一个容器类是可行的,但有一些缺点,我更详细地概述了over here

    【讨论】:

      【解决方案2】:

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

      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']
      

      【讨论】:

        猜你喜欢
        • 2019-11-20
        • 1970-01-01
        • 2018-09-15
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        • 2018-12-06
        • 2014-09-13
        • 1970-01-01
        相关资源
        最近更新 更多