BidirectionalStreamingCall.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 bidirectional-streaming gRPC call. Each response is passed to the provided observer block.
  20. ///
  21. /// Messages should be sent via the `send` method; an `.end` message should be sent
  22. /// to indicate the final message has been sent.
  23. ///
  24. /// The following futures are available to the caller:
  25. /// - `initialMetadata`: the initial metadata returned from the server,
  26. /// - `status`: the status of the gRPC call after it has ended,
  27. /// - `trailingMetadata`: any metadata returned from the server alongside the `status`.
  28. public final class BidirectionalStreamingCall<RequestPayload: GRPCPayload, ResponsePayload: GRPCPayload>
  29. : BaseClientCall<RequestPayload, ResponsePayload>,
  30. StreamingRequestClientCall {
  31. private var messageQueue: EventLoopFuture<Void>
  32. init(
  33. path: String,
  34. scheme: String,
  35. authority: String,
  36. callOptions: CallOptions,
  37. eventLoop: EventLoop,
  38. multiplexer: EventLoopFuture<HTTP2StreamMultiplexer>,
  39. errorDelegate: ClientErrorDelegate?,
  40. logger: Logger,
  41. handler: @escaping (ResponsePayload) -> Void
  42. ) {
  43. self.messageQueue = eventLoop.makeSucceededFuture(())
  44. let requestID = callOptions.requestIDProvider.requestID()
  45. var logger = logger
  46. logger[metadataKey: MetadataKey.requestID] = "\(requestID)"
  47. logger.debug("starting rpc", metadata: ["path": "\(path)"])
  48. let responseHandler = GRPCClientStreamingResponseChannelHandler(
  49. initialMetadataPromise: eventLoop.makePromise(),
  50. trailingMetadataPromise: eventLoop.makePromise(),
  51. statusPromise: eventLoop.makePromise(),
  52. errorDelegate: errorDelegate,
  53. timeout: callOptions.timeout,
  54. logger: logger,
  55. responseHandler: handler
  56. )
  57. let requestHead = _GRPCRequestHead(
  58. scheme: scheme,
  59. path: path,
  60. host: authority,
  61. requestID: requestID,
  62. options: callOptions
  63. )
  64. let requestHandler = _StreamingRequestChannelHandler<RequestPayload>(requestHead: requestHead)
  65. super.init(
  66. eventLoop: eventLoop,
  67. multiplexer: multiplexer,
  68. callType: .bidirectionalStreaming,
  69. callOptions: callOptions,
  70. responseHandler: responseHandler,
  71. requestHandler: requestHandler,
  72. logger: logger
  73. )
  74. }
  75. public func newMessageQueue() -> EventLoopFuture<Void> {
  76. return self.messageQueue
  77. }
  78. }