我正面临一个关于通过马赫端口发送/接收消息的奇怪延迟问题。 我实现的基本概念如下:
插件创建本地端口→启动发送的远程进程 消息到所述端口→返回接收的数据。
以下是插件部分:
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());
但结果几乎相同。我不确定我做错了什么。 非常感谢您的帮助。
答案 0 :(得分: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...)
}