ServerStreamingCallHandler.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2019, 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 Foundation
  17. import SwiftProtobuf
  18. import NIO
  19. import NIOHTTP1
  20. import Logging
  21. /// Handles server-streaming calls. Calls the observer block with the request message.
  22. ///
  23. /// - The observer block is implemented by the framework user and calls `context.sendResponse` as needed.
  24. /// - To close the call and send the status, complete the status future returned by the observer block.
  25. public final class ServerStreamingCallHandler<
  26. RequestMessage: Message,
  27. ResponseMessage: Message
  28. >: _BaseCallHandler<RequestMessage, ResponseMessage> {
  29. public typealias EventObserver = (RequestMessage) -> EventLoopFuture<GRPCStatus>
  30. private var eventObserver: EventObserver?
  31. private var callContext: StreamingResponseCallContext<ResponseMessage>?
  32. public init(
  33. callHandlerContext: CallHandlerContext,
  34. eventObserverFactory: (StreamingResponseCallContext<ResponseMessage>) -> EventObserver
  35. ) {
  36. super.init(callHandlerContext: callHandlerContext)
  37. let callContext = StreamingResponseCallContextImpl<ResponseMessage>(
  38. channel: self.callHandlerContext.channel,
  39. request: self.callHandlerContext.request,
  40. errorDelegate: self.callHandlerContext.errorDelegate,
  41. logger: self.callHandlerContext.logger
  42. )
  43. self.callContext = callContext
  44. self.eventObserver = eventObserverFactory(callContext)
  45. callContext.statusPromise.futureResult.whenComplete { _ in
  46. // When done, reset references to avoid retain cycles.
  47. self.eventObserver = nil
  48. self.callContext = nil
  49. }
  50. }
  51. override internal func processMessage(_ message: RequestMessage) throws {
  52. guard let eventObserver = self.eventObserver,
  53. let callContext = self.callContext else {
  54. self.logger.error("processMessage(_:) called before the call started or after the call completed")
  55. throw GRPCError.StreamCardinalityViolation(stream: .request).captureContext()
  56. }
  57. let resultFuture = eventObserver(message)
  58. resultFuture
  59. // Fulfil the status promise with whatever status the framework user has provided.
  60. .cascade(to: callContext.statusPromise)
  61. self.eventObserver = nil
  62. }
  63. override internal func endOfStreamReceived() throws {
  64. if self.eventObserver != nil {
  65. throw GRPCError.StreamCardinalityViolation(stream: .request).captureContext()
  66. }
  67. }
  68. override internal func sendErrorStatus(_ status: GRPCStatus) {
  69. self.callContext?.statusPromise.fail(status)
  70. }
  71. }