BidirectionalStreamingCall.swift 3.0 KB

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