Can CAREMoteLayerServer and CARemoteLayerClient be used between processes?

On Mac OS X Lion, CARemoteLayerServer and CARemoteLayerClient were added to QuartzCore. I tried to find out if they are suitable for sharing a graphical application between several processes, but without success.

I can use them successfully as part of a single process with some code in the lines:

- (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]; } 

This is good, but I would like to try and do something similar between processes. The fact that server.serverPort is of type mach_port_t implies that it is suitable for use between processes.

However, when I split this code into two processes (two separate applications or the main process and the XPC service), when I try to initialize CARemoteLayerClient using mach_port_t from the server in another process, we get:

 unable to register with server: 0x10000003 

They may not be suitable for random processes, but using mach_port_t kinda implies that.

Has anyone else managed to use these classes between processes?

+4
source share
1 answer

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

Launch the application, click the "Get Remote Layer" button, and the service will start and provide the application with a green remote layer. (Oddly enough, it takes a few seconds - I don't know why.)

After that, the Change Color button sends a message to the service, demanding to change the color of the layer, which takes effect immediately and even enlivens. The "Delete deleted layer" button deletes the layer; the service will be terminated if you miss it for a few seconds.

The hard part passes the CARemoteLayerServer Mach CARemoteLayerServer between processes. mach_port_t that you see in your process is just a number; it only makes sense in your process. (Same idea as a pointer: pointers are just numbers, but you cannot pass a pointer from one process to another and expect them to point to the same thing.)

You would think that XPC can send Mach ports, but that is not possible. That would make it a lot easier!

Instead, you should use the Mach API to send the base port of Mach. In the demo, I use bootstrap_register in the application and bootstrap_look_up in the service using a consistent name. This is not safe, because any other application in the same bootstrap context can find a port, but it is good enough for demonstration. In fact, you'd like to dive into some of the uglier Mach challenges .

+4
source

Source: https://habr.com/ru/post/1396615/


All Articles