|
|
@@ -22,20 +22,20 @@ import Foundation // for String.Encoding
|
|
|
public class Handler {
|
|
|
/// Pointer to underlying C representation
|
|
|
private var underlyingHandler: UnsafeMutableRawPointer
|
|
|
-
|
|
|
+
|
|
|
/// Completion queue for handler response operations
|
|
|
internal var completionQueue: CompletionQueue
|
|
|
-
|
|
|
+
|
|
|
/// Metadata received with the request
|
|
|
public var requestMetadata: Metadata
|
|
|
-
|
|
|
+
|
|
|
/// A Call object that can be used to respond to the request
|
|
|
internal lazy var call: Call = {
|
|
|
return Call(underlyingCall: cgrpc_handler_get_call(self.underlyingHandler),
|
|
|
owned: false,
|
|
|
completionQueue: self.completionQueue)
|
|
|
}()
|
|
|
-
|
|
|
+
|
|
|
/// The host name sent with the request
|
|
|
public lazy var host: String = {
|
|
|
if let string = cgrpc_handler_copy_host(self.underlyingHandler) {
|
|
|
@@ -47,7 +47,7 @@ public class Handler {
|
|
|
return ""
|
|
|
}
|
|
|
}()
|
|
|
-
|
|
|
+
|
|
|
/// The method name sent with the request
|
|
|
public lazy var method: String = {
|
|
|
if let string = cgrpc_handler_copy_method(self.underlyingHandler) {
|
|
|
@@ -59,13 +59,13 @@ public class Handler {
|
|
|
return ""
|
|
|
}
|
|
|
}()
|
|
|
-
|
|
|
+
|
|
|
/// The caller address associated with the request
|
|
|
public lazy var caller: String = {
|
|
|
return String(cString:cgrpc_handler_call_peer(self.underlyingHandler),
|
|
|
encoding:.utf8)!;
|
|
|
}()
|
|
|
-
|
|
|
+
|
|
|
/// Initializes a Handler
|
|
|
///
|
|
|
/// - Parameter underlyingServer: the underlying C representation of the associated server
|
|
|
@@ -76,11 +76,11 @@ public class Handler {
|
|
|
underlyingCompletionQueue:cgrpc_handler_get_completion_queue(underlyingHandler))
|
|
|
completionQueue.name = "Handler"
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
deinit {
|
|
|
cgrpc_handler_destroy(self.underlyingHandler)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// Requests a call for the handler
|
|
|
///
|
|
|
/// Fills the handler properties with information about the received request
|
|
|
@@ -91,7 +91,7 @@ public class Handler {
|
|
|
throw CallError.callError(grpcCallError: error)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// Receive the message sent with a call
|
|
|
///
|
|
|
public func receiveMessage(initialMetadata: Metadata,
|
|
|
@@ -110,13 +110,15 @@ public class Handler {
|
|
|
}
|
|
|
try call.perform(operations)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// Sends the response to a request
|
|
|
///
|
|
|
/// - Parameter message: the message to send
|
|
|
+ /// - Parameter statusCode: status code to send
|
|
|
+ /// - Parameter statusMessage: status message to send
|
|
|
/// - Parameter trailingMetadata: trailing metadata to send
|
|
|
public func sendResponse(message: Data,
|
|
|
- statusCode: Int,
|
|
|
+ statusCode: StatusCode,
|
|
|
statusMessage: String,
|
|
|
trailingMetadata: Metadata) throws -> Void {
|
|
|
let messageBuffer = ByteBuffer(data:message)
|
|
|
@@ -133,12 +135,33 @@ public class Handler {
|
|
|
}
|
|
|
try call.perform(operations)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /// Sends the response to a request
|
|
|
+ ///
|
|
|
+ /// - Parameter statusCode: status code to send
|
|
|
+ /// - Parameter statusMessage: status message to send
|
|
|
+ /// - Parameter trailingMetadata: trailing metadata to send
|
|
|
+ public func sendResponse(statusCode: StatusCode,
|
|
|
+ statusMessage: String,
|
|
|
+ trailingMetadata: Metadata) throws -> Void {
|
|
|
+ let operations = OperationGroup(
|
|
|
+ call:call,
|
|
|
+ operations:[
|
|
|
+ .receiveCloseOnServer,
|
|
|
+ .sendStatusFromServer(statusCode, statusMessage, trailingMetadata)])
|
|
|
+ {(operationGroup) in
|
|
|
+ if operationGroup.success {
|
|
|
+ self.shutdown()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ try call.perform(operations)
|
|
|
+ }
|
|
|
+
|
|
|
/// Shuts down the handler's completion queue
|
|
|
public func shutdown() {
|
|
|
completionQueue.shutdown()
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// Send initial metadata in response to a connection
|
|
|
///
|
|
|
/// - Parameter initialMetadata: initial metadata to send
|
|
|
@@ -156,7 +179,7 @@ public class Handler {
|
|
|
}
|
|
|
try call.perform(operations)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// Receive the message sent with a call
|
|
|
///
|
|
|
/// - Parameter completion: a completion handler to call after the message has been received
|
|
|
@@ -176,7 +199,7 @@ public class Handler {
|
|
|
}
|
|
|
try call.perform(operations)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// Sends the response to a request
|
|
|
///
|
|
|
/// - Parameter message: the message to send
|
|
|
@@ -192,7 +215,7 @@ public class Handler {
|
|
|
}
|
|
|
try call.perform(operations)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// Recognize when the client has closed a request
|
|
|
///
|
|
|
/// - Parameter completion: a completion handler to call after request has been closed
|
|
|
@@ -206,14 +229,14 @@ public class Handler {
|
|
|
}
|
|
|
try call.perform(operations)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// Send final status to the client
|
|
|
///
|
|
|
/// - Parameter statusCode: status code to send
|
|
|
/// - Parameter statusMessage: status message to send
|
|
|
/// - Parameter trailingMetadata: trailing metadata to send
|
|
|
/// - Parameter completion: a completion handler to call after the status has been sent
|
|
|
- public func sendStatus(statusCode: Int,
|
|
|
+ public func sendStatus(statusCode: StatusCode,
|
|
|
statusMessage: String,
|
|
|
trailingMetadata: Metadata,
|
|
|
completion:@escaping (() -> Void)) throws -> Void {
|