Handler.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if SWIFT_PACKAGE
  17. import CgRPC
  18. #endif
  19. import Foundation // for String.Encoding
  20. /// A gRPC request handler
  21. public class Handler {
  22. /// Pointer to underlying C representation
  23. fileprivate let underlyingHandler: UnsafeMutableRawPointer
  24. /// Completion queue for handler response operations
  25. let completionQueue: CompletionQueue
  26. /// Metadata received with the request
  27. public let requestMetadata: Metadata
  28. /// A Call object that can be used to respond to the request
  29. private(set) lazy var call: Call = {
  30. Call(underlyingCall: cgrpc_handler_get_call(self.underlyingHandler),
  31. owned: false,
  32. completionQueue: self.completionQueue)
  33. }()
  34. /// The host name sent with the request
  35. public lazy var host: String? = {
  36. // We actually know that this method will never return nil,
  37. // so we can forcibly unwrap the result. (Also below.)
  38. let string = cgrpc_handler_copy_host(self.underlyingHandler)!
  39. defer { cgrpc_free_copied_string(string) }
  40. return String(cString: string, encoding: .utf8)
  41. }()
  42. /// The method name sent with the request
  43. public lazy var method: String? = {
  44. let string = cgrpc_handler_copy_method(self.underlyingHandler)!
  45. defer { cgrpc_free_copied_string(string) }
  46. return String(cString: string, encoding: .utf8)
  47. }()
  48. /// The caller address associated with the request
  49. public lazy var caller: String? = {
  50. let string = cgrpc_handler_call_peer(self.underlyingHandler)!
  51. defer { cgrpc_free_copied_string(string) }
  52. return String(cString: string, encoding: .utf8)
  53. }()
  54. /// Initializes a Handler
  55. ///
  56. /// - Parameter underlyingServer: the underlying C representation of the associated server
  57. init(underlyingServer: UnsafeMutableRawPointer) {
  58. underlyingHandler = cgrpc_handler_create_with_server(underlyingServer)
  59. requestMetadata = Metadata()
  60. completionQueue = CompletionQueue(
  61. underlyingCompletionQueue: cgrpc_handler_get_completion_queue(underlyingHandler), name: "Handler")
  62. }
  63. deinit {
  64. cgrpc_handler_destroy(self.underlyingHandler)
  65. }
  66. /// Requests a call for the handler
  67. ///
  68. /// Fills the handler properties with information about the received request
  69. ///
  70. func requestCall(tag: Int) throws {
  71. let error = cgrpc_handler_request_call(underlyingHandler, requestMetadata.underlyingArray, tag)
  72. if error != GRPC_CALL_OK {
  73. throw CallError.callError(grpcCallError: error)
  74. }
  75. }
  76. /// Receive the message sent with a call
  77. ///
  78. public func receiveMessage(initialMetadata: Metadata,
  79. completion: @escaping (Data?) throws -> Void) throws {
  80. let operations = OperationGroup(call: call,
  81. operations: [
  82. .sendInitialMetadata(initialMetadata),
  83. .receiveMessage
  84. ]) { operationGroup in
  85. if operationGroup.success {
  86. try completion(operationGroup.receivedMessage()?.data())
  87. } else {
  88. try completion(nil)
  89. }
  90. }
  91. try call.perform(operations)
  92. }
  93. /// Sends the response to a request
  94. ///
  95. /// - Parameter message: the message to send
  96. /// - Parameter statusCode: status code to send
  97. /// - Parameter statusMessage: status message to send
  98. /// - Parameter trailingMetadata: trailing metadata to send
  99. public func sendResponse(message: Data,
  100. statusCode: StatusCode,
  101. statusMessage: String,
  102. trailingMetadata: Metadata) throws {
  103. let messageBuffer = ByteBuffer(data: message)
  104. let operations = OperationGroup(call: call,
  105. operations: [
  106. .receiveCloseOnServer,
  107. .sendStatusFromServer(statusCode, statusMessage, trailingMetadata),
  108. .sendMessage(messageBuffer)
  109. ]) { operationGroup in
  110. self.shutdown()
  111. }
  112. try call.perform(operations)
  113. }
  114. /// Sends the response to a request
  115. ///
  116. /// - Parameter statusCode: status code to send
  117. /// - Parameter statusMessage: status message to send
  118. /// - Parameter trailingMetadata: trailing metadata to send
  119. public func sendResponse(statusCode: StatusCode,
  120. statusMessage: String,
  121. trailingMetadata: Metadata) throws {
  122. let operations = OperationGroup(call: call,
  123. operations: [
  124. .receiveCloseOnServer,
  125. .sendStatusFromServer(statusCode, statusMessage, trailingMetadata)
  126. ]) { operationGroup in
  127. self.shutdown()
  128. }
  129. try call.perform(operations)
  130. }
  131. /// Shuts down the handler's completion queue
  132. public func shutdown() {
  133. completionQueue.shutdown()
  134. }
  135. /// Send initial metadata in response to a connection
  136. ///
  137. /// - Parameter initialMetadata: initial metadata to send
  138. /// - Parameter completion: a completion handler to call after the metadata has been sent
  139. public func sendMetadata(initialMetadata: Metadata,
  140. completion: ((Bool) throws -> Void)? = nil) throws {
  141. let operations = OperationGroup(call: call,
  142. operations: [.sendInitialMetadata(initialMetadata)],
  143. completion: completion != nil
  144. ? { operationGroup in try completion?(operationGroup.success) }
  145. : nil)
  146. try call.perform(operations)
  147. }
  148. /// Receive the message sent with a call
  149. ///
  150. /// - Parameter completion: a completion handler to call after the message has been received
  151. /// - Returns: a tuple containing status codes and a message (if available)
  152. public func receiveMessage(completion: @escaping (Data?) throws -> Void) throws {
  153. try call.receiveMessage(completion: completion)
  154. }
  155. /// Sends the response to a request
  156. ///
  157. /// - Parameter message: the message to send
  158. /// - Parameter completion: a completion handler to call after the response has been sent
  159. public func sendResponse(message: Data,
  160. completion: ((Error?) -> Void)? = nil) throws {
  161. try call.sendMessage(data: message, completion: completion)
  162. }
  163. /// Recognize when the client has closed a request
  164. ///
  165. /// - Parameter completion: a completion handler to call after request has been closed
  166. public func receiveClose(completion: @escaping (Bool) throws -> Void) throws {
  167. let operations = OperationGroup(call: call,
  168. operations: [.receiveCloseOnServer]) { operationGroup in
  169. try completion(operationGroup.success)
  170. }
  171. try call.perform(operations)
  172. }
  173. /// Send final status to the client
  174. ///
  175. /// - Parameter statusCode: status code to send
  176. /// - Parameter statusMessage: status message to send
  177. /// - Parameter trailingMetadata: trailing metadata to send
  178. /// - Parameter completion: a completion handler to call after the status has been sent
  179. public func sendStatus(statusCode: StatusCode,
  180. statusMessage: String,
  181. trailingMetadata: Metadata,
  182. completion: ((Bool) -> Void)? = nil) throws {
  183. let operations = OperationGroup(call: call,
  184. operations: [
  185. .sendStatusFromServer(statusCode,
  186. statusMessage,
  187. trailingMetadata)
  188. ]) { operationGroup in
  189. completion?(operationGroup.success)
  190. self.shutdown()
  191. }
  192. try call.perform(operations)
  193. }
  194. }
  195. extension Handler: Hashable {
  196. public var hashValue: Int { return underlyingHandler.hashValue }
  197. public static func ==(A: Handler, B: Handler) -> Bool {
  198. return A === B
  199. }
  200. }