C++ Thrift: Can I use TFramedTransportFactory with TThreadedServer?

834 views Asked by At

The following code works.

Server:



    // SampleServiceHandler is a class that implements 
    // the Thrift service methods.
    shared_ptr handler(new SampleServiceHandler);
    shared_ptr processor(new SampleServiceProcessor(handler));
    shared_ptr serverTransport(
       new transport::TServerSocket(serverPort));
    shared_ptr transportFactory(
       new transport::TFramedTransportFactory());
    shared_ptr protFactory(
       new protocol::TBinaryProtocolFactory());
    shared_ptr server(new server::TThreadedServer(
       processor, serverTransport, transportFactory, protFactory));
    server->serve();

Client:

shared_ptr<TSocket> socket(new TSocket(serverName, serverPort));
shared_ptr<TTransport> transport(new TFramedTransport(socket));
shared_ptr<protocol::TProtocol> protocol(
   new protocol::TBinaryProtocol(transport));
shared_ptr<ThriftClient> client(new SampleServiceClient(protocol));
transport->open();
client->sampleThriftMethod();

However, it was also working before when I was using TBufferedTransportFactory instead of TFramedTransportFactory on the server side. The only sample code I've found for using TThreadedServer uses TBufferedTransportFactory, so I'm wondering if there's something wrong with TFramedTransportFactory.

The reason I switched it is because I want a Java client that uses TFramedTransport to talk to a Java TThreadedSelectorServer-using server to also be able to talk to this C++ TThreadedServer-using server.

1

There are 1 answers

0
Hcorg On

TFramedTransport is necessary in C++ lib when using TNonblockingServer (similiar to Java TThreadedSelectorServer).

In other situations it is optional, yet it will work. It's not used in examples, are those are using TThreadedServer.

Still - there's nothing wrong with your setup.