XPC 客户端等待服务加载

Posted

技术标签:

【中文标题】XPC 客户端等待服务加载【英文标题】:XPC client wait for service load 【发布时间】:2020-10-27 15:35:41 【问题描述】:

我的安装程序加载了 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()];

【问题讨论】:

【参考方案1】:

您可以尝试如下连接它。这真的很快。

// 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 方法中并用***标记)。在某些后台线程上,重复检查,但如果服务器已启动,则使用适当的循环。然后,您可以发出信号量或使用完成或在服务器启动后执行任何需要完成的操作。您可以在其他地方等待信号量或任何需要的东西。

【讨论】:

这行得通吗?对我来说(在 Big Sur 运行),无论服务是否运行,bootstrap_look_up(bs_port、service_name 和 service_port)总是返回成功 这曾经有效 - 我刚刚再次测试它仍然有效。请记住,示例的编写方式最终会启动服务。

以上是关于XPC 客户端等待服务加载的主要内容,如果未能解决你的问题,请参考以下文章

如何禁用加载/激活 macOS xpc 服务?

哪个处理程序在 macos xpc 中处理来自服务器的回复

ajax学习

Ajax

AJAX & JSON

NSXPCConnection 或 XPCKit