ServerSessionBidirectionalStreaming.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 inputs: [InputType] = []
  61. open var outputs: [OutputType] = []
  62. open var status: ServerStatus?
  63. open func receive() throws -> InputType? {
  64. defer { if !inputs.isEmpty { inputs.removeFirst() } }
  65. return inputs.first
  66. }
  67. open func receive(completion: @escaping (ResultOrRPCError<InputType?>) -> Void) throws {
  68. completion(.result(try self.receive()))
  69. }
  70. open func send(_ message: OutputType, completion _: @escaping (Error?) -> Void) throws {
  71. outputs.append(message)
  72. }
  73. open func send(_ message: OutputType) throws {
  74. outputs.append(message)
  75. }
  76. open func close(withStatus status: ServerStatus, completion: (() -> Void)?) throws {
  77. self.status = status
  78. completion?()
  79. }
  80. open func waitForSendOperationsToFinish() {}
  81. }