2
0

ClientCallServerStreaming.swift 3.2 KB

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