Mojo C++ Platform API
Mojo C++ Platform API
This document is a subset of the Mojo documentation.
Overview
The Mojo C++ Platform API provides a lightweight set of abstractions around stable platform primitive APIs like UNIX domain sockets and Windows named pipes. This API is primarily useful in conjunction with Mojo Invitations to bootstrap Mojo IPC between two processes.
Platform Handles
The PlatformHandle
type provides a move-only wrapper around an owned, platform-specific primitive handle types. The type of primitive it holds can be any of the following:
- Windows HANDLE (Windows only)
- Fuchsia zx_handle_t (Fuchsia only)
- Mach send right (OSX only)
- POSIX file descriptor (POSIX systems only)
See the header for more details.
Platform Channels
The PlatformChannel
type abstracts a platform-specific IPC FIFO primitive primarily for use with the Mojo Invitations API. Constructing a PlatformChannel
instance creates the underlying system primitive with two transferrable PlatformHandle
instances, each thinly wrapped as a PlatformChannelEndpoint
for additional type-safety. One endpoint is designated as local and the other remote, the intention being that the remote endpoint will be transferred to another process in the system.
See the header for more details. See the Invitations documentation for an example of using PlatformChannel
with an invitation to bootstrap IPC between a process and one of its newly launched child processes.
Named Platform Channels
For cases where it is not feasible to transfer a PlatformHandle
from one running process to another, the Platform API also provides NamedPlatformChannel
, which abstracts a named system resource that can facilitate communication similarly to PlatformChannel
.
A NamedPlatformChannel
upon construction will begin listening on a platform-specific primitive (a named pipe server on Windows, a domain socket server on POSIX, etc.). The globally reachable name of the server (e.g. the socket path) can be specified at construction time via NamedPlatformChannel::Options::server_name
, but if no name is given, a suitably random one is generated and used.
// In one process
mojo::NamedPlatformChannel::Options options;
mojo::NamedPlatformChannel named_channel(options);
OutgoingInvitation::Send(std::move(invitation),
named_channel.TakeServerEndpoint());
SendServerNameToRemoteProcessSomehow(named_channel.GetServerName());
// In the other process
void OnGotServerName(const mojo::NamedPlatformChannel::ServerName& name) {
// Connect to the server.
mojo::PlatformChannelEndpoint endpoint =
mojo::NamedPlatformChannel::ConnectToServer(name);
// Proceed normally with invitation acceptance.
auto invitation = mojo::IncomingInvitation::Accept(std::move(endpoint));
// ...
}
posted on 2018-10-16 20:23 huangguanyuan 阅读(1501) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~