简体   繁体   English

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

[英]Apache Thrift for just processing, not server

I hope I don't have misunderstood the Thrift concept, but what I see from (example) questions like this , this framework is composed by different modular layers that can be enabled or disabled. 我希望我不要误解Thrift概念,但是从这样的(示例) 问题中可以看到,该框架由可以启用或禁用的不同模块化层组成。

I'm mostly interesed in the "IDL part" of Thrift, so that I can create a common interface between my C++ code and an external Javascript application. 我主要对Thrift的“ IDL部分”感兴趣,以便可以在C ++代码和外部Javascript应用程序之间创建公共接口。 I would like to call C++ functions using JS, with Binary data transmission, and I've already used the compiler for this. 我想使用带有二进制数据传输的JS调用C ++函数,并且我已经为此使用了编译器。

But both my C++ (the server) and JS (client) application already exchange data using a C++ Webserver with Websockets support, it is not provided by Thrift . 但是我的C ++(服务器)和JS(客户端)应用程序已经使用具有Websockets支持的C ++ Web服务器交换了数据,而Thrift却没有提供

So I was thinking to setup the following items: 所以我在考虑设置以下项目:

  • In JS (already done): 在JS中(已完成):

    • TWebSocketTransport to send data to my "Websocket server" (with host ws://xxx.xxx.xxx.xxx) TWebSocketTransport将数据发送到我的“ Websocket服务器”(主机为ws://xxx.xxx.xxx.xxx)
    • TBinaryProtocol to encapsulate the data (using this JS implementation ) TBinaryProtocol封装数据(使用此JS实现
    • The compiled Thrift JS library with the correspondent C++ functions to call (done with the JS compiler) 带有相应C ++函数调用的已编译Thrift JS库(使用JS编译器完成)
  • In C++ (partial): 在C ++(部分)中:

    • TBinaryProtocol to encode/decode the data TBinaryProtocol编码/解码数据
    • A TProcessor with handler to get the data from the client and process it 一个带有处理程序的TProcessor,用于从客户端获取数据并进行处理

For now, the client is already able to sent requests to my websocket server, I see receiving them in binary form and I just need Thrift to: 到目前为止,客户端已经能够将请求发送到我的websocket服务器,我看到接收到的请求是二进制形式,我只需要Thrift即可:

  1. Decode the input 解码输入
  2. Call the appropriate C++ function 调用适当的C ++函数
  3. Encode the 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. 我看到有TProcessor-> process()函数,当我接收二进制数据时我试图使用它,但是它需要输入/输出TProtocol。 No problem here... but in order to create the TBinaryProtocol I also need a TTransport! 没问题...但是为了创建TBinaryProtocol,我还需要一个TTransport! If no Thrift server is expected... what Transport should I use? 如果不需要Thrift服务器...我应该使用哪种传输方式?

I tried to set TTransport to NULL in TBinaryProtocol constructor, but once I use it it gives nullptr exception. 我试图在TBinaryProtocol构造函数中将TTransport设置为NULL,但是一旦使用它,它就会给出nullptr异常。

Code is something like: 代码类似于:

Init: 在里面:

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

When data arrives: 数据到达时:

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. 我看到有TProcessor-> process()函数,当我接收二进制数据时我试图使用它,但是它需要输入/输出TProtocol。 No problem here... but in order to create the TBinaryProtocol I also need a TTransport! 没问题...但是为了创建TBinaryProtocol,我还需要一个TTransport! If no Thrift server is expected... what Transport should I use? 如果不需要Thrift服务器...我应该使用哪种传输方式?

The usual pattern is to store the bits somewhere and use that buffer or data stream as the input, same for the output. 通常的模式是将位存储在某处,并使用该缓冲区或数据流作为输入,与输出相同。 For certain languages there is a TStreamTransport available, for C++ the TBufferBase class looks promising to me. 对于某些语言,有TStreamTransport可用,对于C ++, TBufferBase类对我来说很有希望。

I've managed to do it using the following components: 我设法使用以下组件来做到这一点:

// 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM