chromium通信系统-ipcz系统(四)-ipcz-分层、和mojo的关系以及handle

news/2025/3/26 8:40:03/

在只有mojo的情况下, 进程间通信都是靠unix 域套接字来完成了,由于这种方式比较低效,并且不够灵活,后来引入了ipcz。 但是系统中基本上使用mojo做进程间通信,想要一步到位迁移到ipcz系统是比较困难的。 所以chrome团队采用了一种折中的方法,利用原来mojo的channel进行socket通信,作为控制消息和唤醒机制。 使用ipcz 来实现共享内存和路由机制。另外由于chrome是一个面向多操作系统的任务,对于不同操作系统使用不同的代码实现,这样就要求抽取出操作系统相关的实现。这样使得现有代码非常混乱。另外ipcz系统希望减少耦合,尽量少的暴露实现细节,以及方便序列化,使用handle(句柄)代理不同层的对象。

ipcz主要有四个模块:
1、ipcz上层:third_party/ipcz 目录。 handle 为IpczHandle。 实现Node、NodeLink、RouterLink、Portal、NodeConnector、Router、Parcle 等对象。架空mojo的Node 和Port
2、ipcz driver层:mojo/core/ipcz_driver目录。handle 为 IpczDriverHandle。 实现Transport,ipcz层和mojo层的粘合剂,利用mojo层的通信能力服务ipcz层。序列化传输能力。
3、mojo 层:mojo/core目录。handle 为 MojoHandle。 实现Channel,进程间通信能力。
4、Platform层/mojo/public/cpp/platform目录。 handle 为PlatformHandle。 系统层面的实现,主要是对Socket文件描述的包装。

下面我们具体分析一下每一层handle的实现。

ipcz 层 IpczHandle实现

third_party/ipcz/src/ipcz/api_object.h

class APIObject : public RefCounted {public:enum ObjectType {kNode,kPortal,kBox,kTransport,kParcel,};static APIObject* FromHandle(IpczHandle handle) {return reinterpret_cast<APIObject*>(static_cast<uintptr_t>(handle));}// Takes ownership of an APIObject from an existing `handle`.static Ref<APIObject> TakeFromHandle(IpczHandle handle) {return AdoptRef(reinterpret_cast<APIObject*>(static_cast<uintptr_t>(handle)));}// Returns an IpczHandle which can be used to reference this object. The// reference is not owned by the caller.IpczHandle handle() const { return reinterpret_cast<uintptr_t>(this); }// Releases ownership of a Ref<APIObject> to produce a new IpczHandle which// implicilty owns the released reference.static IpczHandle ReleaseAsHandle(Ref<APIObject> object) {return static_cast<IpczHandle>(reinterpret_cast<uintptr_t>(object.release()));}
......  
}

ipcz层对应的对象基类为APIObject,APIObject 提供四个方法,FromHandle() 和 TakeFromHandle() 方法用于将IpczHandle() 转化为具体对象。handle() 和 ReleaseAsHandle() 方法用于将对象转成IpczHandle句柄。这里我们还可以看到系统里有5种类型的APIObject,分别是:

  • kNode 代表IPCZ Node(节点)对象
  • kPortal 代表ipcz Portal(端口)对象
  • kBox 用于其他可传输的ipcz对象
  • kTransport 代表ipcz Transport(传输点)对象
  • kParcel 代表ipcz Parcel(消息)对象

ipcz driver 层 IpczDriverHandle实现

typedef uintptr_t IpczDriverHandle;

mojo/core/ipcz_driver/object.h

// Common base class for objects managed by Mojo's ipcz driver.
class MOJO_SYSTEM_IMPL_EXPORT ObjectBase: public base::RefCountedThreadSafe<ObjectBase> {public:REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();enum Type : uint32_t {// An ipcz transport endpoint.kTransport,// A wrapped shared memory region.kSharedBuffer,// An active mapping for a shared memory region. These objects are not// serializable and cannot be transmitted over a Transport.kSharedBufferMapping,// A PlatformHandle which can be transmitted as-is by the platform's Channel// implementation, out-of-band from message data. This is the only type of// driver object which can be emitted by the driver's Serialize(), and it's// the only type accepted by its Transmit(). This type is unused on Windows,// where all platform handles are encoded as inline message data during// serialization.kTransmissiblePlatformHandle,// A PlatformHandle which may or may not be transmissible by the platform's// Channel implementation, but which can at least be transformed into// something transmissible during serialization.kWrappedPlatformHandle,// A DataPipe instance used to emulate Mojo data pipes over ipcz portals.kDataPipe,// A MojoTrap instance used to emulate a Mojo trap. These objects are not// serializable and cannot be transmitted over a Transport.kMojoTrap,// An Invitation instance used to emulate Mojo process invitations. These// objects are not serializable and cannot be transmitted over a Transport.kInvitation,};explicit ObjectBase(Type type);Type type() const { return type_; }IpczDriverHandle handle() const {return reinterpret_cast<IpczDriverHandle>(this);}static ObjectBase* FromHandle(IpczDriverHandle handle) {return reinterpret_cast<ObjectBase*>(handle);}static IpczDriverHandle ReleaseAsHandle(scoped_refptr<ObjectBase> object) {return reinterpret_cast<IpczDriverHandle>(object.release());}static scoped_refptr<ObjectBase> TakeFromHandle(IpczDriverHandle handle) {scoped_refptr<ObjectBase> object(FromHandle(handle));if (object) {// We're inheriting a ref previously owned by `handle`, so drop the extra// ref we just added.object->Release();}return object;}......
// Computes the number of bytes and platform handles required to serialize// this object for transmission through `transmitter`. Returns false if the// object cannot be serialized or transmitted as such.virtual bool GetSerializedDimensions(Transport& transmitter,size_t& num_bytes,size_t& num_handles);// Attempts to serialize this object into `data` and `handles` which are// already sufficiently sized according to GetSerializedDimensions(). Returns// false if serialization fails.virtual bool Serialize(Transport& transmitter,base::span<uint8_t> data,base::span<PlatformHandle> handles);

ipcz driver层对应的对象基类为ObjectBase,ObjectBase 提供的方法包括: type方法返回对象的类型,FromHandle() 和 TakeFromHandle() 方法用于将IpczHandle() 转化为具体对象。handle() 和 ReleaseAsHandle() 方法用于将对象转成IpczHandle句柄。GetSerializedDimensions方法用来序列化过程中返回需要的内存空间和hanlde空间。 Serialize函数用于跨进程序列化传输。

这里我们还可以看到系统里有8种类型的ObjectBase,分别是:

