ClientCallServerStreaming.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 ClientCallServerStreamingBase: class {
  20. static var method: String { get }
  21. /// Cancel the call.
  22. func cancel()
  23. // TODO: Move the other, message type-dependent, methods into this protocol. At the moment, this is not possible,
  24. // as the protocol would then have an associated type requirement (and become pretty much unusable in the process).
  25. }
  26. open class ClientCallServerStreamingImpl<InputType: Message, OutputType: Message>: ClientCallServerStreamingBase {
  27. open class var method: String { fatalError("needs to be overridden") }
  28. private var call: Call
  29. /// Create a call.
  30. public init(_ channel: Channel) {
  31. call = channel.makeCall(type(of: self).method)
  32. }
  33. /// Call this once with the message to send. Nonblocking.
  34. public func start(request: InputType, metadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Self {
  35. let requestData = try request.serializedData()
  36. try call.start(.serverStreaming,
  37. metadata: metadata,
  38. message: requestData,
  39. completion: completion)
  40. return self
  41. }
  42. public func receive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  43. do {
  44. try call.receiveMessage { responseData in
  45. if let responseData = responseData {
  46. if let response = try? OutputType(serializedData: responseData) {
  47. completion(response, nil)
  48. } else {
  49. completion(nil, .invalidMessageReceived)
  50. }
  51. } else {
  52. completion(nil, .endOfStream)
  53. }
  54. }
  55. }
  56. }
  57. public func receive() throws -> OutputType {
  58. var returnError: ClientError?
  59. var returnResponse: OutputType!
  60. let sem = DispatchSemaphore(value: 0)
  61. do {
  62. try receive { response, error in
  63. returnResponse = response
  64. returnError = error
  65. sem.signal()
  66. }
  67. _ = sem.wait(timeout: DispatchTime.distantFuture)
  68. }
  69. if let returnError = returnError {
  70. throw returnError
  71. }
  72. return returnResponse
  73. }
  74. public func cancel() {
  75. call.cancel()
  76. }
  77. }
  78. /// Simple fake implementation of ClientCallServerStreamingBase that returns a previously-defined set of results.
  79. open class ClientCallServerStreamingTestStub<OutputType: Message>: ClientCallServerStreamingBase {
  80. open class var method: String { fatalError("needs to be overridden") }
  81. open var outputs: [OutputType] = []
  82. open func receive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  83. if let output = outputs.first {
  84. outputs.removeFirst()
  85. completion(output, nil)
  86. } else {
  87. completion(nil, .endOfStream)
  88. }
  89. }
  90. open func receive() throws -> OutputType {
  91. if let output = outputs.first {
  92. outputs.removeFirst()
  93. return output
  94. } else {
  95. throw ClientError.endOfStream
  96. }
  97. }
  98. open func cancel() {}
  99. }