使用 NSProxy 和forwardInitation:

发布于 2024-07-22 12:05:29 字数 361 浏览 2 评论 0原文

我想编写一个代理,通过 TCP 将方法调用转发到另一个对象,而无需 NSConnectionNSDistanceObject 内容。 我想要的是我自己的协议。

问题在于,子类化 NSProxy 并重写 forwardInitation: 是不够的。 我还必须覆盖 methodSignatureForSelector

这是我的问题:

– (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
    return ??? 
}

I would like to code a proxy that forwards method invocations to another object over TCP without NSConnection and NSDistanceObject stuff. What I want is my own protocol.

The problem is that subclassing NSProxy and overriding forwardInvocation: is not sufficient. I have also to override methodSignatureForSelector

Here is my question:

– (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
    return ??? 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲凉≈ 2024-07-29 12:05:29

@Brian,这可能没问题,但 setProtocolForProxy: 只是为了优化。 cocoafan 可以在他的解决方案中强制使用它,但它不会是一个直接的替代品。 您应该能够自动获得签名。 为此,网络协议需要一种请求签名的方法。

我相信这里的解决方案是在网络协议中包含一个“请求方法签名”方法,并让它在远程对象上运行 methodSignatureForSelector: 并编码并返回结果。 这就是 NSDistantObject 的作用。

如果您要经常喋喋不休,那么提供 setProtocolForProxy: 是一项重要的优化,但它确实将您限制为您拥有协议的对象,并且在 10.5 之前引入了一些烦人的限制(所有方法是必要的)。 即使在 10.5 上,如果它是获取方法签名的唯一方法,也可能会引入一些有问题的限制。

@cocoafan,我认为您在 NSProxy 之上从头开始创建它可能是正确的,但请查看 NSConnection 并看看是否可以将其子类化以您想要的方式管理网络连接。 如果您能找到一种方法来做到这一点(尽管我没有立即看到一种简单的方法),您可能会从 NSDistantObject 免费获得很多东西。

@Brian, This may be ok, but setProtocolForProxy: is just for optimization. cocoafan could mandate it for his solution, but it wouldn't be a drop-in replacement. You're supposed to be able to get signatures automatically. To do that, the network protocol needs a way to request the signature.

I believe the solution here is to include in the network protocol a "request method signature" method, and have it run methodSignatureForSelector: on the distant object and encode and return the result. That's what NSDistantObject does.

Providing setProtocolForProxy: is an important optimization if you're going to chatter a lot, but it does restrict you to objects that you have a protocol for, and prior to 10.5 that introduces some annoying limitations (all methods are required). Even on 10.5, it may introduce some problematic limitations if it's the only way to get method signatures.

@cocoafan, I think you're probably right to create this from scratch on top of NSProxy, but do take a look at NSConnection and see if you can sub-class it to manage the network connection the way you want. If you can find a way to do that (though I don't see an easy way right off), you'll probably get a lot of stuff for free from NSDistantObject.

一身骄傲 2024-07-29 12:05:29

NSDistantObject 处理此问题的方式是通过其 setProtocolForProxy: 方法,该方法指定一个协议,其中包含连接另一端的对象处理的方法列表。 然后,它可以在转发时使用这些方法的信息来提供适当的 NSMethodSignature 对象。

看起来最简单的方法是通过 中声明的 protocol_getMethodDescription() 函数。 这将返回一个 objc_method_description 结构,其中包含一个 types 字段。 我相信您应该能够将该值传递到 +[NSMethodSignaturesignatureWithObjCTypes:] 中以获取与协议中声明的内容相匹配的方法签名对象。

The way NSDistantObject handles this is by its setProtocolForProxy: method, which specifies a protocol containing the list of methods that the object on the other end of the connection handles. It can then use the information for these methods to provide appropriate NSMethodSignature objects when doing forwarding.

It looks like the most straightforward to do that is via the protocol_getMethodDescription() function, declared in <objc/runtime.h>. This returns an objc_method_description struct which contains a types field. I believe you should be able to pass that value into +[NSMethodSignature signatureWithObjCTypes:] to get a method signature object that matches what's declared in the protocol.

你另情深 2024-07-29 12:05:29

我找到了一个智能解决方案来生成NSInspiration 对象。

I found a smart solution to generate NSInvocation objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文