1

关于通过马赫端口发送/接收消息,我面临一个奇怪的延迟问题。我的实现的基本概念如下:

插件创建本地端口→启动向所述端口发送消息的远程进程→返回接收到的数据。

这是插件部分:

static NSArray *returned=nil;
static CFDataRef handle_port (
   CFMessagePortRef local,
   SInt32 msgid,
   CFDataRef d,
   void *info
) {
    NSPropertyListFormat format;
    NSDictionary* ret = [NSPropertyListSerialization propertyListWithData:(NSData*)d
                                            options: NSPropertyListImmutable
                                            format: &format
                                            error: nil];
    returned=[NSArray arrayWithArray:[ret objectForKey:@"aKey"]]; //this is what I want returned from the portRet()
    NSLog(@"returned array %@",returned); 
    return NULL;     
}

NSArray* portRet(){
    CFMessagePortRef port = CFMessagePortCreateLocal(kCFAllocatorDefault, CFSTR("com.someport"), handle_port, NULL, NULL);
    CFRunLoopSourceRef source =  CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, port, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
    CFRelease(source);
    int r=system("/path/someExecutable"); 
    if(r !=0) NSLog(@"Program error");
    //CFMessagePortInvalidate(port);    
    //CFRelease(port);
    return returned; // always returns nil
}

someExecutable 的代码的重要部分如下:

int main(){
...
CFMessagePortRef port = CFMessagePortCreateRemote(NULL, CFSTR("com.someport"));
    if(port == NULL) exit(1);
    CFDataRef d=CFPropertyListCreateData(kCFAllocatorDefault,[NSDictionary dictionaryWithObject:anArray forKey:@"aKey"], kCFPropertyListXMLFormat_v1_0, 0, NULL);
    CFMessagePortSendRequest (port, 0, d, 0, 0, NULL, NULL);
    NSLog(@"Program is about to exit");
    CFRelease(d);
...
    exit(0);
}

来自远程进程的消息被优雅地发送,但在进程结束并返回空值调用回调。portRet()如果我使函数内的端口无效portRet(),则永远不会收到消息。

我无法弄清楚发生这种延迟的原因。我想要实现的是在portRet()返回之前调用端口回调。我还尝试使用主调度队列而不是CFRunLoopSource端口的回调调度:

 CFMessagePortSetDispatchQueue(port, dispatch_get_main_queue());

但结果几乎是一样的。我不确定我做错了什么。非常感谢您的帮助。

4

1 回答 1

1

您需要运行您的运行循环,portRet直到第二个进程返回一个值。例如:

SInt32 runLoopRunReturnValue = CFRunLoopRunInMode(kCFRunLoopDefaultMode, CFDateGetTimeIntervalSinceDate((__bridge CFDateRef)[NSDate distantFuture], (__bridge CFDateRef)[NSDate date]), true);

if (runLoopRunReturnValue == kCFRunLoopRunHandledSource)
    return returned;
else {
    // Throw exception or whatever
    // (although this will never be called using the above implementation
    // since [NSDate distantFuture] is wayy into the future...)
}
于 2013-03-29T03:43:33.343 回答