ServerSessionBidirectionalStreaming.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. open class ServerSessionBidirectionalStreamingBase<InputType: Message, OutputType: Message>: ServerSessionBase, ServerSessionBidirectionalStreaming {
  21. public typealias ProviderBlock = (ServerSessionBidirectionalStreamingBase) throws -> Void
  22. private var providerBlock: ProviderBlock
  23. public init(handler: Handler, providerBlock: @escaping ProviderBlock) {
  24. self.providerBlock = providerBlock
  25. super.init(handler: handler)
  26. }
  27. public func receive() throws -> InputType {
  28. let sem = DispatchSemaphore(value: 0)
  29. var requestMessage: InputType?
  30. try handler.receiveMessage { requestData in
  31. if let requestData = requestData {
  32. do {
  33. requestMessage = try InputType(serializedData: requestData)
  34. } catch (let error) {
  35. print("error \(error)")
  36. }
  37. }
  38. sem.signal()
  39. }
  40. _ = sem.wait()
  41. if let requestMessage = requestMessage {
  42. return requestMessage
  43. } else {
  44. throw ServerError.endOfStream
  45. }
  46. }
  47. public func send(_ response: OutputType, completion: ((Bool) -> Void)?) throws {
  48. try handler.sendResponse(message: response.serializedData(), completion: completion)
  49. }
  50. public func close() throws {
  51. let sem = DispatchSemaphore(value: 0)
  52. try handler.sendStatus(statusCode: statusCode,
  53. statusMessage: statusMessage,
  54. trailingMetadata: trailingMetadata) { _ in sem.signal() }
  55. _ = sem.wait()
  56. }
  57. public func run(queue: DispatchQueue) throws {
  58. try handler.sendMetadata(initialMetadata: initialMetadata) { _ in
  59. queue.async {
  60. do {
  61. try self.providerBlock(self)
  62. } catch (let error) {
  63. print("error \(error)")
  64. }
  65. }
  66. }
  67. }
  68. }
  69. /// Simple fake implementation of ServerSessionBidirectionalStreaming that returns a previously-defined set of results
  70. /// and stores sent values for later verification.
  71. open class ServerSessionBidirectionalStreamingTestStub<InputType: Message, OutputType: Message>: ServerSessionTestStub, ServerSessionBidirectionalStreaming {
  72. open var inputs: [InputType] = []
  73. open var outputs: [OutputType] = []
  74. open func receive() throws -> InputType {
  75. if let input = inputs.first {
  76. inputs.removeFirst()
  77. return input
  78. } else {
  79. throw ServerError.endOfStream
  80. }
  81. }
  82. open func send(_ response: OutputType, completion _: ((Bool) -> Void)?) throws {
  83. outputs.append(response)
  84. }
  85. open func close() throws {}
  86. }