繁体   English   中英

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

[英]Apache Thrift for just processing, not server

我希望我不要误解Thrift概念,但是从这样的(示例) 问题中可以看到,该框架由可以启用或禁用的不同模块化层组成。

我主要对Thrift的“ IDL部分”感兴趣,以便可以在C ++代码和外部Javascript应用程序之间创建公共接口。 我想使用带有二进制数据传输的JS调用C ++函数,并且我已经为此使用了编译器。

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

所以我在考虑设置以下项目:

  • 在JS中(已完成):

    • TWebSocketTransport将数据发送到我的“ Websocket服务器”(主机为ws://xxx.xxx.xxx.xxx)
    • TBinaryProtocol封装数据(使用此JS实现
    • 带有相应C ++函数调用的已编译Thrift JS库(使用JS编译器完成)
  • 在C ++(部分)中:

    • TBinaryProtocol编码/解码数据
    • 一个带有处理程序的TProcessor,用于从客户端获取数据并进行处理

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

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

我的网络服务器将响应发送给客户端。 因此,这里不需要“节俭服务器”。 我看到有TProcessor-> process()函数,当我接收二进制数据时我试图使用它,但是它需要输入/输出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);

我的网络服务器将响应发送给客户端。 因此,这里不需要“节俭服务器”。 我看到有TProcessor-> process()函数,当我接收二进制数据时我试图使用它,但是它需要输入/输出TProtocol。 没问题...但是为了创建TBinaryProtocol,我还需要一个TTransport! 如果不需要Thrift服务器...我应该使用哪种传输方式?

通常的模式是将位存储在某处,并使用该缓冲区或数据流作为输入,与输出相同。 对于某些语言,有TStreamTransport可用,对于C ++, TBufferBase类对我来说很有希望。

我设法使用以下组件来做到这一点:

// 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