2
0

ClientCallBidirectionalStreaming.swift 3.0 KB

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