GHOST系统之家 - Windows系统光盘下载网站!
当前位置:GHOST系统之家>系统教程 > OHOS标准系统的IPC和RPC代码解读(一)-ipc rpc

OHOS标准系统的IPC和RPC代码解读(一)-ipc rpc

来源:Ghost系统之家浏览:时间:2023-04-12 12:56:12

OHOS标准系统的IPC和RPC代码解读(一)

作者:梁开祝 2022-09-27 15:57:20系统 OpenHarmony 在Linux命令行下进入 //foundation/communication/ipc/ 目录,通过tree命令将目录树结构打印出来并重定向到文本文件中。

​​想了解更多关于开源的内容,请访问:​​

​​51CTO开源基础软件社区​​

​​https://ost.51cto.com​​

本想接着前文《OHOS标准系统的SAMGR代码解读》继续分析dmsfwk组件的实现细节,但发现涉及太多的IPC/RPC的内容了,如果对OHOS的IPC/RPC没有足够的理解,很难把dmsfwk组件理解透彻,因此我花了点时间,先整理了一下IPC/RPC相关的代码和部分流程,作为理解SAMGR相关组件的预备知识。

IPC/RPC的代码仓库,在OHOS 3.1分支上,分为ipc和ipc_lite两个仓库,在master分支上,ipc_lite仓库已经合并到ipc仓库中,因此本文在整理ipc仓库代码目录树结构时,是以当前的master分支代码为准整理的。

但由于我当前工作主要还是在OHOS 3.1分支上,所以在后面整理相关的流程图或数据结构关系图时,还是基于3.1分支代码进行的(待930版本发布后再确认是否更新到930版本)。

1、整理代码目录树结构(基于master分支)

在Linux命令行下进入 //foundation/communication/ipc/ 目录,通过tree命令将目录树结构打印出来并重定向到文本文件中。为减少干扰,直接去除test相关的目录和文件,再酌情微调一下部分目录和文件的位置。重点阅读剩下的几个BUILD.gn文件,结合几类系统分别整理BUILD.gn中的编译目标,尽可能地挖掘有用信息(后继在深入理解代码时也可以对该目录树结构补充新的信息)。

目前我整理出来的信息大概如下:

