Call.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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
  37. /// Singleton class that provides a mutex for synchronizing calls to cgrpc_call_perform()
  38. private class CallLock {
  39. var mutex : Mutex
  40. private init() {
  41. mutex = Mutex()
  42. }
  43. static let sharedInstance = CallLock()
  44. }
  45. public enum CallError : Error {
  46. case ok
  47. case unknown
  48. case notOnServer
  49. case notOnClient
  50. case alreadyAccepted
  51. case alreadyInvoked
  52. case notInvoked
  53. case alreadyFinished
  54. case tooManyOperations
  55. case invalidFlags
  56. case invalidMetadata
  57. case invalidMessage
  58. case notServerCompletionQueue
  59. case batchTooBig
  60. case payloadTypeMismatch
  61. static func callError(grpcCallError error: grpc_call_error) -> CallError {
  62. switch(error) {
  63. case GRPC_CALL_OK:
  64. return .ok
  65. case GRPC_CALL_ERROR:
  66. return .unknown
  67. case GRPC_CALL_ERROR_NOT_ON_SERVER:
  68. return .notOnServer
  69. case GRPC_CALL_ERROR_NOT_ON_CLIENT:
  70. return .notOnClient
  71. case GRPC_CALL_ERROR_ALREADY_ACCEPTED:
  72. return .alreadyAccepted
  73. case GRPC_CALL_ERROR_ALREADY_INVOKED:
  74. return .alreadyInvoked
  75. case GRPC_CALL_ERROR_NOT_INVOKED:
  76. return .notInvoked
  77. case GRPC_CALL_ERROR_ALREADY_FINISHED:
  78. return .alreadyFinished
  79. case GRPC_CALL_ERROR_TOO_MANY_OPERATIONS:
  80. return .tooManyOperations
  81. case GRPC_CALL_ERROR_INVALID_FLAGS:
  82. return .invalidFlags
  83. case GRPC_CALL_ERROR_INVALID_METADATA:
  84. return .invalidMetadata
  85. case GRPC_CALL_ERROR_INVALID_MESSAGE:
  86. return .invalidMessage
  87. case GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE:
  88. return .notServerCompletionQueue
  89. case GRPC_CALL_ERROR_BATCH_TOO_BIG:
  90. return .batchTooBig
  91. case GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH:
  92. return .payloadTypeMismatch
  93. default:
  94. return .unknown
  95. }
  96. }
  97. }
  98. public struct CallResult {
  99. public var statusCode : Int
  100. public var statusMessage : String?
  101. public var resultData : Data?
  102. public var initialMetadata : Metadata?
  103. public var trailingMetadata : Metadata?
  104. }
  105. public typealias CallCompletion = (CallResult) throws -> Void
  106. public typealias SendMessageCompletion = () -> Void
  107. /// A gRPC API call
  108. public class Call {
  109. /// Pointer to underlying C representation
  110. private var underlyingCall : UnsafeMutableRawPointer
  111. /// Completion queue used for call
  112. private var completionQueue: CompletionQueue
  113. /// True if this instance is responsible for deleting the underlying C representation
  114. private var owned : Bool
  115. /// A queue of pending messages to send over the call
  116. private var pendingMessages : Array<Data>
  117. /// True if a message write operation is underway
  118. private var writing : Bool
  119. /// Initializes a Call representation
  120. ///
  121. /// - Parameter call: the underlying C representation
  122. /// - Parameter owned: true if this instance is responsible for deleting the underlying call
  123. init(underlyingCall: UnsafeMutableRawPointer, owned: Bool, completionQueue: CompletionQueue) {
  124. self.underlyingCall = underlyingCall
  125. self.owned = owned
  126. self.completionQueue = completionQueue
  127. self.pendingMessages = []
  128. self.writing = false
  129. }
  130. deinit {
  131. if (owned) {
  132. cgrpc_call_destroy(underlyingCall)
  133. }
  134. }
  135. /// Initiate performance of a group of operations without waiting for completion
  136. ///
  137. /// - Parameter operations: group of operations to be performed
  138. /// - Returns: the result of initiating the call
  139. func perform(_ operations: OperationGroup) throws -> Void {
  140. completionQueue.register(operations)
  141. let mutex = CallLock.sharedInstance.mutex
  142. mutex.lock()
  143. let error = cgrpc_call_perform(underlyingCall, operations.underlyingOperations, operations.tag)
  144. mutex.unlock()
  145. if error != GRPC_CALL_OK {
  146. throw CallError.callError(grpcCallError:error)
  147. }
  148. }
  149. /// Performs a nonstreaming gRPC API call
  150. ///
  151. /// - Parameter message: a ByteBuffer containing the message to send
  152. /// - Parameter metadata: metadata to send with the call
  153. /// - Returns: a CallResponse object containing results of the call
  154. public func performNonStreamingCall(messageData: Data,
  155. metadata: Metadata,
  156. completion: @escaping CallCompletion) throws -> Void {
  157. let messageBuffer = ByteBuffer(data:messageData)
  158. let operation_sendInitialMetadata = Operation_SendInitialMetadata(metadata:metadata);
  159. let operation_sendMessage = Operation_SendMessage(message:messageBuffer)
  160. let operation_sendCloseFromClient = Operation_SendCloseFromClient()
  161. let operation_receiveInitialMetadata = Operation_ReceiveInitialMetadata()
  162. let operation_receiveStatusOnClient = Operation_ReceiveStatusOnClient()
  163. let operation_receiveMessage = Operation_ReceiveMessage()
  164. let operations = OperationGroup(call:self,
  165. operations:[operation_sendInitialMetadata,
  166. operation_sendMessage,
  167. operation_sendCloseFromClient,
  168. operation_receiveInitialMetadata,
  169. operation_receiveStatusOnClient,
  170. operation_receiveMessage],
  171. completion:
  172. {(success) in
  173. if success {
  174. try completion(CallResult(statusCode:operation_receiveStatusOnClient.status(),
  175. statusMessage:operation_receiveStatusOnClient.statusDetails(),
  176. resultData:operation_receiveMessage.message()?.data(),
  177. initialMetadata:operation_receiveInitialMetadata.metadata(),
  178. trailingMetadata:operation_receiveStatusOnClient.metadata()))
  179. } else {
  180. try completion(CallResult(statusCode:0,
  181. statusMessage:nil,
  182. resultData:nil,
  183. initialMetadata:nil,
  184. trailingMetadata:nil))
  185. }
  186. })
  187. try self.perform(operations)
  188. }
  189. // start a streaming connection
  190. public func start(metadata: Metadata) throws -> Void {
  191. try self.sendInitialMetadata(metadata: metadata)
  192. try self.receiveInitialMetadata()
  193. try self.receiveStatus()
  194. }
  195. // send a message over a streaming connection
  196. public func sendMessage(data: Data,
  197. callback:@escaping SendMessageCompletion = {() in })
  198. -> Void {
  199. DispatchQueue.main.async {
  200. if self.writing {
  201. self.pendingMessages.append(data) // TODO: return something if we can't accept another message
  202. callback()
  203. } else {
  204. self.writing = true
  205. do {
  206. try self.sendWithoutBlocking(data: data)
  207. callback()
  208. } catch (let callError) {
  209. print("grpc error: \(callError)")
  210. }
  211. }
  212. }
  213. }
  214. private func sendWithoutBlocking(data: Data) throws -> Void {
  215. let messageBuffer = ByteBuffer(data:data)
  216. let operation_sendMessage = Operation_SendMessage(message:messageBuffer)
  217. let operations = OperationGroup(call:self, operations:[operation_sendMessage])
  218. {(success) in
  219. if success {
  220. DispatchQueue.main.async {
  221. if self.pendingMessages.count > 0 {
  222. let nextMessage = self.pendingMessages.first!
  223. self.pendingMessages.removeFirst()
  224. do {
  225. try self.sendWithoutBlocking(data: nextMessage)
  226. } catch (let callError) {
  227. print("grpc error: \(callError)")
  228. }
  229. } else {
  230. self.writing = false
  231. }
  232. }
  233. } else {
  234. // TODO: if the event failed, shut down
  235. }
  236. }
  237. try self.perform(operations)
  238. }
  239. // receive a message over a streaming connection
  240. public func receiveMessage(callback:@escaping ((Data!) throws -> Void)) throws -> Void {
  241. let operation_receiveMessage = Operation_ReceiveMessage()
  242. let operations = OperationGroup(call:self, operations:[operation_receiveMessage])
  243. {(success) in
  244. if success {
  245. if let messageBuffer = operation_receiveMessage.message() {
  246. try callback(messageBuffer.data())
  247. }
  248. }
  249. }
  250. try self.perform(operations)
  251. }
  252. // send initial metadata over a streaming connection
  253. private func sendInitialMetadata(metadata: Metadata) throws -> Void {
  254. let operation_sendInitialMetadata = Operation_SendInitialMetadata(metadata:metadata);
  255. let operations = OperationGroup(call:self, operations:[operation_sendInitialMetadata])
  256. {(success) in
  257. if success {
  258. print("call successful")
  259. } else {
  260. return
  261. }
  262. }
  263. try self.perform(operations)
  264. }
  265. // receive initial metadata from a streaming connection
  266. private func receiveInitialMetadata() throws -> Void {
  267. let operation_receiveInitialMetadata = Operation_ReceiveInitialMetadata()
  268. let operations = OperationGroup(call:self, operations:[operation_receiveInitialMetadata])
  269. {(success) in
  270. if success {
  271. let initialMetadata = operation_receiveInitialMetadata.metadata()
  272. for j in 0..<initialMetadata.count() {
  273. print("Received initial metadata -> " + initialMetadata.key(index:j) + " : " + initialMetadata.value(index:j))
  274. }
  275. }
  276. }
  277. try self.perform(operations)
  278. }
  279. // receive status from a streaming connection
  280. private func receiveStatus() throws -> Void {
  281. let operation_receiveStatus = Operation_ReceiveStatusOnClient()
  282. let operations = OperationGroup(call:self,
  283. operations:[operation_receiveStatus])
  284. {(success) in
  285. if success {
  286. print("status = \(operation_receiveStatus.status()), \(operation_receiveStatus.statusDetails())")
  287. }
  288. }
  289. try self.perform(operations)
  290. }
  291. // close a streaming connection
  292. public func close(completion:@escaping (() -> Void)) throws -> Void {
  293. let operation_sendCloseFromClient = Operation_SendCloseFromClient()
  294. let operations = OperationGroup(call:self, operations:[operation_sendCloseFromClient])
  295. {(success) in
  296. if success {
  297. completion()
  298. }
  299. }
  300. try self.perform(operations)
  301. }
  302. }