C++ 节俭异步c++;例子
我在Apache Thrift发行版和Apache.org网站上寻找了一个例子,但没有成功 我希望有人给我介绍一个异步客户机的示例实现 经典Apache Thrift中的非阻塞服务器(不是 Facebook),它使用“-gen cpp” <>我可以看到类似的问题,题目是“Apache TurfsC++ +异步客户端”,但答案并没有包含所有的部分。 我想查看“.thrift”文件,然后是填写好的服务器和相应的客户端代码 我真的很想相信有人做了这件事,而我只是没有我想象的那么好的谷歌用户C++ 节俭异步c++;例子,c++,asynchronous,thrift,C++,Asynchronous,Thrift,我在Apache Thrift发行版和Apache.org网站上寻找了一个例子,但没有成功 我希望有人给我介绍一个异步客户机的示例实现 经典Apache Thrift中的非阻塞服务器(不是 Facebook),它使用“-gen cpp” 我可以看到类似的问题,题目是“Apache TurfsC++ +异步客户端”,但答案并没有包含所有的部分。 我想查看“.thrift”文件,然后是填写好的服务器和相应的客户端代码 我真的很想相信有人做了这件事,而我只是没有我想象的那么好的谷歌用户 我知道Face
我知道Facebook版本(fbthrift)是为了更好地实现这一点,但我对该版本的不稳定性感到失望。如果有人能给我指出一个稳定的版本,它不是每天都在修改的,我可以把它看作是一种选择。 < P>不确定你是什么意思的异步客户端,但我会尽我所能来回答这个问题,根据我所理解的。p> 如果说“异步客户机”,您是指node.js中的异步,在node.js中,执行遵循回调结构,这不是开源thrift的一部分。然而,它可以在中获得。脸谱网有一系列工具,包括它们流行的开源C++库。通过节俭C++客户端接口调用其他节俭服务必须阻止。 这是我在试验异步非阻塞服务器时开始使用的代码。希望能有帮助 节俭
#!/usr/local/bin/thrift --gen cpp
namespace cpp something
service Something {
i32 ping()
}
SomethingServer.cpp
#include "gen-cpp/Something.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/server/TThreadedServer.h>
#include <thrift/server/TNonblockingServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/concurrency/ThreadManager.h>
#include <iostream>
using std::cout;
using std::endl;
class SomethingHandler : virtual public something::SomethingIf {
public:
SomethingHandler() {
cout << "Initialized" << endl;
}
int32_t ping() override {
// Your implementation goes here
cout << "Ping!" << endl;
return 1;
}
};
int main(int argc, char **argv) {
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace ::apache::thrift::concurrency;
using boost::shared_ptr;
using namespace ::something;
int port = 9090;
shared_ptr<SomethingHandler> handler(new SomethingHandler());
shared_ptr<TProcessor> processor(new SomethingProcessor(handler));
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
// using thread pool with maximum 15 threads to handle incoming requests
shared_ptr<ThreadManager> threadManager
= ThreadManager::newSimpleThreadManager(15);
shared_ptr<PosixThreadFactory> threadFactory
= shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
TNonblockingServer server(processor, protocolFactory, port, threadManager);
server.serve();
return 0;
}
#include "Something.h"
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace Test;
int main(int /* argc */, char** /* argv */) {
boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
boost::shared_ptr<TTransport> transport(new TFramedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
SomethingClient client(protocol);
transport->open();
for (auto i = 0; i < 10000; ++i) {
client.ping();
}
transport->close();
return 0;
}
#包括“gen cpp/Something.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用std::cout;
使用std::endl;
类SomethingHandler:virtual public something::SomethingIf{
公众:
SomethingHandler(){
不能关闭();
返回0;
}
我很高兴看到这段代码。这正是我要找的例子,因为它似乎不是来自“fbthrift”,也不依赖于包(比如“folly”)这是我们希望避免的。今晚晚些时候我将尝试这个示例,但我现在想花点时间向您表示感谢!更多信息稍后…进一步阅读后,您的示例代码似乎没有实现任何类型的回调。我想您是在试图告诉我,我必须使用fbthrift来成功创建当服务器完成任务时,客户端调用会立即返回,并带有回调机制。这是你的理解吗?@GordonFossum是的,这就是我从它那里得到的,我看到了thrift面临的问题。fbthrift使用folly futures和回调支持来实现基于回调的处理。Boost futures确实有但我认为还没有人在开源节俭中实现这一点。