如何使用 XPC 将 IOSurface 正确发送到另一个进程?

问题描述

我正在开发一个 MacOS 应用程序,在该应用程序中,我试图将 IOSurface 从 xpc 服务发送到客户端应用程序(与 xpc 服务在同一个项目中),但我显然遗漏了一些东西。我收到以下错误

Exception: Attempt to decode an xpc type but whitelist does not specify an xpc type '<no key>'.

整个错误文本如下:

2021-03-10 12:00:42.014421+0100 My Project[82813:1358040] [xpc.exceptions] 
  <nsxpcconnection: 0x600001aae6c0> connection on anonymousListener or serviceListener from pid 82827: 
  Exception caught during decoding of received selector publishNewFrame:,dropping incoming message.
Exception: Exception while decoding argument 0 (#2 of invocation):
Exception: Attempt to decode an xpc type but whitelist does not specify an xpc type '<no key>'.

我正在使用以下代码发送表面:

guard let connection = processConnection else {
    print("Process is not available")
    return
}

let process = connection.remoteObjectProxyWithErrorHandler {
    error in print("remote proxy error: ",error)
} as! MyProtocol
    
let xpcSurfaceRef = IOSurfaceCreatexpcObject(unsafeBitCast(surface,to: IOSurfaceRef.self))
process.publishNewFrame(xpcSurfaceRef)

协议:

@objc public protocol MyProtocol: NSObjectProtocol {
  func publishNewFrame(_ xpcSurfaceRef: xpc_object_t)
}

在客户端进程上:

func publishNewFrame(_ xpcSurfaceRef: xpc_object_t) {
    let surfaceRef = IOSurfaceLookupFromxpcObject(xpcSurfaceRef)
}

我不知道如何进行。我不清楚我到底需要将什么列入白名单,或者如何去做。原则上,IOSurface 是 xpc 支持的类型,所以我理解它应该可以正常工作。

解决方法

当您初始化连接时,您必须将传入的类列入白名单。

例如:

let classSet: Set<AnyHashable> = [NSArray.self,NSDictionary.self]
connection.remoteObjectInterface?.setClasses( classSet,for: #selector(XpcServiceProtocol.article(pzn:withReply:)),argumentIndex: 0,ofReply: true)

有时找到正确的类有点棘手,因为 Swift 和 Objective-C 类有时不一样。 你当然可以使用你的自定义类

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...