ClientCallBidirectionalStreaming.swift 4.1 KB

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