gpt4 book ai didi

c++ - 用 C++ 编写的 Apache Thrift 服务器抛出 TTransportException

转载 作者:行者123 更新时间:2023-11-30 03:31:47 39 4
gpt4 key购买 nike

我正在试验使用 Visual Studio 2015 用 C++ 编写的非常简单的 Apache Thrift 服务器和客户端。代码基于官方 Apache Thrift 示例。

我使用的是最新版本的 Thrift (0.10.0)、Boost (1.64.0) 和 OpenSSL (1.1.0e)。

从客户端到服务器的每次调用都会在 TTransport.h 第 43 行中触发 TTransportException:

throw TTransportException(TTransportException::END_OF_FILE, "No more data to read.");

这是我的 test.thrift 文件:

namespace cpp test

service Test {
void ping()
}

thrift 编译器生成 Test.h,它在下面#included 在服务器和客户端中(不显示实际代码,因为它是自动生成的)。

包含在客户端和服务器中的 thrift 头文件:

#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

#include <Test.h>

客户端主要:

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using namespace std;

int main()
{
boost::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
boost::shared_ptr<TTransport> transport(new TFramedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
test::TestClient client(protocol);

try {
transport->open();

client.ping();
cout << "ping()" << endl;

transport->close();
}
catch (TException& tx) {
cout << "ERROR: " << tx.what() << endl;
}
return 0;
}

和服务器主要:

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using namespace std;

class TestHandler : virtual public test::TestIf {
public:
TestHandler() {
// Your initialization goes here
}

void ping() {
// Your implementation goes here
printf("ping\n");
}
};


int main()
{
std::cout << "Starting thrift server thread" << std::endl;
int port = 9090;
boost::shared_ptr<TestHandler> handler(new TestHandler());
boost::shared_ptr<TProcessor> processor(new test::TestProcessor(handler));
boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
boost::shared_ptr<TTransportFactory> transportFactory(new TFramedTransportFactory());
boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

boost::shared_ptr< apache::thrift::server::TSimpleServer > server = boost::shared_ptr< TSimpleServer>(new TSimpleServer(processor, serverTransport, transportFactory, protocolFactory));

server->serve();
return 0;
}

我也尝试使用 TBufferedTransport 和 TJSONProtocol 得到相同的结果。

抛出异常表明这不是正常运行,然而,调用被接收并处理(异常发生在调用TestHandler::ping()之后)并且服务器继续监听并接收请求(每次触发相同的错误),所以这是一个可恢复的条件。

所以我想知道为什么会发生这种情况,它是否可以/应该修复,以及如何,如果不是,尽管有这个异常,使用服务器是否安全。

最佳答案

按设计。

Thrift 库以这种方式实现,其中通过抛出 TTransportException 在内部发出连接结束信号。

关于c++ - 用 C++ 编写的 Apache Thrift 服务器抛出 TTransportException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43927521/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com