# 系统类型:Lite类型系统(a-LiteM、b-LiteA、c-LiteL)、STD系统# Lite类型系统的编译目标:#【0】shared_library("rpc_log"),LiteM系统该文件直接编译进2.1中。#【Lite类型系统:LOG_DOMAIN 0xD001518,LOG_TAG "IPCRPC"】#【 STD类型系统:ipc_debug.h、log_tags.h】#【1】静态或动态library("rpc_adapter")【简单:RpcGetPid()/RpcGetUid()的适配】#【1.1】static_library("rpc_adapter"),LiteM系统依赖该库#【1.2】shared_library("rpc_adapter"),LiteA+LiteL小型系统依赖该库#【2】静态或动态library("rpc_manager")#【2.1】static_library("rpc_manager"),LiteM系统依赖该库#【2.2】shared_library("rpc_manager"),LiteA编译该库为空实际不依赖,LiteL小型系统依赖该库#【3】静态或动态library("dbinder")#【3.1】static_library("dbinder"),LiteM系统依赖该库#【3.2】shared_library("dbinder"),LiteA小型系统不依赖该库,LiteL小型系统依赖该库#【4】shared_library("ipc_single"),LiteM系统不依赖该库,LiteA和LiteL小型系统依赖该库# STD系统的编译目标:#【10】ohos_shared_library("ipc_core")【包含本地设备内的IPC,以及与dbinder相关的基础功能】#【11】ohos_shared_library("ipc_single")【有定义CONFIG_IPC_SINGLE,完全是本地设备内的IPC】#【12】ohos_shared_library("libdbinder")【dbinder,与RPC相关的功能实现】#【13】ohos_shared_library("rpc") 【rpc_js接口】# 依赖关系:#LiteM:依赖【1.1+2.1+3.1】(0-直接编译到2.1中)#LiteA:依赖【0+1.2+4】(2-编译为空实际不依赖,3-不依赖)#LiteL:依赖【0+1.2+2.2+3.2+4】#STD:依赖【10+11+12】+【13】# 目录树结构//foundation/communication/ipc/├── bundle.json├── BUILD.gn #定义ipc_components:STD系统依赖【10+11+12】,Lite系统依赖【interfaces/innerkits/c:rpc】├── interfaces #接口部分,整理编译目标和依赖关系│ ├── innerkits│ │ │ # Lite类型系统组件│ │ ├── c│ │ │ ├── BUILD.gn#Lite系统专用的组件【lite_component("rpc")】│ │ │ │ #LiteM:依赖【1.1+2.1+3.1】(0-直接编译到2.1中)│ │ │ │ #LiteA:依赖【0+1.2+4】(2-编译为空实际不依赖,3-不依赖)│ │ │ │ #LiteL:依赖【0+1.2+2.2+3.2+4】│ │ │ ├── dbinder #distributed binder for Lite│ │ │ │ ├── BUILD.gn#【3.1】和【3.2】│ │ │ │ └── include/dbinder_service.h│ │ │ └── ipc│ │ │ ├── BUILD.gn#【2.1】和【2.2】│ │ │ └── include│ │ │ ├── ipc_skeleton.h│ │ │ ├── rpc_errno.h│ │ │ └── serializer.h│ │ ││ │ │ # STD系统专用的组件【10】【11】【12】【13】│ │ ├── ipc_core│ │ │ ├── BUILD.gn #【10】ohos_shared_library("ipc_core")│ │ │ └── include│ │ │ ├── ipc_file_descriptor.h│ │ │ ├── ipc_object_proxy.h│ │ │ ├── ipc_object_stub.h│ │ │ ├── ipc_skeleton.h│ │ │ ├── ipc_types.h│ │ │ ├── iremote_broker.h│ │ │ ├── iremote_object.h│ │ │ ├── iremote_proxy.h│ │ │ ├── iremote_stub.h│ │ │ ├── jni_help.h│ │ │ ├── message_option.h│ │ │ ├── message_parcel.h│ │ │ └── peer_holder.h│ │ ├── ipc_single/BUILD.gn#【11】ohos_shared_library("ipc_single") + CONFIG_IPC_SINGLE│ │ └── libdbinder # distributed binder for STD│ │ ├── BUILD.gn #【12】ohos_shared_library("libdbinder")依赖【10】│ │ └── include│ │ ├── dbinder_service.h│ │ ├── dbinder_service_stub.h│ │ └── rpc_system_ability_callback.h│ └── kits│ ├── bundle.json# RPC napi for js│ └── js/napi/BUILD.gn #【13】ohos_shared_library("rpc") 依赖【10】││ #工具部分【Lite类型系统在用,STD系统不用】├── utils│ ├── include│ │ ├── log_tags.h # LOG_ID_COMMUNICATION 以及子模块的LOG_ID定义│ │ └── rpc_session_handle.h│ └── src/rpc_session_handle.c #【a+c】││ # IPC/RPC的框架实现代码├── ipc│ └── native│ │ #Lite类型系统的IPC框架代码│ ├── c│ │ ├── adapter│ │ │ ├── access_token│ │ │ │ ├── include/access_token_adapter.h│ │ │ │ └── src/access_token_adapter.c #【STD】【只有STD系统的[10][11]编译该文件,Lite类型系统不编译】│ │ │ ├── BUILD.gn #【1】单独编译 rpc_os_adapter.c 源文件│ │ │ ├── include│ │ │ │ ├── rpc_bytrace.h│ │ │ │ └── rpc_os_adapter.h│ │ │ ├── Linux/rpc_os_adapter.c #【1.2】【b+c】│ │ │ └── Liteos_m/rpc_os_adapter.c#【1.1】【a】│ │ ├── ipc│ │ │ ├── include│ │ │ │ ├── ipc_invoker.h│ │ │ │ └── ipc_types.h│ │ │ └── src│ │ │ ├── linux│ │ │ │ ├── include/sys_binder.h│ │ │ │ ├── ipc_invoker.c#【c】│ │ │ │ └── serializer_inner.c #【c】│ │ │ ├── liteos_a│ │ │ │ ├── include/lite_ipc.h│ │ │ │ ├── ipc_invoker.c#【b】│ │ │ │ └── serializer_inner.c #【b】│ │ │ └── liteos_m│ │ │ ├── ipc_invoker.c#【a】│ │ │ └── serializer_inner.c #【a】│ │ ├── manager│ │ │ ├── include│ │ │ │ ├── ipc_process_skeleton.h│ │ │ │ ├── ipc_skeleton_pri.h│ │ │ │ ├── ipc_thread_pool.h│ │ │ │ ├── iremote_invoker.h│ │ │ │ ├── rpc_log.h│ │ │ │ ├── rpc_types.h│ │ │ │ └── serializer_inner.h│ │ │ └── src│ │ │ ├── ipc_process_skeleton.c #【a+b+c】公共部分5个文件│ │ │ ├── ipc_skeleton.c #【a+b+c】公共部分5个文件│ │ │ ├── ipc_thread_pool.c#【a+b+c】公共部分5个文件│ │ │ ├── iremote_invoker.c#【a+b+c】公共部分5个文件│ │ │ ├── serializer.c #【a+b+c】公共部分5个文件│ │ │ └── rpc_log.c#【a】编译在【2.1】,【b+c】LiteA+LiteL单独编译出【0】│ │ └── rpc│ │ ├── include│ │ │ ├── dbinder_invoker.h│ │ │ ├── rpc_feature_set.h│ │ │ ├── rpc_process_skeleton.h│ │ │ └── rpc_trans_callback.h│ │ ├── ipc_adapter│ │ │ ├── include│ │ │ │ ├── ipc_proxy_inner.h│ │ │ │ └── ipc_stub_inner.h│ │ │ ├── mini│ │ │ │ ├── ipc_proxy_inner.c #【a】│ │ │ │ └── ipc_stub_inner.c#【a】│ │ │ └── small│ │ │ ├── ipc_proxy_inner.c #【c】│ │ │ └── ipc_stub_inner.c#【c】│ │ ├── src│ │ │ ├── dbinder_invoker.c #【a+c】│ │ │ ├── rpc_feature_set.c #【STD】【只有STD系统的[10]编译该文件,Lite类型系统不编译】│ │ │ ├── rpc_process_skeleton.c#【a+c】│ │ │ ├── rpc_process_skeleton_virtual.c#【b+c】│ │ │ └── rpc_trans_callback.c#【a+c】│ │ └── trans_adapter│ │ ├── include│ │ │ ├── rpc_softbus_trans.h│ │ │ └── rpc_trans.h│ │ └── src./ipc/test/rpc/socket_trans/src/rpc_socket_trans.c#【c】因为enable_socket_trans[true]用socket而不用softbus│ │ ├── rpc_softbus_trans.c #【a】如果enable_socket_trans[false],则这里【+c】用softbus而不用socket│ │ └── rpc_trans.c #【a+c】│ ││ │ # STD系统的IPC框架代码│ └── src│ ├── core│ │ ├── include│ │ │ ├── buffer_object.h│ │ │ ├── comm_auth_info.h│ │ │ ├── databus_session_callback.h│ │ │ ├── dbinder_callback_stub.h│ │ │ ├── dbinder_error_code.h│ │ │ ├── dbinder_session_object.h│ │ │ ├── ipc_debug.h│ │ │ ├── ipc_process_skeleton.h│ │ │ ├── ipc_thread_pool.h│ │ │ ├── ipc_thread_skeleton.h│ │ │ ├── ipc_workthread.h│ │ │ └── stub_refcount_object.h│ │ └── source│ │ ├── buffer_object.cpp│ │ ├── comm_auth_info.cpp│ │ ├── databus_session_callback.cpp│ │ ├── dbinder_callback_stub.cpp│ │ ├── dbinder_session_object.cpp│ │ ├── ipc_file_descriptor.cpp│ │ ├── ipc_object_proxy.cpp│ │ ├── ipc_object_stub.cpp│ │ ├── ipc_process_skeleton.cpp│ │ ├── ipc_skeleton.cpp│ │ ├── ipc_thread_pool.cpp│ │ ├── ipc_thread_skeleton.cpp│ │ ├── ipc_workthread.cpp│ │ ├── iremote_broker.cpp│ │ ├── iremote_object.cpp│ │ ├── message_option.cpp│ │ ├── message_parcel.cpp│ │ ├── peer_holder.cpp│ │ └── stub_refcount_object.cpp│ ├── mock│ │ ├── include│ │ │ ├── binder_connector.h│ │ │ ├── binder_debug.h│ │ │ ├── binder_invoker.h│ │ │ ├── dbinder_base_invoker.h│ │ │ ├── dbinder_databus_invoker.h│ │ │ ├── hitrace_invoker.h│ │ │ ├── invoker_factory.h│ │ │ ├── invoker_rawdata.h│ │ │ ├── iremote_invoker.h│ │ │ └── sys_binder.h│ │ └── source│ │ ├── binder_connector.cpp│ │ ├── binder_debug.cpp│ │ ├── binder_invoker.cpp│ │ ├── hitrace_invoker.cpp│ │ ├── invoker_factory.cpp│ │ ├── nvoker_rawdata.cpp│ │ └── idbinder_databus_invoker.cpp│ ├── jni│ │ ├── include│ │ │ ├── jni_helper.h│ │ │ ├── jni_remote_object.h│ │ │ ├── ohos_rpc_message_option.h│ │ │ ├── ohos_rpc_message_parcel.h│ │ │ └── ohos_rpc_remote_object.h│ │ └── source│ │ ├── jni_helper.cpp│ │ ├── ohos_rpc_message_option.cpp│ │ ├── ohos_rpc_message_parcel.cpp│ │ └── ohos_rpc_remote_object.cpp│ └── napi│ ├── include│ │ ├── napi_ashmem.h│ │ ├── napi_message_option.h│ │ ├── napi_message_parcel.h│ │ └── napi_remote_object.h│ └── src│ ├── napi_ashmem.cpp│ ├── napi_message_option.cpp│ ├── napi_message_parcel.cpp│ ├── napi_remote_object.cpp│ └── napi_rpc_native_module.cpp││ # dbinder服务的实现代码【又分Lite类型系统和STD系统】└── services└── dbinder│ # Lite类型系统的dbinder服务的实现代码【LiteA系统不走dbinder】├── c│ ├── include│ │ ├── dbinder_service_inner.h│ │ ├── dbinder_stub.h│ │ ├── dbinder_trans_callback.h│ │ └── dbinder_types.h│ ├── ipc_adapter│ │ ├── include/dbinder_ipc_adapter.h│ │ ├── mini/dbinder_ipc_adapter.c #【a】│ │ └── small/dbinder_ipc_adapter.c#【c】│ └── src│ ├── dbinder_service.c#【a+c】│ ├── dbinder_stub.c #【a+c】│ └── dbinder_trans_callback.c #【a+c】│ │ # STD系统的dbinder服务的实现代码└── dbinder_service#【12】ohos_shared_library("libdbinder")├── include│ ├── dbinder_death_recipient.h│ ├── dbinder_log.h│ ├── dbinder_remote_listener.h│ └── dbinder_sa_death_recipient.h└── src├── dbinder_death_recipient.cpp├── dbinder_sa_death_recipient.cpp├── dbinder_service.cpp├── dbinder_service_stub.cpp└── socket/dbinder_remote_listener.cpp

