Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions TestApp/TestAppAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
mathConnection = [[XPCConnection alloc] initWithServiceName:@"com.mustacheware.TestService"];
mathConnection.errorHandler = ^(xpc_object_t object, NSString *description, XPCConnection *inConnection) {
if (object == XPC_ERROR_CONNECTION_INTERRUPTED) {
// test by adding abort() or assert() to TestService's main.m file
NSLog(@"XPC_ERROR_CONNECTION_INTERRUPTED: %@", description);
} else if (object == XPC_ERROR_CONNECTION_INVALID) {
// test by using invalid name in initWithServiceName: above
NSLog(@"XPC_ERROR_CONNECTION_INVALID: %@", description);
} else if (object == XPC_ERROR_TERMINATION_IMMINENT) {
// Not really an error, but more like a message that the app/service is terminating AFAIK, not sure how to test it or if it even needs to be handled, but it's here for the sake of completness...
NSLog(@"XPC_ERROR_TERMINATION_IMMINENT: %@", description);
} else {
NSLog(@"ERROR: %@", description);
}
};
mathConnection.eventHandler = ^(NSDictionary *message, XPCConnection *inConnection){
NSNumber *result = [message objectForKey:@"result"];
NSData *data = [message objectForKey:@"data"];
Expand Down
1 change: 1 addition & 0 deletions XPCKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@
1EEDD01F13DD485400D5AEC3 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0420;
ORGANIZATIONNAME = Mustacheware;
};
buildConfigurationList = 1EEDD02213DD485400D5AEC3 /* Build configuration list for PBXProject "XPCKit" */;
Expand Down
4 changes: 4 additions & 0 deletions XPCKit/NSArray+XPCParse.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ +(NSArray *)arrayWithContentsOfXPCObject:(xpc_object_t)object{
}
return true;
});
#if __has_feature(objc_arc)
return [array copy];
#else
return [[array copy] autorelease];
#endif
}

-(xpc_object_t)newXPCObject{
Expand Down
4 changes: 4 additions & 0 deletions XPCKit/NSDictionary+XPCParse.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ +(NSDictionary *)dictionaryWithContentsOfXPCObject:(xpc_object_t)object{
}
return true;
});
#if __has_feature(objc_arc)
return [dict copy];
#else
return [[dict copy] autorelease];
#endif
}

