| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- /*
- *
- * Copyright 2016, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- #if SWIFT_PACKAGE
- import CgRPC
- #endif
- import Foundation // for String.Encoding
- /// A gRPC request handler
- public class Handler {
- /// Pointer to underlying C representation
- private var underlyingHandler: UnsafeMutableRawPointer!
- /// Completion queue for handler response operations
- var completionQueue: CompletionQueue
- /// Metadata received with the request
- public var requestMetadata: Metadata
- /// A Call object that can be used to respond to the request
- lazy var call: Call = {
- return Call(call: cgrpc_handler_get_call(self.underlyingHandler),
- owned: false,
- completionQueue: self.completionQueue)
- }()
- /// The host name sent with the request
- public lazy var host: String = {
- return String(cString:cgrpc_handler_host(self.underlyingHandler),
- encoding:.utf8)!;
- }()
- /// The method name sent with the request
- public lazy var method: String = {
- return String(cString:cgrpc_handler_method(self.underlyingHandler),
- encoding:.utf8)!;
- }()
- /// 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 h: the underlying C representation
- init(underlyingHandler:UnsafeMutableRawPointer!) {
- self.underlyingHandler = underlyingHandler;
- self.requestMetadata = Metadata()
- self.completionQueue = CompletionQueue(
- underlyingCompletionQueue:cgrpc_handler_get_completion_queue(underlyingHandler))
- self.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
- ///
- /// - Returns: a CallError indicating the result of requesting the call
- func requestCall(tag: Int) -> CallError {
- let error = cgrpc_handler_request_call(underlyingHandler, requestMetadata.array, tag)
- return CallError.callError(grpcCallError: error)
- }
- /// Receive the message sent with a call
- ///
- /// - Returns: a tuple containing status codes and a message (if available)
- public func receiveMessage(initialMetadata: Metadata,
- completion:@escaping ((Data?) -> Void)) -> Void {
- let operation_sendInitialMetadata = Operation_SendInitialMetadata(metadata:initialMetadata);
- let operation_receiveMessage = Operation_ReceiveMessage()
- let operations = OperationGroup(
- call:call,
- operations:[
- operation_sendInitialMetadata,
- operation_receiveMessage])
- {(success) in
- if (success) {
- completion(operation_receiveMessage.message()!.data())
- } else {
- completion(nil)
- }
- }
- self.completionQueue.operationGroups[operations.tag] = operations
- _ = call.performOperations(operations:operations,
- tag:operations.tag,
- completionQueue: self.completionQueue)
- }
- /// Sends the response to a request
- ///
- /// - Parameter message: the message to send
- /// - Returns: a tuple containing status codes
- public func sendResponse(message: Data,
- trailingMetadata: Metadata) -> Void {
- let operation_receiveCloseOnServer = Operation_ReceiveCloseOnServer();
- let operation_sendStatusFromServer = Operation_SendStatusFromServer(status:0,
- statusDetails:"OK",
- metadata:trailingMetadata)
- let messageBuffer = ByteBuffer(data:message)
- let operation_sendMessage = Operation_SendMessage(message:messageBuffer)
- let operations = OperationGroup(
- call:call,
- operations:[
- operation_receiveCloseOnServer,
- operation_sendStatusFromServer,
- operation_sendMessage])
- {(call_error) in
- self.shutdown()
- }
- self.completionQueue.operationGroups[operations.tag] = operations
- _ = call.performOperations(operations:operations,
- tag:operations.tag,
- completionQueue: self.completionQueue)
- }
- /// shutdown the handler's completion queue
- public func shutdown() {
- completionQueue.shutdown()
- }
- /// Send initial metadata in response to a connection
- public func sendMetadata(initialMetadata: Metadata,
- completion:@escaping (() -> Void)) {
- let operation_sendInitialMetadata = Operation_SendInitialMetadata(metadata:initialMetadata);
- let operations = OperationGroup(call:call, operations:[operation_sendInitialMetadata])
- {(success) in
- if (success) {
- completion()
- } else {
- completion()
- }
- }
- self.completionQueue.operationGroups[operations.tag] = operations
- _ = call.performOperations(operations:operations,
- tag:operations.tag,
- completionQueue: self.completionQueue)
- }
- /// Receive the message sent with a call
- ///
- /// - Returns: a tuple containing status codes and a message (if available)
- public func receiveMessage(completion:(@escaping (Data?) -> Void)) -> Void {
- let operation_receiveMessage = Operation_ReceiveMessage()
- let operations = OperationGroup(call:call, operations:[operation_receiveMessage])
- {(success) in
- if (success) {
- print("server receiveMessage complete")
- if let message = operation_receiveMessage.message() {
- completion(message.data())
- } else {
- completion(nil)
- }
- } else {
- completion(nil)
- }
- }
- self.completionQueue.operationGroups[operations.tag] = operations
- let call_error = call.performOperations(operations:operations,
- tag:operations.tag,
- completionQueue: self.completionQueue)
- print("perform receiveMessage \(call_error)")
- }
- /// Sends the response to a request
- ///
- /// - Parameter message: the message to send
- /// - Returns: a tuple containing status codes
- public func sendResponse(message: Data,
- completion: @escaping () -> Void) -> Void {
- let operation_sendMessage = Operation_SendMessage(message:ByteBuffer(data:message))
- let operations = OperationGroup(call:call, operations:[operation_sendMessage])
- {(event) in
- print("server sendResponse complete")
- completion()
- }
- self.completionQueue.operationGroups[operations.tag] = operations
- _ = call.performOperations(operations:operations,
- tag:operations.tag,
- completionQueue: self.completionQueue)
- }
- /// Recognize when the client has closed a request
- public func receiveClose(completion: @escaping () -> Void) -> Void {
- let operation_receiveClose = Operation_ReceiveCloseOnServer()
- let operations = OperationGroup(call:call, operations:[operation_receiveClose])
- {(event) in
- print("server receiveClose complete")
- completion()
- }
- self.completionQueue.operationGroups[operations.tag] = operations
- let call_error = call.performOperations(operations:operations,
- tag:operations.tag,
- completionQueue: self.completionQueue)
- print("perform receiveClose \(call_error)")
- }
- /// Send final status to the client
- public func sendStatus(trailingMetadata: Metadata,
- completion:@escaping (() -> Void)) -> Void {
- let operation_sendStatusFromServer = Operation_SendStatusFromServer(status:0,
- statusDetails:"OK",
- metadata:trailingMetadata)
- let operations = OperationGroup(call:call, operations:[operation_sendStatusFromServer])
- {(event) in
- print("server sendStatus complete")
- completion()
- }
- self.completionQueue.operationGroups[operations.tag] = operations
- _ = call.performOperations(operations:operations,
- tag:operations.tag,
- completionQueue: self.completionQueue)
- }
- }
|