ClientCallClientStreaming.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 ClientCallClientStreaming: ClientCall {
  20. func waitForSendOperationsToFinish()
  21. // TODO: Move the other, message type-dependent, methods into this protocol. At the moment, this is not possible,
  22. // as the protocol would then have an associated type requirement (and become pretty much unusable in the process).
  23. }
  24. open class ClientCallClientStreamingBase<InputType: Message, OutputType: Message>: ClientCallBase, ClientCallClientStreaming, StreamSending {
  25. public typealias SentType = InputType
  26. /// Call this to start a call. Nonblocking.
  27. public func start(metadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Self {
  28. try call.start(.clientStreaming, metadata: metadata, completion: completion)
  29. return self
  30. }
  31. public func closeAndReceive(completion: @escaping (ResultOrRPCError<OutputType>) -> Void) throws {
  32. try call.closeAndReceiveMessage { callResult in
  33. guard let responseData = callResult.resultData else {
  34. completion(.error(.callError(callResult))); return
  35. }
  36. if let response = try? OutputType(serializedData: responseData) {
  37. completion(.result(response))
  38. } else {
  39. completion(.error(.invalidMessageReceived))
  40. }
  41. }
  42. }
  43. public func closeAndReceive() throws -> OutputType {
  44. var result: ResultOrRPCError<OutputType>?
  45. let sem = DispatchSemaphore(value: 0)
  46. try closeAndReceive {
  47. result = $0
  48. sem.signal()
  49. }
  50. _ = sem.wait()
  51. switch result! {
  52. case .result(let response): return response
  53. case .error(let error): throw error
  54. }
  55. }
  56. }
  57. /// Simple fake implementation of ClientCallClientStreamingBase that
  58. /// stores sent values for later verification and finally returns a previously-defined result.
  59. open class ClientCallClientStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallClientStreaming {
  60. open class var method: String { fatalError("needs to be overridden") }
  61. open var lock = Mutex()
  62. open var inputs: [InputType] = []
  63. open var output: OutputType?
  64. public init() {}
  65. open func send(_ message: InputType, completion _: @escaping (Error?) -> Void) throws {
  66. lock.synchronize { inputs.append(message) }
  67. }
  68. open func _send(_ message: InputType, timeout: DispatchTime) throws {
  69. lock.synchronize { inputs.append(message) }
  70. }
  71. open func closeAndReceive(completion: @escaping (ResultOrRPCError<OutputType>) -> Void) throws {
  72. completion(.result(output!))
  73. }
  74. open func closeAndReceive() throws -> OutputType {
  75. return output!
  76. }
  77. open func waitForSendOperationsToFinish() {}
  78. open func cancel() {}
  79. }