-(xpc_object_t)newXPCObject{
Expand Down
5 changes: 4 additions & 1 deletion XPCKit/NSFileHandle+XPCParse.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ @implementation NSFileHandle (XPCParse)

+(NSFileHandle *)fileHandleWithXPCObject:(xpc_object_t)xpc{
int fd = xpc_fd_dup(xpc);
NSFileHandle *handle = [[[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES] autorelease];
NSFileHandle *handle = [[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES];
#if !__has_feature(objc_arc)
[handle autorelease];
#endif
return handle;
}

Expand Down
1 change: 1 addition & 0 deletions XPCKit/XPCConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- (id)initWithConnection: (xpc_connection_t)connection;

@property (nonatomic, copy) XPCEventHandler eventHandler;
@property (nonatomic, copy) XPCErrorHandler errorHandler;

@property (nonatomic, readonly) xpc_connection_t connection;
@property (nonatomic, assign) dispatch_queue_t dispatchQueue;
Expand Down
23 changes: 20 additions & 3 deletions XPCKit/XPCConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@

#define XPCSendLogMessages 1

@interface XPCConnection ()
- (void)invokeErrorHandlerWithObject:(xpc_object_t)object connection:(XPCConnection *)connection;
@end

#pragma mark -

@implementation XPCConnection

@synthesize eventHandler=_eventHandler, dispatchQueue=_dispatchQueue, connection=_connection;
@synthesize eventHandler=_eventHandler, errorHandler=_errorHandler, dispatchQueue=_dispatchQueue, connection=_connection;

- (id)initWithServiceName:(NSString *)serviceName{
xpc_connection_t connection = xpc_connection_create([serviceName cStringUsingEncoding:NSUTF8StringEncoding], NULL);
Expand All @@ -37,7 +43,9 @@ - (id)initWithServiceName:(NSString *)serviceName{

-(id)initWithConnection:(xpc_connection_t)connection{
if(!connection){
#if !__has_feature(objc_arc)
[self release];
#endif
return nil;
}

Expand All @@ -60,8 +68,9 @@ -(void)dealloc{
xpc_release(_connection);
_connection = NULL;
}

#if !__has_feature(objc_arc)
[super dealloc];
#endif
}

-(void)setDispatchQueue:(dispatch_queue_t)dispatchQueue{
Expand All @@ -81,9 +90,11 @@ -(void)receiveConnection:(xpc_connection_t)connection{
__block XPCConnection *this = self;
xpc_connection_set_event_handler(connection, ^(xpc_object_t object){
if (object == XPC_ERROR_CONNECTION_INTERRUPTED){
if (this.errorHandler) [this invokeErrorHandlerWithObject:object connection:this];
}else if (object == XPC_ERROR_CONNECTION_INVALID){
}else if (object == XPC_ERROR_KEY_DESCRIPTION){
if (this.errorHandler) [this invokeErrorHandlerWithObject:object connection:this];
}else if (object == XPC_ERROR_TERMINATION_IMMINENT){
if (this.errorHandler) [this invokeErrorHandlerWithObject:object connection:this];
}else{
id message = [NSObject objectWithXPCObject: object];

Expand Down Expand Up @@ -119,6 +130,12 @@ -(void)sendMessage:(NSDictionary *)aDictMessage{
});
}

- (void)invokeErrorHandlerWithObject:(xpc_object_t)object connection:(XPCConnection *)connection {
const char *string = xpc_dictionary_get_string(object, XPC_ERROR_KEY_DESCRIPTION);
NSString *description = [NSString stringWithCString:string encoding:NSUTF8StringEncoding];
connection.errorHandler(object, description, connection);
}

-(NSString *)connectionName{
__block char* name = NULL;
dispatch_sync(self.dispatchQueue, ^{
Expand Down
4 changes: 4 additions & 0 deletions XPCKit/XPCService.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
@interface XPCService : NSObject

@property (nonatomic, copy) XPCConnectionHandler connectionHandler;
#if __has_feature(objc_arc)
@property (nonatomic, strong, readonly) NSArray *connections;
#else
@property (nonatomic, readonly) NSArray *connections;
#endif

+(void)runServiceWithConnectionHandler:(XPCConnectionHandler)connectionHandler;
-(id)initWithConnectionHandler:(XPCConnectionHandler)connectionHandler;
Expand Down
6 changes: 6 additions & 0 deletions XPCKit/XPCService.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
static void XPCServiceConnectionHandler(xpc_connection_t handler){
XPCConnection *connection = [[XPCConnection alloc] initWithConnection:handler];
[[NSNotificationCenter defaultCenter] postNotificationName:XPCConnectionReceivedNotification object:connection];
#if !__has_feature(objc_arc)
[connection release];
#endif
}

@implementation XPCService
Expand Down Expand Up @@ -71,7 +73,11 @@ +(void)runServiceWithConnectionHandler:(XPCConnectionHandler)connectionHandler{

[XPCService runService];

#if __has_feature(objc_arc)
(void)service; // get rid of unused variable warning under ARC...
#else
[service release];
#endif
}

@end
1 change: 1 addition & 0 deletions XPCKit/XPCTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

@class XPCConnection;
typedef void (^XPCEventHandler)(NSDictionary *, XPCConnection *);
typedef void (^XPCErrorHandler)(xpc_object_t, NSString *, XPCConnection *);
typedef void (^XPCConnectionHandler)(XPCConnection *);

#pragma mark Notifications
Expand Down
19 changes: 16 additions & 3 deletions XPCKit/XPCUUID.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ @implementation XPCUUID

+(XPCUUID *)uuid{
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
XPCUUID *uuid = [[[self alloc] initWithUUIDRef:uuidRef] autorelease];
XPCUUID *uuid = [[self alloc] initWithUUIDRef:uuidRef];
#if !__has_feature(objc_arc)
[uuid autorelease];
#endif
CFRelease(uuidRef);
return uuid;
}
Expand Down Expand Up @@ -60,7 +63,10 @@ +(XPCUUID *)uuidWithXPCObject:(xpc_object_t)xpc{
#undef CopyByte

CFUUIDRef uuidRef = CFUUIDCreateFromUUIDBytes(NULL, uuidBytes);
XPCUUID *uuid = [[[self alloc] initWithUUIDRef:uuidRef] autorelease];
XPCUUID *uuid = [[self alloc] initWithUUIDRef:uuidRef];
#if !__has_feature(objc_arc)
[uuid autorelease];
#endif

CFRelease(uuidRef);

Expand Down Expand Up @@ -97,7 +103,12 @@ -(xpc_object_t)newXPCObject{
}

-(NSString *)string{
return [((NSString *)CFUUIDCreateString(NULL, self.uuidRef)) autorelease];
CFStringRef result = CFUUIDCreateString(NULL, self.uuidRef);
#if __has_feature(objc_arc)
return (__bridge NSString *)result;
#else
return [((NSString *)result) autorelease];
#endif
}

-(NSString *)description{
Expand Down Expand Up @@ -128,7 +139,9 @@ -(void)dealloc{
_uuidRef = nil;
}

#if !__has_feature(objc_arc)
[super dealloc];
#endif
}

@end