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?