我正在编写一个Cocoa应用程序,它需要执行一个UNIX程序并逐行读取它的输出,因为它们是在生成的。我成立了一个NSTask和NSPipe这样:NSTask/NSPipe从Unix命令中读取
task = [[NSTask alloc] init];
pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
//... later ...
[task setArguments:...];
[task setLaunchPath:@"..."];
[task launch];
handle = [[task fileHandleForReading] retain];
的命令不会终止,直到程序告诉它与[task terminate]
这样做。我已经尝试了几种从句柄中读取的方法,例如-readInBackgroundAndNotify
,while([(data = [handle availableData]) length] > 0)
和-waitForDataInBackgroundAndNotify
,但是管道似乎不会产生任何数据。有什么方法可以“戳”NSTask
或NSPipe
以刷新数据?
EDIT:与-readInBackgroundAndNotify
:
[handle readInBackgroundAndNotify];
notification_block_t handlerBlock =
^(NSNotification *notification) {
NSData *data = [[notification userInfo]
objectForKey: NSFileHandleNotificationDataItem];
/*... do stuff ...*/
[self addNotification: handle block: handlerBlock];
};
[self addNotification: handler block: handlerBlock];
//...
- (void)addNotification:(id)handle block:(notification_block_t)block {
[[NSNotificationCenter defaultCenter]
addObserverForName: NSFileHandleReadCompletionNotification
object: handle
queue: [NSOperationQueue mainQueue]
usingBlock: block];
}
与-waitForDataInBackgroundAndNotify
:
[handle waitForDataInBackgroundAndNotify];
notification_block_t handlerBlock =
^(NSNotification *notification) {
NSData *data = [handle availableData];
/*... do stuff ...*/
};
[self addNotification: handler block: handlerBlock];
与while
循环:
[self startProcessingThread: handle];
//...
- (void)startProcessingThread:(NSFileHandle *)handle {
[[NSOperationQueue mainQueue]
addOperation: [[[NSInvocationOperation alloc]
initWithTarget: self
selector: @selector(dataLoop:)
object: handle] autorelease]];
}
- (void)dataLoop:(NSFileHandle *)handle {
NSData *data;
while([(data = [handle availableData]) length] > 0) {
/*... do stuff ...*/
}
}
编辑2:该参数设置如下(该命令tshark
):
NSArray *cmd = [NSArray arrayWithObjects:@"-R", @"http.request",
@"-Tfields", @"-Eseparator='|'",
@"-ehttp.host", @"-ehttp.request.method",
@"-ehttp.request.uri", nil];
cmd = [[cmd arrayByAddingObjectsFromArray:[self.ports map:^(id arg1, NSUInteger idx) {
return [NSString stringWithFormat:@"-d tcp.port==%d,http", [arg1 intValue]];
}]]
arrayByAddingObject:[@"dst " stringByAppendingString:
[self.hosts componentsJoinedByString:@" or dst "]]];
[self.tsharktask setArguments:cmd];
发布您的所有代码(例如您如何定义NSFileHandleDataAvailableNotification)。 –
'typedef(^ notification_block_t)(NSNotification *);',当然。 – Actorclavilis