是否可以在进程之间使用CARemoteLayerServer和CARemoteLayerClient? [英] Can CARemoteLayerServer and CARemoteLayerClient be used between processes?

查看:318
本文介绍了是否可以在进程之间使用CARemoteLayerServer和CARemoteLayerClient?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac OS X中,将Lion CARemoteLayerServer和CARemoteLayerClient添加到QuartzCore。我一直在试图调查他们是否适合在多个进程之间分割图形应用程序,但是没有成功。

In Mac OS X Lion CARemoteLayerServer and CARemoteLayerClient were added to QuartzCore. I've been trying to investigate if they'd be suitable for splitting a graphical application between multiple processes, but without success.

我可以在单个进程中成功使用它们,一些代码如下:

I can use them successfully within a single process, with some code along the lines of this:

- (void)buildLayerSingleProcess
{
    CARemoteLayerServer *server = [CARemoteLayerServer sharedServer];

    self.client = [[CARemoteLayerClient alloc] initWithServerPort: server.serverPort];    
    uint32_t clientID = self.client.clientId;    

    CALayer *layer1 = [CALayer layer];
    layer1.bounds = CGRectMake(0.0, 0.0, 100.0, 100.0);

    CGColorRef color = CGColorCreateGenericRGB(0.4, 0.2, 0.3, 1.0);
    [layer1 setBackgroundColor: color];
    CFRelease(color);

    [layer1 setOpacity: 0.75];
    [layer1 setBorderWidth: 5.0f];

    layer1.position = CGPointMake([[self.window contentView] frame].size.width / 2.0, [[self.window contentView] frame ].size.height / 2.0);

    self.client.layer = layer2;

    CALayer *servedLayer = [CALayer layerWithRemoteClientId: self.client.clientId];    
    [[[self.window contentView] layer] addSublayer: servedLayer];
}

这是很好,但我想尝试做类似的事情过程。事实上server.serverPort类型是mach_port_t意味着它适合在进程之间使用。

Which is nice, but I'd like to try and do something similar between processes. The fact that server.serverPort is of type mach_port_t implies that it'd be suitable for use between processes.

但是,当我将该代码拆分为两个进程运行时(两个单独的应用程序,或主进程和XPC服务),当我尝试从服务器mach_port_t初始化CARemoteLayerClient在其他进程中,我得到:

However, when I split that code up to run in two processes (either two individual apps, or a main process and an XPC service), when I try to initialise the CARemoteLayerClient with the mach_port_t from the server in the other process I get:

unable to register with server: 0x10000003

它们不适合在随机过程之间,但是使用mach_port_t kinda意味着。

It may well be they're not suited for between random processes, but the use of mach_port_t kinda implies that.

在进程之间有没有其他人成功使用这些类?

Has anyone else had any success using these classes between processes?

推荐答案

是的,这是可能的。以下是一个示例: https://github.com/krevis/RemoteLayerDemo

Yes, it's possible. Here is an example: https://github.com/krevis/RemoteLayerDemo

运行应用程序,按获取远程层按钮,服务将启动,将给应用程序一个绿色的远程层。 (奇怪的是,它需要几秒钟才出现 - 不知道为什么。)

Run the app, press the "Get Remote Layer" button, and the service will be started and will give the app a green remote layer. (Oddly, it takes several seconds to appear -- no idea why.)

之后,更改颜色按钮发送消息到服务,层的颜色,立即生效,甚至动画。 删除远程层按钮删除图层;

After that, the "Change Color" button sends a message to the service asking it to change the layer's color, which takes effect immediately, and even animates. The "Remove Remote Layer" button removes the layer; the service will then get terminated if you let it idle for a few seconds.

困难的部分是通过 CARemoteLayerServer 的Mach端口, code>。在你的过程中看到的 mach_port_t 只是一个数字;它只在你的过程中有意义。 (与指针相同的想法:指针只是数字,但是不能将指针从一个进程传递到另一个进程,并期望它们指向同一个进程。)

The hard part is passing the Mach port of the CARemoteLayerServer between processes. The mach_port_t that you see in your process is just a number; it only has meaning within your process. (Same idea as a pointer: pointers are just numbers, but you can't pass a pointer from one process to another, and expect them to point to the same thing.)

你会认为XPC可以发送Mach端口,但它不能。这将使这更容易!

You'd think XPC could send Mach ports, but it can't. It would make this a lot easier!

相反,你必须使用Mach API发送底层的Mach端口。在演示中,我在应用程序中使用 bootstrap_register ,在服务中使用 bootstrap_look_up 名称。这不安全,因为在同一个引导上下文中的任何其他应用程序可以找到端口,但它是足够好的演示。实际上,您希望下潜到一些更丑陋的马赫电话

Instead, you have to use Mach API to send the underlying Mach port around. In the demo, I'm using bootstrap_register in the app and and bootstrap_look_up in the service, using an agreed-upon name. This is not secure, because any other app in the same bootstrap context could find the port, but it's good enough for a demo. In reality you'd want to dive down to some uglier Mach calls.

这篇关于是否可以在进程之间使用CARemoteLayerServer和CARemoteLayerClient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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