首先需要按照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) = ;

以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这个文件中。

gRPC源码分析(c++)的更多相关文章

  1. gRPC源码分析0-导读

    gRPC是Google开源的新一代RPC框架,官网是http://www.grpc.io.正式发布于2016年8月,技术栈非常的新,基于HTTP/2,netty4.1,proto3.虽然目前在工程化方 ...

  2. grpc源码分析之域名解析

    环境: win7_x64,VS2015.grpc_1.3.1 场景: 在客户端中使用grpc连接服务器,在多次输入非法的地址后,再次输入正确的地址连出现连接超时的现象.侯捷先生说过“源码面前,了无秘密 ...

  3. Go合集,gRPC源码分析,算法合集

    年初时,朋友圈见到的最多的就是新的一年新的FlAG,年末时朋友圈最多的也是xxxx就要过去了,你的FLAG实现了吗? 这个公众号2016就已经创建了,但截至今年之前从来没发表过文章,现在想想以前很忙, ...

  4. gRPC源码分析2-Server的建立

    gRPC中,Server.Client共享的Class不是很多,所以我们可以单独的分别讲解Server和Client的源码. 通过第一篇,我们知道对于gRPC来说,建立Server是非常简单的,还记得 ...

  5. gRPC源码分析1-SSL/TLS

    引子 前几天看到微信后台团队分享了TLS相关文章,正好gRPC里TLS数据加密是很重要的一块,于是整理出了这篇文章. 在gRPC里,如果仅仅是用来做后端微服务,可以考虑不加密.本文太长,先给个大纲. ...

  6. frakti && RunPodSandbox 源码分析

    listen = flag.String("listen", "/var/run/frakti.sock", "...") hyperEnd ...

  7. [Abp 源码分析]十二、多租户体系与权限验证

    0.简介 承接上篇文章我们会在这篇文章详细解说一下 Abp 是如何结合 IPermissionChecker 与 IFeatureChecker 来实现一个完整的多租户系统的权限校验的. 1.多租户的 ...

  8. Envoy 源码分析--程序启动过程

    目录 Envoy 源码分析--程序启动过程 初始化 main 入口 MainCommon 初始化 服务 InstanceImpl 初始化 启动 main 启动入口 服务启动流程 LDS 服务启动流程 ...

  9. Envoy 源码分析--LDS

    Envoy 源码分析--LDS LDS 是 Envoy 用来自动获取 listener 的 API. Envoy 通过 API 可以增加.修改或删除 listener. 先来总结下 listener ...

随机推荐

  1. 学习ASP.NET Core Razor 编程系列十六——排序

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  2. QUIC协议原理分析(转)

    之前深入了解了一下HTTP1.1.2.0.SPDY等协议,发现HTTP层怎么优化,始终要面对TCP本身的问题.于是了解到了QUIC,这里分享一篇之前找到的有意义的文章. 原创地址:https://mp ...

  3. 内联汇编获取Kernaer32基址.

    DWORD GetKerner32ImageBase() { DWORD nIMageBase = 0; __asm { xor edx,edx mov ecx, fs:[0x30]; mov ecx ...

  4. Linux 用户与组的基本操作及文件权限位的设置方法

    用户的基本操作 添加用户: useradd xxx 查看所有的用户: cat /etc/passwd 用户更改组: usermod -G groups loginname 将用户从组中删除: gpas ...

  5. 💈 线程间互访助手类 (EN)

    Conmajia © 2012, 2018 Published on August 5th, 2012 Updated on February 2nd, 2019 Introduction While ...

  6. 为Qt视图中的文字添加彩虹渐变效果

    将view中的文本内容用自定义的颜色显示是一种十分常见的需求.今天我们稍微改变些"花样". 本文索引 需求定义 需求分析 代码实现 思考题 需求定义 我们的需求很简单,现在有一些在 ...

  7. [转]AI+RPA 融合更智能

    本文转自:https://www.jianshu.com/p/cf25b3dfc0f0 前面已经分析过多次RPA的本质,可以参考 [脱下外衣],看看RPA机器人到底是什么?     哪些AI相关应用技 ...

  8. jQuery获取或设置元素的宽度和高度

    jQuery获取或设置元素的宽度和高度: 可使用以下3种方法: 1,jQuery width() 和 height() 方法: 2,innerWidth() 和 innerHeight() 方法: 3 ...

  9. php禁用函数设置及查看方法详解

    这篇文章主要介绍了php禁用函数设置及查看方法,结合实例形式分析了php禁用函数的方法及使用php探针查看禁用函数信息的相关实现技巧,需要的朋友可以参考下 本文实例讲述了php禁用函数设置及查看方法. ...

  10. div在另一个div居中对齐

    position:fixed; top:0; right:0; left:0; bottom:0; margin:auto;