ServerStreamingCall.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 Logging
  20. /// A server-streaming gRPC call. The request is sent on initialization, each response is passed to the provided observer block.
  21. ///
  22. /// The following futures are available to the caller:
  23. /// - `initialMetadata`: the initial metadata returned from the server,
  24. /// - `status`: the status of the gRPC call after it has ended,
  25. /// - `trailingMetadata`: any metadata returned from the server alongside the `status`.
  26. public final class ServerStreamingCall<RequestMessage: Message, ResponseMessage: Message>: BaseClientCall<RequestMessage, ResponseMessage> {
  27. public init(connection: ClientConnection, path: String, request: RequestMessage, callOptions: CallOptions, errorDelegate: ClientErrorDelegate?, handler: @escaping (ResponseMessage) -> Void) {
  28. let requestID = callOptions.requestIDProvider.requestID()
  29. let logger = Logger(subsystem: .clientChannelCall, requestID: requestID)
  30. logger.info("making server streaming call to '\(path)', request type: \(RequestMessage.self), response type: \(ResponseMessage.self)")
  31. let responseHandler = GRPCClientStreamingResponseChannelHandler(
  32. initialMetadataPromise: connection.channel.eventLoop.makePromise(),
  33. statusPromise: connection.channel.eventLoop.makePromise(),
  34. errorDelegate: errorDelegate,
  35. timeout: callOptions.timeout,
  36. logger: logger,
  37. responseHandler: handler)
  38. let requestHandler = UnaryRequestChannelHandler<RequestMessage>(
  39. requestHead: makeRequestHead(
  40. path: path,
  41. host: connection.configuration.target.host,
  42. callOptions: callOptions,
  43. requestID: requestID
  44. ),
  45. request: _Box(request)
  46. )
  47. super.init(
  48. connection: connection,
  49. responseHandler: responseHandler,
  50. requestHandler: requestHandler,
  51. logger: logger
  52. )
  53. }
  54. }