从上面的整理可以看出,Lite类型系统和STD系统,各有一套自己的IPC/RPC的实现代码。因为当前目标是要研究标准系统的dmsfwk组件(和samgr等其他组件),所以我们当前的重点是上文中的编译目标【10】【11】【12】。

通过对比【10-ipc_core】和【11-ipc_single】两个编译目标的BUILD.gn文件,可以认为【11】是【10】的子集,因为:

目标【10】没有定义CONFIG_IPC_SINGLE,目标【11】是有定义的。在部分共用的源代码文件中,有使用 #ifndef CONFIG_IPC_SINGLE 控制的代码,是目标【10】专用的。目标【10】比目标【11】多编译几个文件,是与DBinder、DataBus相关的,用于RPC。

目前还没有非常确定【10】【11】各自的应用场景有多大的差别,但我的理解偏向于Stub/Server端多依赖【10】,而Proxy/Client端多依赖【11】,或者分布式SA多依赖【10】,普通SA多依赖【11】(有待后面有更多的理解后确认)。

不管怎样,【10】和【11】两个目标的代码是共用的,都属于IPC范畴,可以一并阅读理解。【12】则属于RPC范围,再单独阅读理解。至于Lite类型系统的编译目标,待以后有空研究的时候再补充相关总结。

2、基于Binder驱动的IPC(基于3.1 Release分支)

