Handler.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #if SWIFT_PACKAGE
  34. import CgRPC
  35. #endif
  36. import Foundation // for String.Encoding
  37. /// A gRPC request handler
  38. public class Handler {
  39. /// Pointer to underlying C representation
  40. var h: UnsafeMutableRawPointer!
  41. /// Completion queue for handler response operations
  42. var completionQueue: CompletionQueue
  43. /// Metadata received with the request
  44. public var requestMetadata: Metadata
  45. /// Initializes a Handler
  46. ///
  47. /// - Parameter h: the underlying C representation
  48. init(h:UnsafeMutableRawPointer!) {
  49. self.h = h;
  50. self.requestMetadata = Metadata()
  51. self.completionQueue = CompletionQueue(cq:cgrpc_handler_get_completion_queue(h))
  52. }
  53. deinit {
  54. cgrpc_handler_destroy(self.h)
  55. }
  56. /// Gets the host name sent with the request
  57. ///
  58. /// - Returns: the host name sent with the request
  59. public func host() -> String {
  60. return String(cString:cgrpc_handler_host(h), encoding:String.Encoding.utf8)!;
  61. }
  62. /// Gets the method name sent with the request
  63. ///
  64. /// - Returns: the method name sent with the request
  65. public func method() -> String {
  66. return String(cString:cgrpc_handler_method(h), encoding:String.Encoding.utf8)!;
  67. }
  68. /// Gets the caller identity associated with the request
  69. ///
  70. /// - Returns: a string representing the caller address
  71. public func caller() -> String {
  72. return String(cString:cgrpc_handler_call_peer(h), encoding:String.Encoding.utf8)!;
  73. }
  74. /// Creates a call object associated with the handler
  75. ///
  76. /// - Returns: a Call object that can be used to respond to the request
  77. func call() -> Call {
  78. return Call(call: cgrpc_handler_get_call(h), owned:false)
  79. }
  80. /// Request a call for the handler
  81. ///
  82. /// Fills the handler properties with information about the received request
  83. ///
  84. /// - Returns: a grpc_call_error indicating the result of requesting the call
  85. func requestCall(tag: Int) -> grpc_call_error {
  86. return cgrpc_handler_request_call(h, requestMetadata.array, tag)
  87. }
  88. /// Receive the message sent with a call
  89. ///
  90. /// - Returns: a tuple containing status codes and a message (if available)
  91. public func receiveMessage(initialMetadata: Metadata) -> (grpc_call_error, grpc_completion_type, ByteBuffer?) {
  92. let operation_sendInitialMetadata = Operation_SendInitialMetadata(metadata:initialMetadata);
  93. let operation_receiveMessage = Operation_ReceiveMessage()
  94. let operations: [Operation] = [
  95. operation_sendInitialMetadata,
  96. operation_receiveMessage
  97. ]
  98. let call = self.call()
  99. let call_error = call.performOperations(operations:operations, tag:222)
  100. if call_error != GRPC_CALL_OK {
  101. return (call_error, GRPC_OP_COMPLETE, nil)
  102. }
  103. let call_status = completionQueue.waitForCompletion(timeout:5.0)
  104. if (call_status == GRPC_OP_COMPLETE) {
  105. return (GRPC_CALL_OK, GRPC_OP_COMPLETE, operation_receiveMessage.message())
  106. } else {
  107. return (GRPC_CALL_OK, call_status, nil)
  108. }
  109. }
  110. /// Sends the response to a request
  111. ///
  112. /// - Parameter message: the message to send
  113. /// - Returns: a tuple containing status codes
  114. public func sendResponse(message: ByteBuffer,
  115. trailingMetadata: Metadata) -> (grpc_call_error, grpc_completion_type) {
  116. let operation_receiveCloseOnServer = Operation_ReceiveCloseOnServer();
  117. let operation_sendStatusFromServer = Operation_SendStatusFromServer(status:0,
  118. statusDetails:"OK",
  119. metadata:trailingMetadata)
  120. let operation_sendMessage = Operation_SendMessage(message:message)
  121. let operations: [Operation] = [
  122. operation_receiveCloseOnServer,
  123. operation_sendStatusFromServer,
  124. operation_sendMessage
  125. ]
  126. let call = self.call()
  127. let call_error = call.performOperations(operations:operations, tag:333)
  128. if call_error != GRPC_CALL_OK {
  129. return (call_error, GRPC_OP_COMPLETE)
  130. }
  131. let call_status = completionQueue.waitForCompletion(timeout:5.0)
  132. return (GRPC_CALL_OK, call_status)
  133. }
  134. }