ClientCallClientStreaming.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2018, 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. import Dispatch
  17. import Foundation
  18. import SwiftProtobuf
  19. public protocol ClientCallClientStreamingBase: class {
  20. static var method: String { get }
  21. /// Cancel the call.
  22. func cancel()
  23. // TODO: Move the other, message type-dependent, methods into this protocol. At the moment, this is not possible,
  24. // as the protocol would then have an associated type requirement (and become pretty much unusable in the process).
  25. }
  26. open class ClientCallClientStreamingImpl<InputType: Message, OutputType: Message>: ClientCallClientStreamingBase {
  27. open class var method: String { fatalError("needs to be overridden") }
  28. private var call: Call
  29. /// Create a call.
  30. public init(_ channel: Channel) {
  31. call = channel.makeCall(type(of: self).method)
  32. }
  33. /// Call this to start a call. Nonblocking.
  34. public func start(metadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Self {
  35. try call.start(.clientStreaming, metadata: metadata, completion: completion)
  36. return self
  37. }
  38. public func send(_ message: InputType, errorHandler: @escaping (Error) -> Void) throws {
  39. let messageData = try message.serializedData()
  40. try call.sendMessage(data: messageData, errorHandler: errorHandler)
  41. }
  42. public func closeAndReceive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  43. do {
  44. try call.receiveMessage { responseData in
  45. if let responseData = responseData,
  46. let response = try? OutputType(serializedData: responseData) {
  47. completion(response, nil)
  48. } else {
  49. completion(nil, .invalidMessageReceived)
  50. }
  51. }
  52. try call.close(completion: {})
  53. } catch (let error) {
  54. throw error
  55. }
  56. }
  57. public func closeAndReceive() throws -> OutputType {
  58. var returnError: ClientError?
  59. var returnResponse: OutputType!
  60. let sem = DispatchSemaphore(value: 0)
  61. do {
  62. try closeAndReceive { response, error in
  63. returnResponse = response
  64. returnError = error
  65. sem.signal()
  66. }
  67. _ = sem.wait(timeout: DispatchTime.distantFuture)
  68. } catch (let error) {
  69. throw error
  70. }
  71. if let returnError = returnError {
  72. throw returnError
  73. }
  74. return returnResponse
  75. }
  76. public func cancel() {
  77. call.cancel()
  78. }
  79. }
  80. /// Simple fake implementation of ClientCallClientStreamingBase that
  81. /// stores sent values for later verification and finally returns a previously-defined result.
  82. open class ClientCallClientStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallClientStreamingBase {
  83. open class var method: String { fatalError("needs to be overridden") }
  84. open var inputs: [InputType] = []
  85. open var output: OutputType?
  86. open func send(_ message: InputType, errorHandler _: @escaping (Error) -> Void) throws {
  87. inputs.append(message)
  88. }
  89. open func closeAndReceive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  90. completion(output!, nil)
  91. }
  92. open func closeAndReceive() throws -> OutputType {
  93. return output!
  94. }
  95. open func cancel() {}
  96. }