mirror of
https://github.com/Karmaz95/Snake_Apple.git
synced 2026-03-30 14:00:16 +02:00
Adding client_server CFMessagePort example
This commit is contained in:
15
X. NU/custom/mach_ipc/client_server_CFMessagePort/Makefile
Normal file
15
X. NU/custom/mach_ipc/client_server_CFMessagePort/Makefile
Normal 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
|
||||
16
X. NU/custom/mach_ipc/client_server_CFMessagePort/client.m
Normal file
16
X. NU/custom/mach_ipc/client_server_CFMessagePort/client.m
Normal 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;
|
||||
}
|
||||
18
X. NU/custom/mach_ipc/client_server_CFMessagePort/server.m
Normal file
18
X. NU/custom/mach_ipc/client_server_CFMessagePort/server.m
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user