ipc组件根目录下的README.md文件中有提到:

IPC(Inter-Process Communication)与RPC(Remote Procedure Call)机制用于实现跨进程通信,不同的是前者使用Binder驱动,用于设备内的跨进程通信,而后者使用软总线驱动,用于跨设备跨进程通信。IPC和RPC通常采用客户端-服务器(Client-Server)模型,服务请求方(Client)可获取提供服务提供方(Server)的代理 (Proxy),并通过此代理读写数据来实现进程间的数据通信。通常,系统能力(System Ability)Server侧会先注册到系统能力管理者(System Ability Manager,缩写SAMgr)中,SAMgr负责管理这些SA并向Client提供相关的接口。Client要和某个具体的SA通信,必须先从SAMgr中获取该SA的代理,然后使用代理和SA通信。

熟悉Android系统或者做过OHOS系统移植的小伙伴,即使没有非常深入理解过Binder,但估计也会经常听到基于Binder驱动的IPC机制。

内核态的Binder驱动实现代码,在 //kernel/linux/linux-5.10/drivers/android/ 目录下。在编译Linux内核时,通过 //kernel/linux/config/linux-5.10/arch/arm(arm64)/configs/ 目录下的 xxxx_defconfig 文件中的如下配置,将Binder驱动模块编译进内核:

