Adding client_server CFMessagePort example

This commit is contained in:
Karmaz95
2024-12-16 14:45:50 +01:00
parent b735706891
commit 043c2714f1
3 changed files with 49 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
CC = clang
FRAMEWORK = -framework Foundation
all: client server
client: client.m
$(CC) $(FRAMEWORK) client.m -o client
server: server.m
$(CC) $(FRAMEWORK) server.m -o server
clean:
rm -f client server *.o
.PHONY: all clean

View File

@@ -0,0 +1,16 @@
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
if (argc != 2) return 1;
CFMessagePortRef port = CFMessagePortCreateRemote(NULL, CFSTR("com.crimson.message_service"));
if (port) {
NSString *msg = [NSString stringWithUTF8String:argv[1]];
NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
CFMessagePortSendRequest(port, 0, (__bridge CFDataRef)data, 1, 1, NULL, NULL);
CFRelease(port);
}
}
return 0;
}

View File

@@ -0,0 +1,18 @@
// server.m
// clang -framework Foundation server.m -o server
#import <Foundation/Foundation.h>
static CFDataRef callback(CFMessagePortRef port, SInt32 msgid, CFDataRef data, void *info) {
NSLog(@"Received: %@", [[NSString alloc] initWithData:(__bridge NSData *)data encoding:NSUTF8StringEncoding]);
return NULL;
}
int main() {
@autoreleasepool {
CFMessagePortRef port = CFMessagePortCreateLocal(NULL, CFSTR("com.crimson.message_service"), callback, NULL, NULL);
CFRunLoopSourceRef source = CFMessagePortCreateRunLoopSource(NULL, port, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
[[NSRunLoop currentRunLoop] run];
}
return 0;
}