Handling XPC Communication to Multiple Clients: Is Storing Connections a Reliable Approach?

This is the functionality I am trying to achieve with libxpc: There's one xpc server and two xpc clients. When the xpc server receives a particular dictionary item from clientB, the server needs to send a response to both clientA and clientB.

This is the approach I am currently using: First, clientA creates a dictionary item that indicates that this item is from clientA. Now, clientA sends this dictionary to server. When server receives this item, it stores the connection instance with clientA in a global variable. Next, when clientB sends a particular dictionary item, server uses this global variable where it perviously stored clientA's connection instance to send a response back to clientA, alongside clientB. Only one edge case I can see is that when clientA closes this connection instance, server will be trying to send a response to an invalidated connection.

Question: Is this approach recommended? Any edge cases I should be aware of? Is there any better way to achieve this functionality?

Answered by DTS Engineer in 823552022

I’m not sure I really understanding your question. It sounds like you’re asking whether it’s OK for your XPC server to:

  • Keep a reference to the active connections from all active clients.

  • Send messages on those connections.

  • Filter the connection list by the connection reference [1].

If so, then, yes, all of those are fine.

The one thing to watch out for is clients that fail to read messages. See my comments here.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] I’m not sure which API you’re using, but for the low-level C API that’d be xpc_connection_t and for Foundation API that’d be NSXPCConnection.

Accepted Answer

I’m not sure I really understanding your question. It sounds like you’re asking whether it’s OK for your XPC server to:

  • Keep a reference to the active connections from all active clients.

  • Send messages on those connections.

  • Filter the connection list by the connection reference [1].

If so, then, yes, all of those are fine.

The one thing to watch out for is clients that fail to read messages. See my comments here.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] I’m not sure which API you’re using, but for the low-level C API that’d be xpc_connection_t and for Foundation API that’d be NSXPCConnection.

Yes, I'm using the libxpc api. I just wanted to know if there's a better way to have a two-way communication between client and server other than storing reference for client connection in server.

Handling XPC Communication to Multiple Clients: Is Storing Connections a Reliable Approach?
 
 
Q