## Android#CONFIG_ANDROID=yCONFIG_ANDROID_BINDER_IPC=yCONFIG_ANDROID_BINDER_DEVICES="binder,hwbinder,vndbinder"# CONFIG_ANDROID_BINDER_IPC_SELFTEST is not set# CONFIG_DAX is not setCONFIG_NVMEM=y

Binder驱动在内核向用户态进程提供服务;用户态进程通过 open(“/dev/binder”) 和 ioctl() 来使用Binder服务实现IPC。

这中间其实要经历一些非常复杂的过程,建议小伙伴们自行搜索和理解Binder在内核中实现IPC的原理和细节,我现在还无法保证未来会做这方面的总结。

但本系列文章的下一篇,将会以全景图的形式展示和讲解OHOS的用户态IPC框架的实现流程和细节。

3、基于DBinder驱动的RPC(待定)

DBinder应该是Distributed Binder的缩写。

DBinder将会涉及软总线的一些实现细节,我先看看能理解得多深入,待有所理解后再写总结。

​​想了解更多关于开源的内容,请访问:​​

​​51CTO开源基础软件社区​​

​​https://ost.51cto.com​​。

责任编辑:jianghua 来源:51CTO开源基础软件社区 OHOS代码仓库
标签:rpcRPC

推荐系统

  • 微软Win11原版22H2下载_Win11GHOST 免 激活密钥 22H2正式版64位免费下载

    微软Win11原版22H2下载_Win11GHOST 免 激活密钥 22H2正式版64位免费下载

    语言:中文版系统大小:5.13GB系统类型:Win11

    微软Win11原版22H2下载_Win11GHOST 免 激活密钥 22H2正式版64位免费下载系统在家用办公上跑分表现都是非常优秀,完美的兼容各种硬件和软件,运行环境安全可靠稳定。Win11 64位 Office办公版(免费)优化  1、保留 Edge浏览器。  2、隐藏“操作中心”托盘图标。  3、保留常用组件(微软商店,计算器,图片查看器等)。  5、关闭天气资讯。 

  • Win11 21H2 官方正式版下载_Win11 21H2最新系统免激活下载

    Win11 21H2 官方正式版下载_Win11 21H2最新系统免激活下载

    语言:中文版系统大小:4.75GB系统类型:Win11

    Ghost Win11 21H2是微软在系统方面技术积累雄厚深耕多年,Ghost Win11 21H2系统在家用办公上跑分表现都是非常优秀,完美的兼容各种硬件和软件,运行环境安全可靠稳定。Ghost Win11 21H2是微软最新发布的KB5019961补丁升级而来的最新版的21H2系统,以Windows 11 21H2 22000 1219 专业版为基础进行优化,保持原汁原味,系统流畅稳定,保留常用组件

  • windows11中文版镜像 微软win11正式版简体中文GHOST ISO镜像64位系统下载

    windows11中文版镜像 微软win11正式版简体中文GHOST ISO镜像64位系统下载

    语言:中文版系统大小:5.31GB系统类型:Win11

    windows11中文版镜像 微软win11正式版简体中文GHOST ISO镜像64位系统下载,微软win11发布快大半年了,其中做了很多次补丁和修复一些BUG,比之前的版本有一些功能上的调整,目前已经升级到最新版本的镜像系统,并且优化了自动激活,永久使用。windows11中文版镜像国内镜像下载地址微软windows11正式版镜像 介绍:1、对函数算法进行了一定程度的简化和优化

  • 微软windows11正式版GHOST ISO镜像 win11下载 国内最新版渠道下载

    微软windows11正式版GHOST ISO镜像 win11下载 国内最新版渠道下载

    语言:中文版系统大小:5.31GB系统类型:Win11

    微软windows11正式版GHOST ISO镜像 win11下载 国内最新版渠道下载,微软2022年正式推出了win11系统,很多人迫不及待的要体验,本站提供了最新版的微软Windows11正式版系统下载,微软windows11正式版镜像 是一款功能超级强大的装机系统,是微软方面全新推出的装机系统,这款系统可以通过pe直接的完成安装,对此系统感兴趣,想要使用的用户们就快来下载

  • 微软windows11系统下载 微软原版 Ghost win11 X64 正式版ISO镜像文件

    微软windows11系统下载 微软原版 Ghost win11 X64 正式版ISO镜像文件

    语言:中文版系统大小:0MB系统类型:Win11

    微软Ghost win11 正式版镜像文件是一款由微软方面推出的优秀全新装机系统,这款系统的新功能非常多,用户们能够在这里体验到最富有人性化的设计等,且全新的柔软界面,看起来非常的舒服~微软Ghost win11 正式版镜像文件介绍:1、与各种硬件设备兼容。 更好地完成用户安装并有效地使用。2、稳定使用蓝屏,系统不再兼容,更能享受无缝的系统服务。3、为

  • 雨林木风Windows11专业版 Ghost Win11官方正式版 (22H2) 系统下载

    雨林木风Windows11专业版 Ghost Win11官方正式版 (22H2) 系统下载

    语言:中文版系统大小:4.75GB系统类型:

    雨林木风Windows11专业版 Ghost Win11官方正式版 (22H2) 系统下载在系统方面技术积累雄厚深耕多年,打造了国内重装系统行业的雨林木风品牌,其系统口碑得到许多人认可,积累了广大的用户群体,雨林木风是一款稳定流畅的系统,一直以来都以用户为中心,是由雨林木风团队推出的Windows11国内镜像版,基于国内用户的习惯,做了系统性能的优化,采用了新的系统

  • 雨林木风win7旗舰版系统下载 win7 32位旗舰版 GHOST 免激活镜像ISO

    雨林木风win7旗舰版系统下载 win7 32位旗舰版 GHOST 免激活镜像ISO

    语言:中文版系统大小:5.91GB系统类型:Win7

    雨林木风win7旗舰版系统下载 win7 32位旗舰版 GHOST 免激活镜像ISO在系统方面技术积累雄厚深耕多年,加固了系统安全策略,雨林木风win7旗舰版系统在家用办公上跑分表现都是非常优秀,完美的兼容各种硬件和软件,运行环境安全可靠稳定。win7 32位旗舰装机版 v2019 05能够帮助用户们进行系统的一键安装、快速装机等,系统中的内容全面,能够为广大用户

  • 番茄花园Ghost Win7 x64 SP1稳定装机版2022年7月(64位) 高速下载

    番茄花园Ghost Win7 x64 SP1稳定装机版2022年7月(64位) 高速下载

    语言:中文版系统大小:3.91GB系统类型:Win7

    欢迎使用 番茄花园 Ghost Win7 x64 SP1 2022.07 极速装机版 专业装机版具有更安全、更稳定、更人性化等特点。集成最常用的装机软件,集成最全面的硬件驱动,精心挑选的系统维护工具,加上独有人性化的设计。是电脑城、个人、公司快速装机之首选!拥有此系统