XPC 客户端等待服务加载

XPC client wait for service load

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

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

+(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, 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;

    }
}

// 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_name, & service_port );
    NSLog ( @"%s port %d err %d", service_name, 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 : %@", service_name, 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 : %@", service_name, 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 : %@", service_name, port == MACH_PORT_NULL ? @"NO" : @"YES" );
    }

    return 0;
}

我认为你应该像我在测试中做的那样(在main方法中并用***标记)。在某些后台线程上,如果服务器已启动,请反复检查但要有一个不错的循环。然后您可以发出信号量或使用完成或在服务器启动后做任何需要做的事情。在其他地方,您可以等待信号量或任何需要的东西。