ClientCallClientStreaming.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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, completion: @escaping (Error?) -> Void) throws {
  32. let messageData = try message.serializedData()
  33. try call.sendMessage(data: messageData, completion: completion)
  34. }
  35. public func closeAndReceive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  36. do {
  37. try call.closeAndReceiveMessage { 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. } catch (let error) {
  46. throw error
  47. }
  48. }
  49. public func closeAndReceive() throws -> OutputType {
  50. var returnError: ClientError?
  51. var returnResponse: OutputType!
  52. let sem = DispatchSemaphore(value: 0)
  53. do {
  54. try closeAndReceive { response, error in
  55. returnResponse = response
  56. returnError = error
  57. sem.signal()
  58. }
  59. _ = sem.wait()
  60. } catch (let error) {
  61. throw error
  62. }
  63. if let returnError = returnError {
  64. throw returnError
  65. }
  66. return returnResponse
  67. }
  68. public func cancel() {
  69. call.cancel()
  70. }
  71. }
  72. /// Simple fake implementation of ClientCallClientStreamingBase that
  73. /// stores sent values for later verification and finally returns a previously-defined result.
  74. open class ClientCallClientStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallClientStreaming {
  75. open class var method: String { fatalError("needs to be overridden") }
  76. open var inputs: [InputType] = []
  77. open var output: OutputType?
  78. public init() {}
  79. open func send(_ message: InputType, completion _: @escaping (Error?) -> Void) throws {
  80. inputs.append(message)
  81. }
  82. open func closeAndReceive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  83. completion(output!, nil)
  84. }
  85. open func closeAndReceive() throws -> OutputType {
  86. return output!
  87. }
  88. open func cancel() {}
  89. }