IPC Mechanism to communicate between C++ & Objective C

前端 未结 2 1412
太阳男子
太阳男子 2021-01-01 08:06

I\'m developing a Mac Application in which i need to implement IPC Mechanism. The Scenario is this :

My Application contains two executables, one is Native Mac App(N

相关标签:
2条回答
  • 2021-01-01 08:40

    Unix domain sockets are Great for this. http://www.cs.cf.ac.uk/Dave/C/node28.html

    0 讨论(0)
  • 2021-01-01 09:00

    If you targeting Mac OS X 10.7 and higher you can use XPC using a Mach service connection for your IPC.

    On your server you create the Mach service, set an event handler that accepts new connections and resume the connection:

    xpc_connection_t conn = xpc_connection_create_mach_service( "com.yourname.product.service", dispatch_get_main_queue(), XPC_CONNECTION_MACH_SERVICE_LISTENER );
    xpc_connection_set_event_handler( conn, ^( xpc_object_t client ) {
    
        xpc_connection_set_event_handler( client, ^(xpc_object_t object) {
            NSLog( @"received message: %s", xpc_copy_description( object ) );
    
            xpc_object_t reply = xpc_dictionary_create_reply( object );
            xpc_dictionary_set_string( reply, "reply", "Back from the service" );
    
            xpc_connection_t remote = xpc_dictionary_get_remote_connection( object );
            xpc_connection_send_message( remote, reply );
        } );
    
        xpc_connection_resume( client );
    }) ;
    
    xpc_connection_resume( conn );
    

    I'm assuming that this is running in your Cocoa app which has an event loop. If there is no event loop you need to make sure there is one running (NSRunloop, dispatch_main(), ...)

    In your client you also create an Mach service connection without the XPC_CONNECTION_MACH_SERVICE_LISTENER flag, set an event handler and then resume it. After that you can send messages to your server and receive it's answers:

    xpc_connection_t conn = xpc_connection_create_mach_service( "com.yourname.product.service", NULL, 0 );
    xpc_connection_set_event_handler( conn, ^(xpc_object_t object) {
        NSLog( @"client received event: %s", xpc_copy_description( object ) );
    });
    xpc_connection_resume( conn );
    
    xpc_object_t message = xpc_dictionary_create( NULL, NULL, 0 );
    xpc_dictionary_set_string( message, "message", "hello world" );
    
    xpc_connection_send_message_with_reply( conn, message, dispatch_get_main_queue(), ^(xpc_object_t object) {
        NSLog( @"received reply from service: %s", xpc_copy_description( object ));
    });
    
    dispatch_main();
    

    Note that for this to work your client (probably the command line tool in your case) needs to run an event loop too for this to work. In my example that's the dispatch_main(). This may seem inconvenient at first but it's necessary and well worth it.

    Also note that my example code misses all error handling which really is necessary.

    The XPC APIs are plain C and thus are useable from C, C++ and Objective-C. You only need to use a compiler that supports blocks.

    0 讨论(0)
提交回复
热议问题