使用 NSProxy 和forwardInitation:
我想编写一个代理,通过 TCP 将方法调用转发到另一个对象,而无需 NSConnection
和 NSDistanceObject
内容。 我想要的是我自己的协议。
问题在于,子类化 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@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 whatNSDistantObject
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 atNSConnection
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 fromNSDistantObject
.NSDistantObject 处理此问题的方式是通过其 setProtocolForProxy: 方法,该方法指定一个协议,其中包含连接另一端的对象处理的方法列表。 然后,它可以在转发时使用这些方法的信息来提供适当的 NSMethodSignature 对象。
看起来最简单的方法是通过
中声明的protocol_getMethodDescription()
函数。 这将返回一个 objc_method_description 结构,其中包含一个 types 字段。 我相信您应该能够将该值传递到+[NSMethodSignaturesignatureWithObjCTypes:]
中以获取与协议中声明的内容相匹配的方法签名对象。The way
NSDistantObject
handles this is by itssetProtocolForProxy:
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 appropriateNSMethodSignature
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 anobjc_method_description
struct which contains atypes
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.我找到了一个智能解决方案来生成
NSInspiration
对象。I found a smart solution to generate
NSInvocation
objects.