BidirectionalStreamingCall.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 NIOCore
  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 path used to make the RPC.
  38. public var path: String {
  39. return self.call.path
  40. }
  41. /// The `Channel` used to transport messages for this RPC.
  42. public var subchannel: EventLoopFuture<Channel> {
  43. return self.call.channel
  44. }
  45. /// The `EventLoop` this call is running on.
  46. public var eventLoop: EventLoop {
  47. return self.call.eventLoop
  48. }
  49. /// Cancel this RPC if it hasn't already completed.
  50. public func cancel(promise: EventLoopPromise<Void>?) {
  51. self.call.cancel(promise: promise)
  52. }
  53. // MARK: - Response Parts
  54. /// The initial metadata returned from the server.
  55. public var initialMetadata: EventLoopFuture<HPACKHeaders> {
  56. return self.responseParts.initialMetadata
  57. }
  58. /// The trailing metadata returned from the server.
  59. public var trailingMetadata: EventLoopFuture<HPACKHeaders> {
  60. return self.responseParts.trailingMetadata
  61. }
  62. /// The final status of the the RPC.
  63. public var status: EventLoopFuture<GRPCStatus> {
  64. return self.responseParts.status
  65. }
  66. internal init(
  67. call: Call<RequestPayload, ResponsePayload>,
  68. callback: @escaping (ResponsePayload) -> Void
  69. ) {
  70. self.call = call
  71. self.responseParts = StreamingResponseParts(on: call.eventLoop, callback)
  72. }
  73. internal func invoke() {
  74. self.call.invokeStreamingRequests(
  75. onError: self.responseParts.handleError(_:),
  76. onResponsePart: self.responseParts.handle(_:)
  77. )
  78. }
  79. // MARK: - Requests
  80. /// Sends a message to the service.
  81. ///
  82. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()` or
  83. /// `sendEnd(promise:)`.
  84. ///
  85. /// - Parameters:
  86. /// - message: The message to send.
  87. /// - compression: Whether compression should be used for this message. Ignored if compression
  88. /// was not enabled for the RPC.
  89. /// - promise: A promise to fulfill with the outcome of the send operation.
  90. public func sendMessage(
  91. _ message: RequestPayload,
  92. compression: Compression = .deferToCallDefault,
  93. promise: EventLoopPromise<Void>?
  94. ) {
  95. let compress = self.call.compress(compression)
  96. self.call.send(.message(message, .init(compress: compress, flush: true)), promise: promise)
  97. }
  98. /// Sends a sequence of messages to the service.
  99. ///
  100. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()` or
  101. /// `sendEnd(promise:)`.
  102. ///
  103. /// - Parameters:
  104. /// - messages: The sequence of messages to send.
  105. /// - compression: Whether compression should be used for this message. Ignored if compression
  106. /// was not enabled for the RPC.
  107. /// - promise: A promise to fulfill with the outcome of the send operation. It will only succeed
  108. /// if all messages were written successfully.
  109. public func sendMessages<S>(
  110. _ messages: S,
  111. compression: Compression = .deferToCallDefault,
  112. promise: EventLoopPromise<Void>?
  113. ) where S: Sequence, S.Element == RequestPayload {
  114. self.call.sendMessages(messages, compression: compression, promise: promise)
  115. }
  116. /// Terminates a stream of messages sent to the service.
  117. ///
  118. /// - Important: This should only ever be called once.
  119. /// - Parameter promise: A promise to be fulfilled when the end has been sent.
  120. public func sendEnd(promise: EventLoopPromise<Void>?) {
  121. self.call.send(.end, promise: promise)
  122. }
  123. }