简体   繁体   English

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

[英]Apache Thrift server written in C++ throws TTransportException

I'm experimenting with a very simple Apache Thrift server and client written in C++ using Visual Studio 2015. The code is based on the the official Apache Thrift samples. 我正在尝试使用Visual Studio 2015用C ++编写的非常简单的Apache Thrift服务器和客户端。该代码基于官方的Apache Thrift示例。

I'm using the latest version of Thrift (0.10.0), Boost (1.64.0) and OpenSSL (1.1.0e). 我正在使用Thrift(0.10.0),Boost(1.64.0)和OpenSSL(1.1.0e)的最新版本。

Each call from the client to the server triggers a TTransportException in TTransport.h line 43: 从客户端到服务器的每次调用都会在TTransport.h第43行中触发TTransportException:

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

Here's my test.thrift file: 这是我的test.thrift文件:

namespace cpp test

service Test {
    void ping()
}

The thrift compiler generates Test.h which is #included below in both server and client (not showing the actual code as it's autogenerated). 节俭的编译器生成Test.h,该文件在服务器和客户端中都包含在下面(未显示实际代码,因为它是自动生成的)。

thrift header files included in both client and server: 客户端和服务器中都包含节俭头文件:

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

client main: 客户主要:

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;
}

and server main: 和服务器主:

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;
}

I also tried using TBufferedTransport and TJSONProtocol with the same result. 我也尝试过使用TBufferedTransport和TJSONProtocol获得相同的结果。

The fact that an exception is thrown indicates that this is not normal functioning, however, the call is received and processed (the exception happens after the call to TestHandler::ping()) and the server continues to listen to and receive requests (triggering the same error each time), so it is a recoverable condition. 抛出异常的事实表明这不是正常的功能,但是,调用已被接收和处理(该异常发生在对TestHandler :: ping()的调用之后),并且服务器继续侦听和接收请求(触发每次都有相同的错误),因此这是可恢复的情况。

So I'm wondering why this is happening, is it something that can/should be fixed, and how, and if not, is it safe to use the server despite this exception. 因此,我想知道为什么会这样,是否可以/应该解决该问题,以及尽管有此例外,如何以及是否可以安全使用服务器?

By design. 通过设计。

Thrift libraries are implemented in the way, where the end of an connection is signaled internally by throwing an TTransportException . 节俭库是通过以下方式实现的:通过抛出TTransportException内部通知连接结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM