ClientCallBidirectionalStreaming.swift 3.8 KB

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