[英]Simple server with Thrift and Qt
我當前的任務是使用基於Qt庫的Thrift IPC接口創建簡單的服務器。 我已經下載了Thrift,已編譯,創建了interface.thrift文件並生成了存根(--gen cpp)。 另外,我已經成功地編譯了簡單的示例,但是所有這些東西都沒有Qt。 現在,我需要將Thrift與Qt集成在一起,但是TQTcpServer需要Async處理器! 在存根中,我沒有找到任何異步處理器(只有TDispatchProcessor)。
如何將處理器傳遞給TQTcpServer? 小例子將是優選的。
TQTcpServer(boost::shared_ptr<QTcpServer> server,
boost::shared_ptr<TAsyncProcessor> processor,
boost::shared_ptr<apache::thrift::protocol::TProtocolFactory> protocolFactory,
QT_PREPEND_NAMESPACE(QObject)* parent = NULL);
我已經找到解決方案,也許對其他人會有用
為了在Qt中使用Thrift,您需要創建具有異步支持的STUB
thrift --gen cpp:cob_style ./your_name.thrift
將生成的STAB中的類your_nameAsyncHandler,your_nameHandler復制到您的項目中
boost::shared_ptr<QTcpServer> tcp_server_( new QTcpServer() )
if( !tcp_server_->isListening() && !tcp_server_->listen(QHostAddress::Any, 9090) )
{
// throw exception
return;
}
shared_ptr<your_nameAsyncHandler> handler(new your_nameAsyncHandler());
shared_ptr<TAsyncProcessor> processor(new your_nameAsyncProcessor(handler));
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
boost::shared_ptr<apache::thrift::async::TQTcpServer> thrift_server_( new apache::thrift::async::TQTcpServer( tcp_server_, processor, protocolFactory) );
這是我主要的樣子,非常簡單,它們都在localhost上運行。
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
boost::shared_ptr<QTcpServer> tcp_server_( new QTcpServer() );
boost::shared_ptr<UserStorageAsyncHandler> handler(new UserStorageAsyncHandler());
boost::shared_ptr<TAsyncProcessor> processor(new UserStorageAsyncProcessor(handler));
boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
boost::shared_ptr<apache::thrift::async::TQTcpServer> thrift_server_(
new apache::thrift::async::TQTcpServer( tcp_server_, processor, protocolFactory) );
if (!tcp_server_->listen(QHostAddress::Any, 27015)) {
std::cout << "TCP Server not listening" << std::endl;
return 1;
}
return a.exec();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.