是否可以使用Mac OS X XPC(如IPC)在进程之间交换消息?如何? [英] Is possible to use Mac OS X XPC like IPC to exchange messages between processes? How?

查看:491
本文介绍了是否可以使用Mac OS X XPC(如IPC)在进程之间交换消息?如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Apple的介绍,Lion中引入了新的XPC Services API,它提供了一种轻量级的基本进程间通信机制,该机制已与Grand Central Dispatch(GCD)集成并启动.

According to Apple, the new XPC Services API, introduced in Lion, provides a lightweight mechanism for basic interprocess communication integrated with Grand Central Dispatch (GCD) and launchd.

似乎可以将此API用作一种IPC,例如POSIX IPC,但是,我找不到该怎么做.

It seems possible to use this API as a kind of IPC, like the POSIX IPC, however, I cannot find how to do it.

我试图使用XPC API交流两个进程,以便我可以在它们之间传递消息,但是在服务器端总是会出现"XPC连接无效"错误.

I am trying to communicate two processes using the XPC API so I can pass messages between them but I always get a "XPC connection invalid" error in the server side.

我不需要XPC服务,我只想使用客户端-服务器体系结构交换消息.

I don't want an XPC Service, I just want to exchange messages using a client-server architecture.

我正在使用两个类似BSD的进程,因此没有Info.plist或其他任何东西.

I am using two BSD-like processes, so there is no Info.plist or whatever...

我一直在关注此讨论 http://lists. macosforge.org/pipermail/launchd-dev/2011-November/000982.html ,但是这个主题似乎有点晦涩,没有记载.

I have been following this discussion http://lists.macosforge.org/pipermail/launchd-dev/2011-November/000982.html but this topic seems a bit obscure and undocumented.

谢谢!

推荐答案

是的,这是可能的,但并非您所期望的那样.

Yes, that is possible, but not the way you'd expect.

您可以让(未启动的)流程出售服务.这是出于安全原因,因为这样可以很容易地进行中间人攻击.

You can not have a (non launchd) process vend a service. That is for security reasons, since it would make it easy to do man-in-the-middle attacks.

尽管如此,您仍然可以实现所需的目标:您必须设置一个启动的服务来出售XPC/mach服务.然后,进程A和B都连接到您启动的服务.然后,进程A可以创建一个所谓的匿名连接,并将其发送到启动的服务,该服务将其转发给进程B.一旦发生这种情况,进程A和B可以通过该连接直接相互通信. (即已启动的服务可以在不断开连接的情况下退出).

You can still achieve what you want, though: You have to set up a launchd service that vends an XPC / mach service. Both process A and B then connect to your launchd service. Process A can then create a so called anonymous connection and send that to the launchd service which will forward it to process B. Once that has happened, processes A and B can talk to each other directly through that connection (i.e. the launchd service can exit without the connection breaking).

这似乎是绕来绕去的,但出于安全原因是必须的.

This may seem round-about, but it's necessary for security reasons.

有关匿名连接的详细信息,请参见xpc_object(3)手册页.

See the xpc_object(3) man page for details about anonymous connections.

这有点反直观,因为进程A将使用xpc_connection_create()创建一个 listener 对象.然后,A使用xpc_endpoint_create()从侦听器创建一个 endpoint 对象,并将该终结点通过电线(通过XPC)发送给进程B.然后B可以将该对象转变为与xpc_connection_create_from_endpoint()的连接.然后,A的 listener 的事件处理程序将收到一个与B用xpc_connection_create_from_endpoint()创建的连接匹配的连接对象.类似于客户端连接时xpc_connection_create_mach_service()事件处理程序将接收连接对象的方式.

It's a bit counter intuitive, because process A will create a listener object with xpc_connection_create(). A then creates an endpoint object from the listener with xpc_endpoint_create() and sends that endpoint across the wire (over XPC) to process B. B can then turn that object into a connection with xpc_connection_create_from_endpoint(). A's event handler for the listener will then receive a connection object matching the connection that B created with xpc_connection_create_from_endpoint(). This works similar to the way that the event handler of xpc_connection_create_mach_service() will receive connection objects when clients connect.

这篇关于是否可以使用Mac OS X XPC(如IPC)在进程之间交换消息?如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