1

I am from a C/C++, Python, Javascript background and recently started learning development on MacOS using Swift.

I want simple IPC(Inter Process Communication) between two processes, let's call them server process and client process.

I could not find any simple example showing use of XPC for IPC. Let me try to put my thinking and what I am trying to build.

Suppose I have server_process.swift

// This is XPC Server

import Foundation

func run_server_loop() {
    // I guess I have to use NSXPCListener e.g. XPC Service
}

func on_message(message) {
    // I have received message from client_server.swift
    // do whatever can be done with this message
    // send reply to client_server.swift

    if(message == "ping") {
        send("pong")
    } else {
        send("command not supported")
    }
}

// run loop to start listener
run_server_loop()

I would need client_server.swift

import Foundation

// This is XPC Client

func some_way_to_connect() {
    // some way to connect to server process
}

// connect to server which is running on server_process.swift
client = some_way_to_connect()

// send a message to server
client.send("ping")

I put my thoughts in pseudo code.

How can I achieve such communication using XPC using Swift?

Alok
  • 7,734
  • 8
  • 55
  • 100
  • 1
    Technically speaking, XPC *only* does IPC ^_^ , typically between a main app some helper service, whose lifecycle is managed by XPC. I think what you mean is that you would like to use it for some kind of peer-to-peer communication between existing processes of yours, is that correct? – Alexander Sep 23 '21 at 12:29
  • @Alexander: Yes you are right I could have right `using XPC` instead of `using IPC using XPC`. but I wrote intentionally to keep things more informative. Someone who dont know `XPC` will get to know that its related to IPC :-) – Alok Sep 23 '21 at 13:19
  • @Alexander: My real requirement is this https://stackoverflow.com/questions/69233948/how-to-send-a-string-from-a-application-to-dal-plugin-using-ipc-on-macos – Alok Sep 23 '21 at 13:20
  • 1
    Oh I've seen that question before, I already had it up-voted :). From what I can tell, XPC is catered exclusively to a client-server style of communication, where a large part of its "value add " is to manage the life cycle of an XPC or Mach service for you. I don't know if that's compatible with a `DAL plugin` (device abstraction layer plugin? I don't know what this is, and I couldn't find any docs on this), where I suspect the DAL plugin's life-cycle is managed for you. IIRC, XPC is quite similar to CFMessagePort, which is suited for more generalized IPC. – Alexander Sep 23 '21 at 13:56
  • 1
    Preface: I'm a dummy who doesn't know much about most of these things, so verify any information I give that's operative to any design decisions you make, lol – Alexander Sep 23 '21 at 13:57
  • @Alexander: Thank you so much for writing whatever you could :-) You can see this https://developer.apple.com/library/archive/samplecode/CoreMediaIO/Introduction/Intro.html – Alok Sep 23 '21 at 16:56

0 Answers0