ClientCallServerStreaming.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 ClientCallServerStreaming: 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 ClientCallServerStreamingBase<InputType: Message, OutputType: Message>: ClientCallBase, ClientCallServerStreaming {
  24. /// Call this once with the message to send. Nonblocking.
  25. public func start(request: InputType, metadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Self {
  26. let requestData = try request.serializedData()
  27. try call.start(.serverStreaming,
  28. metadata: metadata,
  29. message: requestData,
  30. completion: completion)
  31. return self
  32. }
  33. public func receive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  34. do {
  35. try call.receiveMessage { responseData in
  36. if let responseData = responseData {
  37. if let response = try? OutputType(serializedData: responseData) {
  38. completion(response, nil)
  39. } else {
  40. completion(nil, .invalidMessageReceived)
  41. }
  42. } else {
  43. completion(nil, .endOfStream)
  44. }
  45. }
  46. }
  47. }
  48. public func receive() throws -> OutputType {
  49. var returnError: ClientError?
  50. var returnResponse: OutputType!
  51. let sem = DispatchSemaphore(value: 0)
  52. do {
  53. try receive { response, error in
  54. returnResponse = response
  55. returnError = error
  56. sem.signal()
  57. }
  58. _ = sem.wait()
  59. }
  60. if let returnError = returnError {
  61. throw returnError
  62. }
  63. return returnResponse
  64. }
  65. }
  66. /// Simple fake implementation of ClientCallServerStreamingBase that returns a previously-defined set of results.
  67. open class ClientCallServerStreamingTestStub<OutputType: Message>: ClientCallServerStreaming {
  68. open class var method: String { fatalError("needs to be overridden") }
  69. open var outputs: [OutputType] = []
  70. public init() {}
  71. open func receive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  72. if let output = outputs.first {
  73. outputs.removeFirst()
  74. completion(output, nil)
  75. } else {
  76. completion(nil, .endOfStream)
  77. }
  78. }
  79. open func receive() throws -> OutputType {
  80. if let output = outputs.first {
  81. outputs.removeFirst()
  82. return output
  83. } else {
  84. throw ClientError.endOfStream
  85. }
  86. }
  87. open func cancel() {}
  88. }