Apache Thrift 仅用于处理,而不是服务器

Apache Thrift for just processing, not server

我希望我没有误解 Thrift 的概念,但我从(示例)questions like this 中看到,这个框架由不同的模块化层组成,可以启用或禁用。

我最感兴趣的是 Thrift 的 "IDL part",这样我就可以在我的 C++ 代码和外部 Javascript 应用程序之间创建一个通用接口。我想使用 JS 调用 C++ 函数,使用二进制数据传输,我已经为此使用了编译器。

但是我的 C++(服务器)和 JS(客户端)应用程序已经使用支持 Websockets 的 C++ Web 服务器交换数据,Thrift 不提供它

所以我想设置以下项目:

目前,客户端已经能够向我的 websocket 服务器发送请求,我看到以二进制形式接收它们,我只需要 Thrift 即可:

  1. 解码输入
  2. 调用适当的 C++ 函数
  3. 编码输出

我的网络服务器会将响应发送给客户端。所以这里不需要"Thrift server"。我看到有 TProcessor->process() 函数,我在接收二进制数据时尝试使用它,但它需要一个 in/out TProtocol。这里没问题...但是为了创建 TBinaryProtocol,我还需要一个 TTransport!如果不需要 Thrift 服务器...我应该使用什么传输?

我试图在 TBinaryProtocol 构造函数中将 TTransport 设置为 NULL,但是一旦我使用它,它就会出现 nullptr 异常。

代码类似于:

初始化:

boost::shared_ptr<MySDKServiceHandler> handler(new MySDKServiceHandler());
thriftCommandProcessor = boost::shared_ptr<TProcessor>(new MySDKServiceProcessor(handler));

thriftInputProtocol = boost::shared_ptr<TBinaryProtocol>(new TBinaryProtocol(TTransport???));
thriftOutputProtocol = boost::shared_ptr<TBinaryProtocol>(new TBinaryProtocol(TTransport???));

数据到达时间:

this->thriftInputProtocol->writeBinary(input); // exception here
this->thriftCommandProcessor->process(this->thriftInputProtocol, this->thriftOutputProtocol, NULL);
this->thriftOutputProtocol->readBinary(output);

My webserver will send the response to the client. So no "Thrift server" is needed here. I see there is the TProcessor->process() function, I'm trying to use it when I receive the binary data but it needs an in/out TProtocol. No problem here... but in order to create the TBinaryProtocol I also need a TTransport! If no Thrift server is expected... what Transport should I use?

通常的模式是将位存储在某处并使用该缓冲区或数据流作为输入,输出也是如此。对于某些语言,有 TStreamTransport 可用,对于 C++,TBufferBase class 看起来很有希望。

我已经使用以下组件成功地做到了:

// create the Processor using my compiled Thrift class (from IDL)
boost::shared_ptr<MySDKServiceHandler> handler(new MySDKServiceHandler());
thriftCommandProcessor = boost::shared_ptr<TProcessor>(new ThriftSDKServiceProcessor(handler));

// Transport is needed, I use the TMemoryBuffer so everything is kept in local memory
boost::shared_ptr<TTransport> transport(new apache::thrift::transport::TMemoryBuffer());

// my client/server data is based on binary protocol. I pass the transport to it
thriftProtocol = boost::shared_ptr<TProtocol>(new TBinaryProtocol(transport, 0, 0, false, false));

/* .... when the message arrives through my webserver */
void parseMessage(const byte* input, const int input_size, byte*& output, int& output_size)
{
    // get the transports to write and read Thrift data
    boost::shared_ptr<TTransport> iTr = this->thriftProtocol->getInputTransport();
    boost::shared_ptr<TTransport> oTr = this->thriftProtocol->getOutputTransport();

    // "transmit" my data to Thrift
    iTr->write(input, input_size);
    iTr->flush();

    // make the Thrift work using the Processor
    this->thriftCommandProcessor->process(this->thriftProtocol, NULL);

    // the output transport (oTr) contains the called procedure result
    output = new byte[MAX_SDK_WS_REPLYSIZE];
    output_size = oTr->read(output, MAX_SDK_WS_REPLYSIZE);
}