2
0

ServerStreamingCall.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 NIO
  17. import NIOHTTP2
  18. import Logging
  19. /// A server-streaming gRPC call. The request is sent on initialization, each response is passed to the provided observer block.
  20. ///
  21. /// The following futures are available to the caller:
  22. /// - `initialMetadata`: the initial metadata returned from the server,
  23. /// - `status`: the status of the gRPC call after it has ended,
  24. /// - `trailingMetadata`: any metadata returned from the server alongside the `status`.
  25. public final class ServerStreamingCall<RequestPayload: GRPCPayload, ResponsePayload: GRPCPayload>: BaseClientCall<RequestPayload, ResponsePayload> {
  26. init(
  27. path: String,
  28. scheme: String,
  29. authority: String,
  30. callOptions: CallOptions,
  31. eventLoop: EventLoop,
  32. multiplexer: EventLoopFuture<HTTP2StreamMultiplexer>,
  33. errorDelegate: ClientErrorDelegate?,
  34. logger: Logger,
  35. request: RequestPayload,
  36. handler: @escaping (ResponsePayload) -> Void
  37. ) {
  38. let requestID = callOptions.requestIDProvider.requestID()
  39. var logger = logger
  40. logger[metadataKey: MetadataKey.requestID] = "\(requestID)"
  41. logger.debug("starting rpc", metadata: ["path": "\(path)"])
  42. let responseHandler = GRPCClientStreamingResponseChannelHandler(
  43. initialMetadataPromise: eventLoop.makePromise(),
  44. trailingMetadataPromise: eventLoop.makePromise(),
  45. statusPromise: eventLoop.makePromise(),
  46. errorDelegate: errorDelegate,
  47. timeout: callOptions.timeout,
  48. logger: logger,
  49. responseHandler: handler
  50. )
  51. let requestHead = _GRPCRequestHead(
  52. scheme: scheme,
  53. path: path,
  54. host: authority,
  55. requestID: requestID,
  56. options: callOptions
  57. )
  58. let requestHandler = _UnaryRequestChannelHandler<RequestPayload>(
  59. requestHead: requestHead,
  60. request: .init(request, compressed: callOptions.messageEncoding.enabledForRequests)
  61. )
  62. super.init(
  63. eventLoop: eventLoop,
  64. multiplexer: multiplexer,
  65. callType: .serverStreaming,
  66. callOptions: callOptions,
  67. responseHandler: responseHandler,
  68. requestHandler: requestHandler,
  69. logger: logger
  70. )
  71. }
  72. }