Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions Sources/SecureXPC/Server/XPCServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -448,13 +448,33 @@ public class XPCServer {
// concurrent. (Although if an API user sets the target queue to be serial that's supported too.)
self.handlerQueue.async {
XPCServer.ClientIdentity.setForCurrentThread(connection: connection, message: message) {
var reply = handler.shouldCreateReply ? xpc_dictionary_create_reply(message) : nil
var reply = handler.shouldCreateReply ? xpc_dictionary_create_reply(message) : nil
do {
try handler.handle(request: request, server: self, connection: connection, reply: &reply)
try self.maybeSendReply(&reply, request: request, connection: connection)
} catch {
var reply = handler.shouldCreateReply ? reply : xpc_dictionary_create_reply(message)
self.handleError(error, request: request, connection: connection, reply: &reply)
guard !handler.shouldCreateReply else {
self.handleError(error, request: request, connection: connection, reply: &reply)
return
}

let error = XPCError.asXPCError(error: error)
self.errorHandler.handle(error, connection.token)

// If it's possible to reply, then send the error back to the client
do {
var response = xpc_dictionary_create(nil, nil, 0)
try Response.encodeRequestID(request.requestID, intoReply: &response)
try Response.encodeError(error, intoReply: &response)
xpc_connection_send_message(connection, response)

// End Transaction
if let reply = xpc_dictionary_create_reply(message) {
xpc_connection_send_message(connection, reply)
}
} catch {
// If these actions fail, then there's no way to proceed
}
}
}
}
Expand All @@ -478,8 +498,28 @@ public class XPCServer {
try await handler.handle(request: request, server: self, connection: connection, reply: &reply)
try self.maybeSendReply(&reply, request: request, connection: connection)
} catch {
var reply = handler.shouldCreateReply ? reply : xpc_dictionary_create_reply(message)
self.handleError(error, request: request, connection: connection, reply: &reply)
guard !handler.shouldCreateReply else {
self.handleError(error, request: request, connection: connection, reply: &reply)
return
}

let error = XPCError.asXPCError(error: error)
self.errorHandler.handle(error, connection.token)

// If it's possible to reply, then send the error back to the client
do {
var response = xpc_dictionary_create(nil, nil, 0)
try Response.encodeRequestID(request.requestID, intoReply: &response)
try Response.encodeError(error, intoReply: &response)
xpc_connection_send_message(connection, response)

// End Transaction
if let reply = xpc_dictionary_create_reply(message) {
xpc_connection_send_message(connection, reply)
}
} catch {
// If these actions fail, then there's no way to proceed
}
}
}
}
Expand Down Expand Up @@ -850,8 +890,8 @@ fileprivate struct ConstrainedXPCHandlerWithMessageWithSequentialReplySync<M: De

func handle(request: Request, server: XPCServer, connection: xpc_connection_t, reply: inout xpc_object_t?) throws {
try checkRequest(request, reply: &reply, messageType: M.self, replyType: nil, sequentialReplyType: S.self)
let sequenceProvider = SequentialResultProvider<S>(request: request, server: server, connection: connection)
let decodedMessage = try request.decodePayload(asType: M.self)
let sequenceProvider = SequentialResultProvider<S>(request: request, server: server, connection: connection)
self.handler(connection.token, decodedMessage, sequenceProvider)
}
}
Expand Down