ClientCallBidirectionalStreaming.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 ClientCallBidirectionalStreaming: 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 ClientCallBidirectionalStreamingBase<InputType: Message, OutputType: Message>: ClientCallBase, ClientCallBidirectionalStreaming, StreamReceiving, StreamSending {
  25. public typealias ReceivedType = OutputType
  26. public typealias SentType = InputType
  27. /// Call this to start a call. Nonblocking.
  28. public func start(metadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Self {
  29. try call.start(.bidiStreaming, metadata: metadata) { result in
  30. withExtendedLifetime(self) { // retain `self` (and, transitively, the channel) until the call has finished.
  31. completion?(result)
  32. }
  33. }
  34. return self
  35. }
  36. public func closeSend(completion: (() -> Void)?) throws {
  37. try call.close(completion: completion)
  38. }
  39. public func closeSend() throws {
  40. let sem = DispatchSemaphore(value: 0)
  41. try closeSend {
  42. sem.signal()
  43. }
  44. _ = sem.wait()
  45. }
  46. }
  47. /// Simple fake implementation of ClientCallBidirectionalStreamingBase that returns a previously-defined set of results
  48. /// and stores sent values for later verification.
  49. open class ClientCallBidirectionalStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallBidirectionalStreaming {
  50. open class var method: String { fatalError("needs to be overridden") }
  51. open var lock = Mutex()
  52. open var inputs: [InputType] = []
  53. open var outputs: [OutputType] = []
  54. public init() {}
  55. open func _receive(timeout: DispatchTime) throws -> OutputType? {
  56. return lock.synchronize {
  57. defer { if !outputs.isEmpty { outputs.removeFirst() } }
  58. return outputs.first
  59. }
  60. }
  61. open func receive(completion: @escaping (ResultOrRPCError<OutputType?>) -> Void) throws {
  62. completion(.result(try self._receive(timeout: .distantFuture)))
  63. }
  64. open func send(_ message: InputType, completion _: @escaping (Error?) -> Void) throws {
  65. lock.synchronize { inputs.append(message) }
  66. }
  67. open func _send(_ message: InputType, timeout: DispatchTime) throws {
  68. lock.synchronize { inputs.append(message) }
  69. }
  70. open func closeSend(completion: (() -> Void)?) throws { completion?() }
  71. open func closeSend() throws {}
  72. open func waitForSendOperationsToFinish() {}
  73. open func cancel() {}
  74. }