mirror of
https://github.com/Karmaz95/Snake_Apple.git
synced 2026-03-30 14:00:16 +02:00
Adding client_server NSMachPort example
This commit is contained in:
13
X. NU/custom/mach_ipc/client_server_NSMachPort/Makefile
Normal file
13
X. NU/custom/mach_ipc/client_server_NSMachPort/Makefile
Normal file
@@ -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
|
||||
19
X. NU/custom/mach_ipc/client_server_NSMachPort/client.m
Normal file
19
X. NU/custom/mach_ipc/client_server_NSMachPort/client.m
Normal file
@@ -0,0 +1,19 @@
|
||||
// client.m
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <servers/bootstrap.h>
|
||||
|
||||
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;
|
||||
}
|
||||
31
X. NU/custom/mach_ipc/client_server_NSMachPort/server.m
Normal file
31
X. NU/custom/mach_ipc/client_server_NSMachPort/server.m
Normal file
@@ -0,0 +1,31 @@
|
||||
// server.m
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <servers/bootstrap.h>
|
||||
|
||||
@interface Server : NSObject <NSMachPortDelegate>
|
||||
@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;
|
||||
}
|
||||
Reference in New Issue
Block a user