ServerSessionBidirectionalStreaming.swift 3.5 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 ServerSessionBidirectionalStreaming: ServerSession {
  20. func waitForSendOperationsToFinish()
  21. }
  22. open class ServerSessionBidirectionalStreamingBase<InputType: Message, OutputType: Message>: ServerSessionBase, ServerSessionBidirectionalStreaming, StreamReceiving, StreamSending {
  23. public typealias ReceivedType = InputType
  24. public typealias SentType = OutputType
  25. public typealias ProviderBlock = (ServerSessionBidirectionalStreamingBase) throws -> Void
  26. private var providerBlock: ProviderBlock
  27. public init(handler: Handler, providerBlock: @escaping ProviderBlock) {
  28. self.providerBlock = providerBlock
  29. super.init(handler: handler)
  30. }
  31. public func run(queue: DispatchQueue) throws {
  32. try handler.sendMetadata(initialMetadata: initialMetadata) { success in
  33. queue.async {
  34. var responseStatus: ServerStatus?
  35. if success {
  36. do {
  37. try self.providerBlock(self)
  38. } catch {
  39. responseStatus = (error as? ServerStatus) ?? .processingError
  40. }
  41. } else {
  42. print("ServerSessionBidirectionalStreamingBase.run sending initial metadata failed")
  43. responseStatus = .sendingInitialMetadataFailed
  44. }
  45. if let responseStatus = responseStatus {
  46. // Error encountered, notify the client.
  47. do {
  48. try self.handler.sendStatus(responseStatus)
  49. } catch {
  50. print("ServerSessionBidirectionalStreamingBase.run error sending status: \(error)")
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. /// Simple fake implementation of ServerSessionBidirectionalStreaming that returns a previously-defined set of results
  58. /// and stores sent values for later verification.
  59. open class ServerSessionBidirectionalStreamingTestStub<InputType: Message, OutputType: Message>: ServerSessionTestStub, ServerSessionBidirectionalStreaming {
  60. open var lock = Mutex()
  61. open var inputs: [InputType] = []
  62. open var outputs: [OutputType] = []
  63. open var status: ServerStatus?
  64. open func _receive(timeout: DispatchTime) throws -> InputType? {
  65. return lock.synchronize {
  66. defer { if !inputs.isEmpty { inputs.removeFirst() } }
  67. return inputs.first
  68. }
  69. }
  70. open func receive(completion: @escaping (ResultOrRPCError<InputType?>) -> Void) throws {
  71. completion(.result(try self._receive(timeout: .distantFuture)))
  72. }
  73. open func send(_ message: OutputType, completion _: @escaping (Error?) -> Void) throws {
  74. lock.synchronize { outputs.append(message) }
  75. }
  76. open func _send(_ message: OutputType, timeout: DispatchTime) throws {
  77. lock.synchronize { outputs.append(message) }
  78. }
  79. open func close(withStatus status: ServerStatus, completion: (() -> Void)?) throws {
  80. lock.synchronize { self.status = status }
  81. completion?()
  82. }
  83. open func waitForSendOperationsToFinish() {}
  84. }