GRPC 错误代码 12 客户端服务器应用程序 C++ [调试]

GRPC error code 12 Client server application C++ [Debug]

我需要使用 gRPC 在 C++ 中创建客户端服务器应用程序。我在 gRPC 上参考了以下 tutorial and the example 来制作一个客户端服务器应用程序,它看起来与存储库中的 helloworld 示例 (grpc/examples/cpp/helloworld/) 非常相似。但是,当我 运行 服务器端 (manager.cc) 时,我收到一条消息说 Error code 12: RPC failed。请让我知道哪里出错了:

manager.cc(类似于greeter_server.cc):

#include<stdio.h>
#include<iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "nodes.grpc.pb.h"
#include<map>
#define MAX_NODES 20
using namespace std;
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using nodes::joinrequest;
using nodes::nodejoin;
using nodes::serverresponse;
struct track
{
    int nodes_having_it[MAX_NODES];
    int last_serviced_by;
    int tot_nodes;
};
#define MAX_NO_OF_NODES 15
static map<int,int> map_key_node;//Primary
static map<int, struct track> track_service;//Keep track of replicated nodes that have the key
static map<int, int> version_map;

list <int> nodes_joined;

struct NodeDS{
    int nodeid;
    int pred;
    int succ;
    int version;
    static map<int,int> data_stored;//storage
};

class nodejoining final: public nodejoin::Service
{
    Status joinCM(ServerContext* context, joinrequest* request,serverresponse* reply_CM)
    {
    cout << "called";        
    int node_id = request -> id();
        nodes_joined.push_back(node_id);
    //reply_CM.set_stat("done");
    cout << "OK";
        return Status::OK;
    }
};

void Nodeserver()
{
    std::string server_address("0.0.0.0:50051"); // As of now to test with a single node, have to figure out a way to give different addressed to different nodes.
    nodejoining service;

    ServerBuilder builder;
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    builder.RegisterService(&service);
    std::unique_ptr<Server> server(builder.BuildAndStart());
    std::cout << "Server listening on " << server_address << std::endl;
    server->Wait();

}

int main()
{

    Nodeserver();
    std::hash<string> hashvalue;
    string client_id = "Haha"; //As of now --- to be obtained from client later
    long nodeid_hash = hashvalue(client_id);
    cout << nodeid_hash << "\n";
    int data = 100; 
     return 0;



}

node.cc(类似于greeter_client.cc):

#include<stdio.h>
#include<iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "nodes.grpc.pb.h"
#include<map>
#define MAX_NODES 20
using namespace std;

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using nodes::joinrequest;
using nodes::nodejoin;
using nodes::serverresponse;

class Node_init
{
public:
    Node_init(std::shared_ptr<Channel> channel): stub_(nodejoin::NewStub(channel)) {}
    std::string joinCM(const int& n_id)
    {
        joinrequest request_CM;
        request_CM.set_id(n_id);
        cout << "1" << "\n";

        serverresponse reply_CM;
        ClientContext context;
        cout << "\n" << "2";

        Status status = stub_->joinCM(&context, request_CM, &reply_CM);
        cout << "\n" << "3";
        if(status.ok())
        {
            cout<<"\n I have joined you";
            return "Success";
        }
        else
        {   std::cout << status.error_code() << ": " << status.error_message() << std::endl;
            return "Failed";

        }


    }
private:
std::unique_ptr<nodejoin::Stub> stub_;
};

int main(int argc, char** argv)
{
 Node_init nj(grpc::CreateChannel("localhost:50051",grpc::InsecureChannelCredentials()));
 std::string reply = nj.joinCM(2);
 std::cout << "\n" << reply;

}

nodes.proto(类似于 grpc/examples/protos/ 中的 helloworld.proto):

syntax = "proto3";

package nodes;

service nodejoin
{
rpc joinCM (joinrequest) returns (serverresponse) {}
}

message joinrequest
{
int32 id = 1;
}

message serverresponse
{
string stat = 1;
}

你能用以下方式打开追踪功能吗:

export GRPC_TRACE=api,call_error
export GRPC_VERBOSITY=debug

您的 joinCM 定义中缺少一个常量

Status joinCM(ServerContext* context, joinrequest* request,serverresponse* reply_CM)

应该是

Status joinCM(ServerContext* context, const joinrequest* request,serverresponse* reply_CM) 反而。编译器通过 'hides overloaded virtual function' 警告表明这一点 :)