XPC客户端等待服务负载

问题描述

我的安装程序加载了xpc服务和xpc客户端,它们试图在该服务上调用远程xpc方法

但是,该服务可以任意加载,并且由于该服务尚未加载,客户端可能会获得无效的连接。到目前为止,我还没有找到任何获取服务负载指示的方法,因此我在连接invalidationHandler上递归调用retry方法。这是正确的方法吗?有什么我可以等待的服务事件吗?

+(void) callxpcWithRetry {
    nsxpcconnection* hubConnection = [[nsxpcconnection alloc] initWithMachServiceName:@"com.bla.myservice" options:0];
    hubConnection.remoteObjectInterface = getInterface();
    [hubConnection setInvalidationHandler:^{
        // No one is listening. connection cannot be established
        NSLog(@"Connection to keystore hub service invalidated .. retry in 5");

        dispatch_after(dispatch_time(disPATCH_TIME_Now,5 * NSEC_PER_SEC),dispatch_get_main_queue(),^{
            [ServiceDelegate registerBiometricKeyStoreRetry];
        });
    }];
    
    [hubConnection resume];
    id<myxpcProtocol> hub = [hubConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
    }];
    
    [hub xpcProtocolMethodForUser:NSUserName()];
}

解决方法

您可以按以下方式尝试连接到它。这是真的很快。

// Connect to the service,this also checks to see if it is available
mach_port_t connect_to_service ( const char * service_name )
{
    mach_port_t bs_port,service_port;
    kern_return_t err;

    task_get_bootstrap_port ( mach_task_self (),& bs_port );
    err = bootstrap_look_up ( bs_port,service_name,& service_port );

    if ( err == KERN_SUCCESS )
    {
        return service_port;

    }
    else
    {
        return MACH_PORT_NULL;

    }
}

这是我使用的完整测试。

#import <Foundation/Foundation.h>
#import <bootstrap.h>

// Connect to the service,& service_port );

    if ( err == KERN_SUCCESS )
    {
        return service_port;

    }
    else
    {
        return MACH_PORT_NULL;

    }
}

// Start the service
start_service ( const char * service_name,dispatch_semaphore_t s )
{
    // Simulate delay
    [NSThread sleepForTimeInterval:10];

    mach_port_t service_port = MACH_PORT_NULL;
    kern_return_t err;

    err = bootstrap_check_in ( bootstrap_port,& service_port );
    NSLog ( @"%s port %d err %d",service_port,err );

    NSLog ( @"Server up and running - signal semaphore" );
    dispatch_semaphore_signal ( s );

    // Simulate running server
    while ( YES )
    {
        [NSThread sleepForTimeInterval:60];
    }
}

int main(int argc,const char * argv[])
{
    @autoreleasepool
    {
        // insert code here...
        NSLog(@"Hello,World!");

        // Configuration
        char * service_name = "com.hopla.test";
        mach_port_t port;

        // Check it
        NSLog ( @"Connecting to %s that is down",service_name );
        port = connect_to_service ( service_name );
        NSLog ( @"%s available : %@",port == MACH_PORT_NULL ? @"NO" : @"YES" );

        // The semaphore fires when the server is up,but is not used here
        dispatch_semaphore_t s = dispatch_semaphore_create ( 0 );

        // Starting service on background thread
        dispatch_async( dispatch_queue_create( "bak",dispatch_queue_attr_make_with_qos_class( DISPATCH_QUEUE_CONCURRENT,QOS_CLASS_DEFAULT,DISPATCH_QUEUE_PRIORITY_DEFAULT ) ),^ {

            NSLog ( @"Starting %s",service_name );
            start_service ( service_name,s );
        } );

        // Repeatedly checks until the service is up
        // *** do something like in this loop ***
        while ( YES )
        {
            NSLog ( @"Connecting to %s that may be up",service_name );
            port = connect_to_service ( service_name );
            NSLog ( @"%s available : %@",port == MACH_PORT_NULL ? @"NO" : @"YES" );

            if ( port == MACH_PORT_NULL )
            {
                [NSThread sleepForTimeInterval:5];
            }
            else
            {
                break;
            }
        }

        // Final test - should be up
        port = connect_to_service ( service_name );
        NSLog ( @"%s fin available : %@",port == MACH_PORT_NULL ? @"NO" : @"YES" );
    }

    return 0;
}

我认为您应该像在测试中那样做(使用main方法并标记为***)。在某些后台线程上,重复检查一次,但要进行循环检查,以确定服务器是否已启动。然后,可以在服务器启动后发出信号或使用完成信号或执行任何需要完成的操作。在其他地方,您可以等待信号量或所需的任何内容。

相关问答

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