BidirectionalStreamingCall.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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<RequestMessage: Message, ResponseMessage: Message>
  30. : BaseClientCall<RequestMessage, ResponseMessage>,
  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 (ResponseMessage) -> 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. options: callOptions
  59. )
  60. let requestHandler = _StreamingRequestChannelHandler<RequestMessage>(requestHead: requestHead)
  61. super.init(
  62. eventLoop: connection.eventLoop,
  63. multiplexer: connection.multiplexer,
  64. callType: .bidirectionalStreaming,
  65. responseHandler: responseHandler,
  66. requestHandler: requestHandler,
  67. logger: logger
  68. )
  69. }
  70. public func newMessageQueue() -> EventLoopFuture<Void> {
  71. return self.messageQueue
  72. }
  73. }