C++ 带有Thrift和Qt的简单服务器
我目前的任务是基于Qt库创建一个简单的服务器,它具有Thrift IPC接口。 我已经下载了thrift,编译,创建了interface.thrift文件并生成了存根(--gen cpp)。另外,我已经成功地编译了简单的示例,但所有这些东西都没有Qt。 现在,我需要将thrift与Qt集成,但TQTcpServer需要异步处理器! 在存根中,我没有找到任何异步处理器(只有TDispatchProcessor) 如何将处理器传递到TQTcpServer?最好举个小例子C++ 带有Thrift和Qt的简单服务器,c++,qt,ipc,thrift,C++,Qt,Ipc,Thrift,我目前的任务是基于Qt库创建一个简单的服务器,它具有Thrift IPC接口。 我已经下载了thrift,编译,创建了interface.thrift文件并生成了存根(--gen cpp)。另外,我已经成功地编译了简单的示例,但所有这些东西都没有Qt。 现在,我需要将thrift与Qt集成,但TQTcpServer需要异步处理器! 在存根中,我没有找到任何异步处理器(只有TDispatchProcessor) 如何将处理器传递到TQTcpServer?最好举个小例子 TQTcpServer(bo
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);
TQTcpServer(boost::shared_ptr server,
boost::共享的ptr处理器,
boost::共享的ptr协议工厂,
QT_PREPEND_名称空间(QObject)*parent=NULL);
我找到了解决方案,可能对其他人有用
为了在Qt中节约使用,您需要创建具有异步支持的存根
thrift --gen cpp:cob_style ./your_name.thrift
将类\u nameAncyHandler、生成的STAB中的\u 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) );
boost::共享\u ptr tcp\u服务器(新的QTcpServer())
如果(!tcp_服务器->isListening()&&&!tcp_服务器->侦听(QHostAddress::Any,9090))
{
//抛出异常
返回;
}
共享的\u ptr处理程序(新的\u name异步处理程序());
共享ptr处理器(新名称异步处理器(处理器));
共享的ptr协议工厂(新的TBinaryProtocolFactory());
boost::shared_ptr thrift_server_(新的apache::thrift::async::TQTcpServer(tcp_server_,处理器,protocolFactory));
我找到了解决方案,可能对其他人有用
为了在Qt中节约使用,您需要创建具有异步支持的存根
thrift --gen cpp:cob_style ./your_name.thrift
将类\u nameAncyHandler、生成的STAB中的\u 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) );
boost::共享\u ptr tcp\u服务器(新的QTcpServer())
如果(!tcp_服务器->isListening()&&&!tcp_服务器->侦听(QHostAddress::Any,9090))
{
//抛出异常
返回;
}
共享的\u ptr处理程序(新的\u name异步处理程序());
共享ptr处理器(新名称异步处理器(处理器));
共享的ptr协议工厂(新的TBinaryProtocolFactory());
boost::shared_ptr thrift_server_(新的apache::thrift::async::TQTcpServer(tcp_server_,处理器,protocolFactory));
这是我的main的样子,非常简单,这都是在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();
}
intmain(intargc,char*argv[]){
qcorea应用程序(argc、argv);
boost::shared_ptr tcp_server(新的qtcserver());
boost::shared_ptr处理程序(新的UserStorageAsyncHandler());
boost::shared_ptr处理器(新的UserStorageAsyncProcessor(handler));
boost::shared_ptr protocolFactory(新的TBinaryProtocolFactory());
boost::共享\u ptr节俭\u服务器_(
新的apache::thrift::async::TQTcpServer(tcp_服务器、处理器、protocolFactory));
如果(!tcp_服务器->侦听(QHostAddress::Any,27015)){
std::cout这是我的main的样子,非常简单,这都是在本地主机上运行的
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();
}
intmain(intargc,char*argv[]){
qcorea应用程序(argc、argv);
boost::shared_ptr tcp_server(新的qtcserver());
boost::shared_ptr处理程序(新的UserStorageAsyncHandler());
boost::shared_ptr处理器(新的UserStorageAsyncProcessor(handler));
boost::shared_ptr protocolFactory(新的TBinaryProtocolFactory());
boost::共享\u ptr节俭\u服务器_(
新的apache::thrift::async::TQTcpServer(tcp_服务器、处理器、protocolFactory));
如果(!tcp_服务器->侦听(QHostAddress::Any,27015)){
std::cout看起来不错;)你检查UserStorageAsyncHandler中的代码是否有错误或死循环吗?看起来不错;)你检查UserStorageAsyncHandler中的代码是否有错误或死循环吗?我一整天都在寻找如何使异步工作。为什么没有文档。我一整天都在寻找如何实现异步et异步工作。为什么没有文档。