I am trying to learn how to use gRPC and make a simple server class that has some services that manipulate some shared data. As a first try, I put all the shared data in a class and instantiated that as a member of a server class. But it seems that the services cannot access the member variables of the server class.
Here is an example:
class grpcServices final : public bla::Services::Service {
public:
grpcServices() { test_3.reset(new std::vector<double>{0, 1, 2, 3, 4}); };
grpc::Status myServ(grpc::ServerContext *context,
const bla::Request *request,
bla::Responce *reply) override;
void run(const std::string &server_address = "localhost:50051");
private:
std::unique_ptr<grpc::Server> server_;
std::vector<double> test_1;
std::vector<double> *test_2;
std::unique_ptr<std::vector<double>> test_3;
};
void grpcServices::run(const std::string &server_address) {
test_1.push_back(2);
test_2 = new std::vector<double>(4, 0);
std::cout << "run -> test1 " << test_1.size() << std::endl;
std::cout << "run -> test2 " << test_2->size() << std::endl;
std::cout << "run -> test3 " << test_3->size() << std::endl;
test_3.reset(new std::vector<double>{0});
std::cout << "run -> test3 " << test_3->size() << std::endl;
bla::grpcServices service;
grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
grpc::ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
server_ = builder.BuildAndStart();
std::cout << "Server listening on " << server_address << std::endl;
server_->Wait();
}
grpc::Status grpcServices::myServ(
grpc::ServerContext *context, const bla::Request *request,
bla::Responce *reply) {
std::cout << "serv -> test1 " << test_1.size() << std::endl;
std::cout << "serv -> test2 " << test_2->size() << std::endl;
std::cout << "serv -> test3 " << test_3->size() << std::endl;
return grpc::Status::OK;
}
Calling first the run method from the main and then calling the myServe from the client I get:
run -> test1 1
run -> test2 4
run -> test3 5
run -> test3 1
Server listening on localhost:50051
serv -> test1 0
serv -> test2 18446744072591769856
serv -> test3 5
What am I doing wrong here?
Aucun commentaire:
Enregistrer un commentaire