ClientCallClientStreaming.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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) { result in
  29. withExtendedLifetime(self) { // retain `self` (and, transitively, the channel) until the call has finished.
  30. completion?(result)
  31. }
  32. }
  33. return self
  34. }
  35. public func closeAndReceive(completion: @escaping (ResultOrRPCError<OutputType>) -> Void) throws {
  36. try call.closeAndReceiveMessage { callResult in
  37. guard let responseData = callResult.resultData else {
  38. completion(.error(.callError(callResult))); return
  39. }
  40. if let response = try? OutputType(serializedData: responseData) {
  41. completion(.result(response))
  42. } else {
  43. completion(.error(.invalidMessageReceived))
  44. }
  45. }
  46. }
  47. public func closeAndReceive() throws -> OutputType {
  48. var result: ResultOrRPCError<OutputType>?
  49. let sem = DispatchSemaphore(value: 0)
  50. try closeAndReceive {
  51. result = $0
  52. sem.signal()
  53. }
  54. _ = sem.wait()
  55. switch result! {
  56. case .result(let response): return response
  57. case .error(let error): throw error
  58. }
  59. }
  60. }
  61. /// Simple fake implementation of ClientCallClientStreamingBase that
  62. /// stores sent values for later verification and finally returns a previously-defined result.
  63. open class ClientCallClientStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallClientStreaming {
  64. open class var method: String { fatalError("needs to be overridden") }
  65. open var lock = Mutex()
  66. open var inputs: [InputType] = []
  67. open var output: OutputType?
  68. public init() {}
  69. open func send(_ message: InputType, completion _: @escaping (Error?) -> Void) throws {
  70. lock.synchronize { inputs.append(message) }
  71. }
  72. open func _send(_ message: InputType, timeout: DispatchTime) throws {
  73. lock.synchronize { inputs.append(message) }
  74. }
  75. open func closeAndReceive(completion: @escaping (ResultOrRPCError<OutputType>) -> Void) throws {
  76. completion(.result(output!))
  77. }
  78. open func closeAndReceive() throws -> OutputType {
  79. return output!
  80. }
  81. open func waitForSendOperationsToFinish() {}
  82. open func cancel() {}
  83. }