BidirectionalStreamingCall.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Logging
  17. import NIO
  18. import NIOHPACK
  19. import NIOHTTP2
  20. /// A bidirectional-streaming gRPC call. Each response is passed to the provided observer block.
  21. ///
  22. /// Messages should be sent via the `sendMessage` and `sendMessages` methods; the stream of messages
  23. /// must be terminated by calling `sendEnd` to indicate the final message has been sent.
  24. ///
  25. /// Note: while this object is a `struct`, its implementation delegates to `Call`. It therefore
  26. /// has reference semantics.
  27. public struct BidirectionalStreamingCall<
  28. RequestPayload,
  29. ResponsePayload
  30. >: StreamingRequestClientCall {
  31. private let call: Call<RequestPayload, ResponsePayload>
  32. private let responseParts: StreamingResponseParts<ResponsePayload>
  33. /// The options used to make the RPC.
  34. public var options: CallOptions {
  35. return self.call.options
  36. }
  37. /// The `Channel` used to transport messages for this RPC.
  38. public var subchannel: EventLoopFuture<Channel> {
  39. return self.call.channel
  40. }
  41. /// The `EventLoop` this call is running on.
  42. public var eventLoop: EventLoop {
  43. return self.call.eventLoop
  44. }
  45. /// Cancel this RPC if it hasn't already completed.
  46. public func cancel(promise: EventLoopPromise<Void>?) {
  47. self.call.cancel(promise: promise)
  48. }
  49. // MARK: - Response Parts
  50. /// The initial metadata returned from the server.
  51. public var initialMetadata: EventLoopFuture<HPACKHeaders> {
  52. return self.responseParts.initialMetadata
  53. }
  54. /// The trailing metadata returned from the server.
  55. public var trailingMetadata: EventLoopFuture<HPACKHeaders> {
  56. return self.responseParts.trailingMetadata
  57. }
  58. /// The final status of the the RPC.
  59. public var status: EventLoopFuture<GRPCStatus> {
  60. return self.responseParts.status
  61. }
  62. internal init(
  63. call: Call<RequestPayload, ResponsePayload>,
  64. callback: @escaping (ResponsePayload) -> Void
  65. ) {
  66. self.call = call
  67. self.responseParts = StreamingResponseParts(on: call.eventLoop, callback)
  68. }
  69. internal func invoke() {
  70. self.call.invokeStreamingRequests(
  71. onError: self.responseParts.handleError(_:),
  72. onResponsePart: self.responseParts.handle(_:)
  73. )
  74. }
  75. // MARK: - Requests
  76. /// Sends a message to the service.
  77. ///
  78. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()` or
  79. /// `sendEnd(promise:)`.
  80. ///
  81. /// - Parameters:
  82. /// - message: The message to send.
  83. /// - compression: Whether compression should be used for this message. Ignored if compression
  84. /// was not enabled for the RPC.
  85. /// - promise: A promise to fulfill with the outcome of the send operation.
  86. public func sendMessage(
  87. _ message: RequestPayload,
  88. compression: Compression = .deferToCallDefault,
  89. promise: EventLoopPromise<Void>?
  90. ) {
  91. let compress = self.call.compress(compression)
  92. self.call.send(.message(message, .init(compress: compress, flush: true)), promise: promise)
  93. }
  94. /// Sends a sequence of messages to the service.
  95. ///
  96. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()` or
  97. /// `sendEnd(promise:)`.
  98. ///
  99. /// - Parameters:
  100. /// - messages: The sequence of messages to send.
  101. /// - compression: Whether compression should be used for this message. Ignored if compression
  102. /// was not enabled for the RPC.
  103. /// - promise: A promise to fulfill with the outcome of the send operation. It will only succeed
  104. /// if all messages were written successfully.
  105. public func sendMessages<S>(
  106. _ messages: S,
  107. compression: Compression = .deferToCallDefault,
  108. promise: EventLoopPromise<Void>?
  109. ) where S: Sequence, S.Element == RequestPayload {
  110. self.call.sendMessages(messages, compression: compression, promise: promise)
  111. }
  112. /// Terminates a stream of messages sent to the service.
  113. ///
  114. /// - Important: This should only ever be called once.
  115. /// - Parameter promise: A promise to be fulfilled when the end has been sent.
  116. public func sendEnd(promise: EventLoopPromise<Void>?) {
  117. self.call.send(.end, promise: promise)
  118. }
  119. }