Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added NSFileHandle support for XPCKit, with example code in the app/s…

…ervice
  • Loading branch information...
commit 1ce594d925c0f65944ea46fd3ed2f6b3c6f6ff7d 1 parent 1e53fc1
@stevestreza authored
View
3  TestApp/TestAppAppDelegate.h
@@ -22,7 +22,8 @@
@interface TestAppAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
- XPCConnection *connection;
+ XPCConnection *mathConnection;
+ XPCConnection *readConnection;
}
@property (assign) IBOutlet NSWindow *window;
View
37 TestApp/TestAppAppDelegate.m
@@ -28,13 +28,31 @@ @implementation TestAppAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
- connection = [[XPCConnection alloc] initWithServiceName:@"com.mustacheware.TestService"];
- connection.eventHandler = ^(NSDictionary *message, XPCConnection *inConnection){
+ mathConnection = [[XPCConnection alloc] initWithServiceName:@"com.mustacheware.TestService"];
+ mathConnection.eventHandler = ^(NSDictionary *message, XPCConnection *inConnection){
NSNumber *result = [message objectForKey:@"result"];
- NSLog(@"We got a calculation result! %@", result);
+ NSData *data = [message objectForKey:@"data"];
+ NSFileHandle *fileHandle = [message objectForKey:@"fileHandle"];
+ if(result){
+ NSLog(@"We got a calculation result! %@", result);
+ }else if(data || fileHandle){
+ NSData *newData = [fileHandle readDataToEndOfFile];
+ NSLog(@"We got a file handle! Read %i bytes - %@", newData.length, fileHandle);
+ }
};
- NSDictionary *data =
+ readConnection = [[XPCConnection alloc] initWithServiceName:@"com.mustacheware.TestService"];
+ readConnection.eventHandler = ^(NSDictionary *message, XPCConnection *inConnection){
+ NSData *data = [message objectForKey:@"data"];
+ NSFileHandle *fileHandle = [message objectForKey:@"fileHandle"];
+ if(data || fileHandle){
+ NSData *newData = [fileHandle readDataToEndOfFile];
+ NSLog(@"We got a file handle! Read %i bytes - %@", newData.length, fileHandle);
+ }
+ };
+
+
+ NSDictionary *multiplyData =
[NSDictionary dictionaryWithObjectsAndKeys:
@"multiply", @"operation",
[NSArray arrayWithObjects:
@@ -44,7 +62,16 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
nil], @"values",
nil];
- [connection sendMessage:data];
+ NSDictionary *readData = [NSDictionary dictionaryWithObjectsAndKeys:
+ @"read", @"operation",
+ @"/Users/syco/Library/Safari/Bookmarks.plist", @"path",
+ nil];
+ NSData *loadedData = [[NSFileManager defaultManager] contentsAtPath:[readData objectForKey:@"path"]];
+ NSFileHandle *loadedHandle = [NSFileHandle fileHandleForReadingAtPath:[readData objectForKey:@"path"]];
+ NSLog(@"Sandbox is %@ at path %@, got %i bytes and a file handle %@",((loadedData.length == 0 && loadedHandle == nil) ? @"working" : @"NOT working"), [readData objectForKey:@"path"], loadedData.length, loadedHandle);
+
+ [mathConnection sendMessage:multiplyData];
+ [readConnection sendMessage:readData];
}
@end
View
17 TestService/main.m
@@ -24,9 +24,9 @@
int main(int argc, const char *argv[])
{
[XPCService runServiceWithConnectionHandler:^(XPCConnection *connection){
-// [connection _sendLog:@"Multiply received a connection"];
+ [connection _sendLog:@"Multiply received a connection"];
[connection setEventHandler:^(NSDictionary *message, XPCConnection *connection){
-// [connection _sendLog:[NSString stringWithFormat:@"Multiply received a message! %@", message]];
+ [connection _sendLog:[NSString stringWithFormat:@"Multiply received a message! %@", message]];
if([[message objectForKey:@"operation"] isEqual:@"multiply"]){
NSArray *values = [message objectForKey:@"values"];
@@ -38,6 +38,19 @@ int main(int argc, const char *argv[])
[connection sendMessage:[NSDictionary dictionaryWithObject:[NSNumber numberWithDouble:product] forKey:@"result"]];
}
+
+ if([[message objectForKey:@"operation"] isEqual:@"read"]){
+ NSString *path = [message objectForKey:@"path"];
+// NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
+ NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
+
+// [connection _sendLog:[NSString stringWithFormat:@"data %i bytes handle %@",data.length, fileHandle]];
+
+ [connection sendMessage:[NSDictionary dictionaryWithObjectsAndKeys:
+// data, @"data",
+ fileHandle, @"fileHandle",
+ nil]];
+ }
}];
}];
View
16 XPCKit/NSFileHandle+XPCParse.h
@@ -0,0 +1,16 @@
+//
+// NSFileHandle+XPCParse.h
+// XPCKit
+//
+// Created by Steve Streza on 9/10/11.
+// Copyright 2011 Mustacheware. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSFileHandle (XPCParse)
+
++(NSFileHandle *)fileHandleWithXPCObject:(xpc_object_t)xpc;
+-(xpc_object_t)newXPCObject;
+
+@end
View
25 XPCKit/NSFileHandle+XPCParse.m
@@ -0,0 +1,25 @@
+//
+// NSFileHandle+XPCParse.m
+// XPCKit
+//
+// Created by Steve Streza on 9/10/11.
+// Copyright 2011 Mustacheware. All rights reserved.
+//
+
+#import "NSFileHandle+XPCParse.h"
+
+@implementation NSFileHandle (XPCParse)
+
++(NSFileHandle *)fileHandleWithXPCObject:(xpc_object_t)xpc{
+ int fd = xpc_fd_dup(xpc);
+ NSFileHandle *handle = [[[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES] autorelease];
+ return handle;
+}
+
+-(xpc_object_t)newXPCObject{
+ int fd = [self fileDescriptor];
+ xpc_object_t object = xpc_fd_create(fd);
+ return object;
+}
+
+@end
View
2  XPCKit/NSObject+XPCParse.m
@@ -36,6 +36,8 @@ +(id)objectWithXPCObject:(xpc_object_t)xpcObject{
object = [NSString stringWithXPCObject:xpcObject];
}else if(type == XPC_TYPE_UUID){
object = [XPCUUID uuidWithXPCObject:xpcObject];
+ }else if(type == XPC_TYPE_FD){
+ object = [NSFileHandle fileHandleWithXPCObject:xpcObject];
}else if(type == XPC_TYPE_BOOL || type == XPC_TYPE_UINT64 || type == XPC_TYPE_INT64 || XPC_TYPE_DOUBLE){
object = [NSNumber numberWithXPCObject:xpcObject];
}
View
2  XPCKit/XPCExtensions.h
@@ -29,4 +29,6 @@
#import "NSData+XPCParse.h"
#import "NSString+XPCParse.h"
+#import "NSFileHandle+XPCParse.h"
+
#import "XPCUUID.h"
Please sign in to comment.
Something went wrong with that request. Please try again.