检测/修复 NSConnection 故障

发布于 2024-08-28 16:57:14 字数 559 浏览 5 评论 0原文

我想使用 NSConnection/NSDistributedObject 进行进程间通信。我希望客户端能够处理服务器只能偶尔访问的情况。

如何确定向 NSConnection 发送消息是否会失败或已经失败?目前,如果我的服务器(出售远程对象的进程)死机,客户端向远程对象发送选择器时将崩溃

理想情况下,我希望有一个远程对象的包装器,它可以延迟实例化(或重新实例化)连接,并在无法实例化连接或连接失败的情况下返回默认值。我真的不知道使用 Objective C 执行此操作的正确方法。

这是代表此逻辑的一些伪代码:

if myConnection is null:
    instantiate myConnection
    if MyConnection is null:
        return defaultValue

    try
        return [myConnection someMethod]
    catch
        myConnection = null
        return defaultValue

I would like to use NSConnection/NSDistributedObject for interprocess communication. I would like the client to be able to handle the case where the server is only occasionally reachable.

How can I determine if sending a message to the NSConnection will fail or has failed? Currently if my server (the process that has vended the remote object) dies, the client will crash if it sends a selector to the remote object.

Ideally I'd like to have a wrapper for the remote object that can lazily instantiate (or reinstantiate) the connection, and return a default value in the case where the connection could not be instantiated, or the connection has failed. I don't really know the correct way to do this using objective c.

Here's some pseudocode representing this logic:

if myConnection is null:
    instantiate myConnection
    if MyConnection is null:
        return defaultValue

    try
        return [myConnection someMethod]
    catch
        myConnection = null
        return defaultValue

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

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

发布评论

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

评论(2

梦里人 2024-09-04 16:57:14

不幸的是,检测连接失败的唯一方法是使用异常处理程序,因为没有可靠的方法来“询问”远程对象连接是否仍然有效。值得庆幸的是,这很简单:

//get the distributed object
id <YourDOProtocol> remoteObject = (id <YourDOProtocol>)[NSConnection rootProxyForConnectionWithRegisteredName:@"YourRegisteredName" host:yourHost];

//call a method on the distributed object
@try
{
    NSString* response = [remoteObject responseMethod];
    //do something with response
}
@catch(NSException* e)
{
    //the receiver is invalid, which occurs if the connection cannot be made
    //handle error here
}

Unfortunately the only way to detect a connection failure is to use an exception handler, as there is no reliable way to "ask" a remote object if the connection is still valid. Thankfully, this is simple:

//get the distributed object
id <YourDOProtocol> remoteObject = (id <YourDOProtocol>)[NSConnection rootProxyForConnectionWithRegisteredName:@"YourRegisteredName" host:yourHost];

//call a method on the distributed object
@try
{
    NSString* response = [remoteObject responseMethod];
    //do something with response
}
@catch(NSException* e)
{
    //the receiver is invalid, which occurs if the connection cannot be made
    //handle error here
}
小嗲 2024-09-04 16:57:14

如果您的服务器优雅地退出,我的理解是,它会在连接关闭时发布 NSConnectionDidDieNotification ,以便您可以像这样注册您的客户端:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectionDidDie:) name:NSConnectionDidDieNotification object:remoteObject];

也许您的 connectionDidDie:< /code>method 可以设置一个布尔变量,您可以在尝试发送消息之前检查该变量。

您的 DO 可以发布一条通知,表示它已启动(尽管我认为也有系统消息,但我才刚刚开始了解 DO),您也可以类似地注册以获取其启动的通知。

我想 Rob 的回答肯定是“包罗万象”,您无需担心通知中心未及时连接到服务器。

我一直在我的第一个 DO 应用程序中使用“死亡”通知,希望它对您有所帮助。

托德。

If your server is quiting gracefully then, I'm of the understanding, that it'll post an NSConnectionDidDieNotification as it's connection closes so you could register your client like this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectionDidDie:) name:NSConnectionDidDieNotification object:remoteObject];

Perhaps your connectionDidDie:method could set a Boolean var that you can check prior to attempting sending the message.

Your DO could post a notification to say that it's started (although I think there are also system messages for that but I've only just started learning about DO's) and you could similarly register to be notified of it's startup.

I guess Rob's answer is a definite 'catch-all' and you wouldn't need to worry about the notification centre having not got through to the server in time.

I've been using the 'did die' notification it in my first DO app and I hope it helps you.

Todd.

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