  • kTransport 代表传输点对象
  • kSharedBuffer 代表共享内存(端口)对象
  • kSharedBufferMapping 映射后的共享内存对象
  • kTransmissiblePlatformHandle 可传输的PlatformHandle对象
  • kWrappedPlatformHandle 包装的PlatformHandle对象
  • kDataPipe 数据管道
  • kMojoTrap Trap监听对象
  • kInvitation 链接邀请对象。

Mojo层 MojoHandle实现

typedef uintptr_t MojoHandle;

mojo层名没有对应的对象。可以和ipcz driver handle 和 ipcz handle强转。

Platform层 PlatformHandle实现

class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) PlatformHandle {public:enum class Type {kNone,
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_FUCHSIA)kHandle,
#elif BUILDFLAG(IS_APPLE)kMachSend,kMachReceive,
#endif
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)kFd,
#endif};private:Type type_ = Type::kNone;#if BUILDFLAG(IS_WIN)base::win::ScopedHandle handle_;
#elif BUILDFLAG(IS_FUCHSIA)zx::handle handle_;
#elif BUILDFLAG(IS_APPLE)base::mac::ScopedMachSendRight mach_send_;base::mac::ScopedMachReceiveRight mach_receive_;
#endif#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)base::ScopedFD fd_;
#endif
};

对于linux系统, type为kFd, fd_变量保存文件描述符。


http://www.ppmy.cn/news/1264235.html

相关文章

Python可视化(一)——Matplotlib

Matplotlib是一个可视化库&#xff0c;能够用来创建各种各样的图表&#xff0c;可以用于数据科学、量化交易等领域。官网对该工具的介绍为&#xff1a; Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Mat…

点云欧式聚类(基于平面提取基础上进行)

目录 一、相关介绍 二、实现步骤 三、实现代码 四、运行结果

【docker】centos7配置docker镜像加速

国内从 DockerHub 拉取镜像有时会遇到困难&#xff0c;由于网络原因&#xff0c;下载一个Docker官方镜像可能会需要很长的时间&#xff0c;甚至下载失败。此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务。 国内常见镜像仓库 docker官方地址仓…

【自定义Source、Sink】Flink自定义Source、Sink对redis进行读写操作

使用ParameterTool读取配置文件 Flink读取参数的对象 Commons-cli&#xff1a; Apache提供的&#xff0c;需要引入依赖ParameterTool&#xff1a;Flink内置 ParameterTool 比 Commons-cli 使用上简便&#xff1b; ParameterTool能避免Jar包的依赖冲突 建议使用第二种 使用Par…

ssh 公私钥登录

关于 SSH 公私钥登录 SSH 公私钥登录方式是一种基于非对称加密机制的身份验证方法&#xff0c;用于登录 SSH 服务器&#xff0c;可以安全、高效地完成身份验证。 在使用 SSH 公私钥登录方式时&#xff0c;用户需要生成一对公私钥&#xff0c;并将公钥放置在 SSH 服务器上的授…

openEuler学习05-kernel升级

周末没事&#xff0c;尝试下openEuler的kernel升级 [rootlocalhost ~]# more /etc/os-release NAME"openEuler" VERSION"20.03 (LTS-SP3)" ID"openEuler" VERSION_ID"20.03" PRETTY_NAME"openEuler 20.03 (LTS-SP3)" ANSI_…

AWS攻略——创建VPC

文章目录 创建一个可以外网访问的VPCCIDR主路由表DestinationTarget 主网络ACL入站规则出站规则 子网创建EC2测试连接创建互联网网关&#xff08;IGW&#xff09;编辑路由表 知识点参考资料 在 《AWS攻略——VPC初识》一文中&#xff0c;我们在AWS默认的VPC下部署了一台可以SS…

基于lambda简化设计模式

前言 虽说使用设计模式可以让复杂的业务代码变得清晰且易于维护&#xff0c;但是某些情况下&#xff0c;开发可能会遇到我为了简单的业务逻辑去适配设计模式的情况&#xff0c;本文笔者就以四种常见的设计模式为例&#xff0c;演示如何基于lambda来简化设计模式的实现。 策略…