2
0

ClientCallClientStreaming.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 inputs: [InputType] = []
  62. open var output: OutputType?
  63. public init() {}
  64. open func send(_ message: InputType, completion _: @escaping (Error?) -> Void) throws {
  65. inputs.append(message)
  66. }
  67. open func _send(_ message: InputType, timeout: DispatchTime) throws {
  68. inputs.append(message)
  69. }
  70. open func closeAndReceive(completion: @escaping (ResultOrRPCError<OutputType>) -> Void) throws {
  71. completion(.result(output!))
  72. }
  73. open func closeAndReceive() throws -> OutputType {
  74. return output!
  75. }
  76. open func waitForSendOperationsToFinish() {}
  77. open func cancel() {}
  78. }