ClientCallClientStreaming.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 ClientCallClientStreaming: 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 ClientCallClientStreamingBase<InputType: Message, OutputType: Message>: ClientCallBase, ClientCallClientStreaming {
  26. /// Call this to start a call. Nonblocking.
  27. public func start(metadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Self {
  28. try call.start(.clientStreaming, metadata: metadata, completion: completion)
  29. return self
  30. }
  31. public func send(_ message: InputType, errorHandler: @escaping (Error) -> Void) throws {
  32. let messageData = try message.serializedData()
  33. try call.sendMessage(data: messageData, errorHandler: errorHandler)
  34. }
  35. public func closeAndReceive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  36. do {
  37. try call.receiveMessage { responseData in
  38. if let responseData = responseData,
  39. let response = try? OutputType(serializedData: responseData) {
  40. completion(response, nil)
  41. } else {
  42. completion(nil, .invalidMessageReceived)
  43. }
  44. }
  45. try call.close(completion: {})
  46. } catch (let error) {
  47. throw error
  48. }
  49. }
  50. public func closeAndReceive() throws -> OutputType {
  51. var returnError: ClientError?
  52. var returnResponse: OutputType!
  53. let sem = DispatchSemaphore(value: 0)
  54. do {
  55. try closeAndReceive { response, error in
  56. returnResponse = response
  57. returnError = error
  58. sem.signal()
  59. }
  60. _ = sem.wait(timeout: DispatchTime.distantFuture)
  61. } catch (let error) {
  62. throw error
  63. }
  64. if let returnError = returnError {
  65. throw returnError
  66. }
  67. return returnResponse
  68. }
  69. public func cancel() {
  70. call.cancel()
  71. }
  72. }
  73. /// Simple fake implementation of ClientCallClientStreamingBase that
  74. /// stores sent values for later verification and finally returns a previously-defined result.
  75. open class ClientCallClientStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallClientStreaming {
  76. open class var method: String { fatalError("needs to be overridden") }
  77. open var inputs: [InputType] = []
  78. open var output: OutputType?
  79. public init() {}
  80. open func send(_ message: InputType, errorHandler _: @escaping (Error) -> Void) throws {
  81. inputs.append(message)
  82. }
  83. open func closeAndReceive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  84. completion(output!, nil)
  85. }
  86. open func closeAndReceive() throws -> OutputType {
  87. return output!
  88. }
  89. open func cancel() {}
  90. }