From b7357068919ed395160e1d8fe68b58662a19ccc9 Mon Sep 17 00:00:00 2001 From: Karmaz95 Date: Mon, 16 Dec 2024 14:35:30 +0100 Subject: [PATCH] Adding client_server NSMachPort example --- .../client_server_NSMachPort/Makefile | 13 ++++++++ .../client_server_NSMachPort/client.m | 19 ++++++++++++ .../client_server_NSMachPort/server.m | 31 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 X. NU/custom/mach_ipc/client_server_NSMachPort/Makefile create mode 100644 X. NU/custom/mach_ipc/client_server_NSMachPort/client.m create mode 100644 X. NU/custom/mach_ipc/client_server_NSMachPort/server.m diff --git a/X. NU/custom/mach_ipc/client_server_NSMachPort/Makefile b/X. NU/custom/mach_ipc/client_server_NSMachPort/Makefile new file mode 100644 index 0000000..a653ff7 --- /dev/null +++ b/X. NU/custom/mach_ipc/client_server_NSMachPort/Makefile @@ -0,0 +1,13 @@ +CC=clang +FRAMEWORKS=-framework Foundation + +all: server client + +server: server.m + $(CC) $(FRAMEWORKS) server.m -o server + +client: client.m + $(CC) $(FRAMEWORKS) client.m -o client + +clean: + rm -f server client \ No newline at end of file diff --git a/X. NU/custom/mach_ipc/client_server_NSMachPort/client.m b/X. NU/custom/mach_ipc/client_server_NSMachPort/client.m new file mode 100644 index 0000000..124db61 --- /dev/null +++ b/X. NU/custom/mach_ipc/client_server_NSMachPort/client.m @@ -0,0 +1,19 @@ +// client.m +#import +#import + +int main(int argc, char *argv[]) { + @autoreleasepool { + if (argc != 2) return 1; + + mach_port_t bp, port; + task_get_bootstrap_port(mach_task_self(), &bp); + bootstrap_look_up(bp, "com.crimson.message_service", &port); + + NSMachPort *machPort = [[NSMachPort alloc] initWithMachPort:port]; + NSData *data = [[NSString stringWithUTF8String:argv[1]] dataUsingEncoding:NSUTF8StringEncoding]; + NSMutableArray *components = [NSMutableArray arrayWithObject:data]; + [machPort sendBeforeDate:[NSDate date] msgid:0 components:components from:nil reserved:0]; + } + return 0; +} \ No newline at end of file diff --git a/X. NU/custom/mach_ipc/client_server_NSMachPort/server.m b/X. NU/custom/mach_ipc/client_server_NSMachPort/server.m new file mode 100644 index 0000000..a98f904 --- /dev/null +++ b/X. NU/custom/mach_ipc/client_server_NSMachPort/server.m @@ -0,0 +1,31 @@ +// server.m +#import +#import + +@interface Server : NSObject +@end + +@implementation Server +- (void)handlePortMessage:(NSPortMessage *)message { + NSData *data = [[message components] firstObject]; + NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + NSLog(@"Received: %@", msg); +} +@end + +int main() { + @autoreleasepool { + Server *server = [[Server alloc] init]; + mach_port_t bp; + task_get_bootstrap_port(mach_task_self(), &bp); + mach_port_t servicePort; + kern_return_t kr = bootstrap_check_in(bp, "com.crimson.message_service", &servicePort); + if (kr != KERN_SUCCESS) return 1; + + NSMachPort *port = [[NSMachPort alloc] initWithMachPort:servicePort]; + port.delegate = server; + [[NSRunLoop currentRunLoop] addPort:port forMode:NSDefaultRunLoopMode]; + [[NSRunLoop currentRunLoop] run]; + } + return 0; +} \ No newline at end of file