ClientCallBidirectionalStreaming.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 {
  25. /// Call this to start a call. Nonblocking.
  26. public func start(metadata: Metadata, completion: ((CallResult) -> Void)?)
  27. throws -> Self {
  28. try call.start(.bidiStreaming, metadata: metadata, completion: completion)
  29. return self
  30. }
  31. public func receive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  32. do {
  33. try call.receiveMessage { data in
  34. if let data = data {
  35. if let returnMessage = try? OutputType(serializedData: data) {
  36. completion(returnMessage, nil)
  37. } else {
  38. completion(nil, .invalidMessageReceived)
  39. }
  40. } else {
  41. completion(nil, .endOfStream)
  42. }
  43. }
  44. }
  45. }
  46. public func receive() throws -> OutputType {
  47. var returnError: ClientError?
  48. var returnMessage: OutputType!
  49. let sem = DispatchSemaphore(value: 0)
  50. do {
  51. try receive { response, error in
  52. returnMessage = response
  53. returnError = error
  54. sem.signal()
  55. }
  56. _ = sem.wait()
  57. }
  58. if let returnError = returnError {
  59. throw returnError
  60. }
  61. return returnMessage
  62. }
  63. public func send(_ message: InputType, completion: @escaping (Error?) -> Void) throws {
  64. let messageData = try message.serializedData()
  65. try call.sendMessage(data: messageData, completion: completion)
  66. }
  67. public func closeSend(completion: (() -> Void)?) throws {
  68. try call.close(completion: completion)
  69. }
  70. public func closeSend() throws {
  71. let sem = DispatchSemaphore(value: 0)
  72. try closeSend {
  73. sem.signal()
  74. }
  75. _ = sem.wait()
  76. }
  77. public func waitForSendOperationsToFinish() {
  78. call.messageQueueEmpty.wait()
  79. }
  80. }
  81. /// Simple fake implementation of ClientCallBidirectionalStreamingBase that returns a previously-defined set of results
  82. /// and stores sent values for later verification.
  83. open class ClientCallBidirectionalStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallBidirectionalStreaming {
  84. open class var method: String { fatalError("needs to be overridden") }
  85. open var inputs: [InputType] = []
  86. open var outputs: [OutputType] = []
  87. public init() {}
  88. open func receive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  89. if let output = outputs.first {
  90. outputs.removeFirst()
  91. completion(output, nil)
  92. } else {
  93. completion(nil, .endOfStream)
  94. }
  95. }
  96. open func receive() throws -> OutputType {
  97. if let output = outputs.first {
  98. outputs.removeFirst()
  99. return output
  100. } else {
  101. throw ClientError.endOfStream
  102. }
  103. }
  104. open func send(_ message: InputType, completion _: @escaping (Error?) -> Void) throws {
  105. inputs.append(message)
  106. }
  107. open func closeSend(completion: (() -> Void)?) throws { completion?() }
  108. open func closeSend() throws {}
  109. open func waitForSendOperationsToFinish() {}
  110. open func cancel() {}
  111. }