I'm hoping to reuse an opened thrift connection to send/recieve multiple messages/responses over the duration of a session.
The client is created in the file
Client.h
#include <iostream>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>
#include "User.h"
#include <future>
using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
Client connect(const std::string &host, int port);
string run_command(Client client, const int32_t op_code, const std::string addr);
Client.cpp
#include <iostream>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>
#include "Client.h"
#include <future>
using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
Client connect(const std::string &host, int port) {
std::shared_ptr<TTransport> socket(new TSocket(host, port));
std::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
std::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
Client client(protocol);
cout << "CONNECTING";
transport->open();
return client;
}
string run_command(Client client, const int32_t op_code, const std::string addr){
printf("CLIENT\n");
client.send_run_command(op_code, addr);
printf("CLIENT SEND_RUN_COMMAND COMPLETE\n");
string resp;
client.recv_run_command(resp);
printf("CLIENT RECV_RUN_COMMAND COMPLETE: %s\n", resp.c_str());
return resp;
}
However, I want to reuse the same client in a different file.
Shell.cpp
#include "Client.h"
#include <future>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
Client client;
int32_t
taskset_fn(int32_t argc, char** argv) {
// stuff omitted
// auto client = connect("127.0.0.1", 9095); -> don't want to recreate client every time
string output = run_command(client, 6, string(cmd_buf));
return 0;
}
int lwshell_connect(const std::string &host, int port){
client = connect("127.0.0.1", 9095);
}
Building yields the following error:
no matching function for call to ‘ClientT<apache::thrift::protocol::TProtocol>::ClientT()’
Client client;
^~~~~~
In file included from [full path omitted]/programs/libuser/librpc/src/Client.h:7:0,
from [full path omitted]/programs/libuser
[full path omitted]/programs/libuser/librpc/src/User.h:183:3: note: candidate: ClientT<Protocol_>::ClientT(std::shared_ptr<_Tp>, std::shared_ptr<_Tp>) [with Protocol_ = apache::thrift::protocol::TProtocol]
ClientT(std::shared_ptr< Protocol_> iprot, std::shared_ptr< Protocol_> oprot) {
^~~~~~~
[full path omitted]/programs/libuser/librpc/src/User.h:183:3: note: candidate expects 2 arguments, 0 provided
[full path omitted]/programs/libuser/librpc/src/User.h:180:3: note: candidate: ClientT<Protocol_>::ClientT(std::shared_ptr<_Tp>) [with Protocol_ = apache::thrift::protocol::TProtocol]
ClientT(std::shared_ptr< Protocol_> prot) {
^~~~~~~
[full path omitted]/programs/libuser/librpc/src/User.h:180:3: note: candidate expects 1 argument, 0 provided
In short, the compiler thinks that Client client;
is a function call, but I just want a static instance of the client. How can I make a reusable thrift client?
Things I've tried
1.
I have tried doing Client client(std::shared_ptr<TProtocol>);
, which results in
: could not convert ‘client’ from ‘Client (*)(std::shared_ptr<apache::thrift::protocol::TProtocol>) {aka ClientT<apache::thrift::protocol::TProtocol>
(*)(std::shared_ptr<apache::thrift::protocol::TProtocol>)}’ to ‘Client {aka ClientT<apache::thrift::protocol::TProtocol>}’
2.
Making the global client a Client*:
Shell.cpp
Client* client;
...
Client client_temp = connect("127.0.0.1", 9095);
client = &client_temp;
printf("addr: %p\n", (void*) client);
...
printf("taskset ptr: %p\n", (void*) client);
string output = run_command(*client, 6, string(cmd_buf));```
causes segfault
▶ ./ShellUser
CONNECTINGaddr: 0x7fffbfdf2430
$ taskset 1
RECV:taskset 1
, 10
parsing
ARG: taskset
TASKSET
taskset ptr: 0x7fffbfdf2430
[1] 27587 segmentation fault (core dumped) ./ShellUser