protocol-buffersgrpc-c++

How to send the data from a server to a client in the embedded messages in C++ using grpc?


I am implementing a simple client-server grpc-c++ based application. In the Hello rpc, I am taking the request and sending the fields of another message called SeverInfo as response. The problem is I exactly don't know how to send this ServerInfo data to a client from server side. We basically use set_fieldname(ex: set_name) for general datatypes to send the data but how should we send this serverInfo data to HelloResponse and then to HelloRequest. Can somebody please help me??

Below I am attaching the proto file.

syntax = "proto3";
package sample;

service Sample {
   rpc Hello(HelloRequest) returns (HelloReply){}
}
message HelloRequest {
    string name = 1;
}
message HelloReply {
    ServerInfo sinfo = 1;
}
message ServerInfo {
    string name = 1;
    string os = 2;
}

Solution

  • Here is the answer that worked for me. Thank you.

    ServerInfo* serverinfo=new ServerInfo();
    serverinfo->set_name("");
    serverinfo->set_os("");
    HelloReply* rep;
    rep->set_allocated_server(serverinfo);