ServerSessionBidirectionalStreaming.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 -> ServerStatus?
  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() throws -> ServerStatus? {
  32. try sendInitialMetadataAndWait()
  33. do {
  34. return try self.providerBlock(self)
  35. } catch {
  36. // Errors thrown by `providerBlock` should be logged in that method;
  37. // we return the error as a status code to avoid `ServiceServer` logging this as a "really unexpected" error.
  38. return (error as? ServerStatus) ?? .processingError
  39. }
  40. }
  41. }
  42. /// Simple fake implementation of ServerSessionBidirectionalStreaming that returns a previously-defined set of results
  43. /// and stores sent values for later verification.
  44. open class ServerSessionBidirectionalStreamingTestStub<InputType: Message, OutputType: Message>: ServerSessionTestStub, ServerSessionBidirectionalStreaming {
  45. open var lock = Mutex()
  46. open var inputs: [InputType] = []
  47. open var outputs: [OutputType] = []
  48. open var status: ServerStatus?
  49. open func _receive(timeout: DispatchTime) throws -> InputType? {
  50. return lock.synchronize {
  51. defer { if !inputs.isEmpty { inputs.removeFirst() } }
  52. return inputs.first
  53. }
  54. }
  55. open func receive(completion: @escaping (ResultOrRPCError<InputType?>) -> Void) throws {
  56. completion(.result(try self._receive(timeout: .distantFuture)))
  57. }
  58. open func send(_ message: OutputType, completion _: @escaping (Error?) -> Void) throws {
  59. lock.synchronize { outputs.append(message) }
  60. }
  61. open func _send(_ message: OutputType, timeout: DispatchTime) throws {
  62. lock.synchronize { outputs.append(message) }
  63. }
  64. open func close(withStatus status: ServerStatus, completion: (() -> Void)?) throws {
  65. lock.synchronize { self.status = status }
  66. completion?()
  67. }
  68. open func waitForSendOperationsToFinish() {}
  69. }