gRPC源码分析(c++)

首先需要按照grpc官网上说的办法从github上下载源码,编译,然后跑一跑对应的测试代码。我分析的代码版本为v1.20.0。

在cpp的helloworld例子中,client端,第一个函数是创建channel。

GreeterClient greeter(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));

我们从这里开始分析,CreateChannel这个函数的具体实现在src/cpp/client/create_channel.cc(这个文件里指定了namespace grpc),CreateChannel再调用CreateCustomChannel,这里会根据creds的类型来创建Channel。

std::shared_ptr<Channel> CreateCustomChannel(
    const grpc::string& target,
    const std::shared_ptr<ChannelCredentials>& creds,
    const ChannelArguments& args) {
  GrpcLibraryCodegen init_lib;  // We need to call init in case of a bad creds.
  return creds ? creds->CreateChannel(target, args)
               : CreateChannelInternal(
                     "",
                     grpc_lame_client_channel_create(
                         nullptr, GRPC_STATUS_INVALID_ARGUMENT,
                         "Invalid credentials."),
                     std::vector<std::unique_ptr<
                         experimental::ClientInterceptorFactoryInterface>>());
}
creds的类型为ChannelCredentials,那我们来具体看看ChannelCredentials这个类是如何实现的,ChannelCredentials这个类的定义在include/grpcpp/security/credentials.h文件中,这里定义了虚函数CreateChannel。
virtual std::shared_ptr<Channel> CreateChannel(
      const grpc::string& target, const ChannelArguments& args) = 0;

以ChannelCredentials类为父类的子类有,InsecureChannelCredentialsImpl,CronetChannelCredentialsImpl,SecureChannelCredentials,具体是哪个类,我们需要再看下CreateChannel的第二个参数,

grpc::InsecureChannelCredentials()
InsecureChannelCredentials这个函数的定义和实现在src/cpp/client/insecure_credentials.cc文件中,函数中创建了一个指向ChannelCredentials类的InsecureChannelCredentialsImpl对象,再看InsecureChannelCredentialsImpl类实现的CreateChannel。也就是根据c++的多态用法,在上面提到的CreateCustomChannel函数中,调用的CreateChannel,就是调用InsecureChannelCredentialsImpl这个类的CreateChannel。

我们再来看一下CreateChannel的调用关系,CreateChannel->CreateChannelWithInterceptors->CreateChannelInternal,CreateChannelInternal其实就是创建了一个Channel对象,创建对象的参数中,有host信息,指向grpc_channel的指针,还有一个是ClientInterceptorFactoryInterface类指针的vector。这里我先重点关注下第二个参数,看其是如何生成的。在CreateChannelWithInterceptors函数中,使用grpc_insecure_channel_create这个函数来生成grpc_channel,下面来具体分下这个函数。
grpc_insecure_channel_create 这个函数是grpc core中提供的so接口,实现在ext/transport/chttp2/client/insecure/channel_create.cc这个文件中。

 

posted @ 2019-04-19 22:07  行木辛  阅读(4428)  评论(0编辑  收藏  举报