2
0

BidirectionalStreamingCall.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(_:compression:)`` and ``sendMessages(_:compression:)`` 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. onStart: {},
  76. onError: self.responseParts.handleError(_:),
  77. onResponsePart: self.responseParts.handle(_:)
  78. )
  79. }
  80. // MARK: - Requests
  81. /// Sends a message to the service.
  82. ///
  83. /// - Important: Callers must terminate the stream of messages by calling ``sendEnd()`` or
  84. /// ``sendEnd(promise:)``.
  85. ///
  86. /// - Parameters:
  87. /// - message: The message to send.
  88. /// - compression: Whether compression should be used for this message. Ignored if compression
  89. /// was not enabled for the RPC.
  90. /// - promise: A promise to fulfill with the outcome of the send operation.
  91. public func sendMessage(
  92. _ message: RequestPayload,
  93. compression: Compression = .deferToCallDefault,
  94. promise: EventLoopPromise<Void>?
  95. ) {
  96. let compress = self.call.compress(compression)
  97. self.call.send(.message(message, .init(compress: compress, flush: true)), promise: promise)
  98. }
  99. /// Sends a sequence of messages to the service.
  100. ///
  101. /// - Important: Callers must terminate the stream of messages by calling ``sendEnd()`` or
  102. /// ``sendEnd(promise:)``.
  103. ///
  104. /// - Parameters:
  105. /// - messages: The sequence of messages to send.
  106. /// - compression: Whether compression should be used for this message. Ignored if compression
  107. /// was not enabled for the RPC.
  108. /// - promise: A promise to fulfill with the outcome of the send operation. It will only succeed
  109. /// if all messages were written successfully.
  110. public func sendMessages<S>(
  111. _ messages: S,
  112. compression: Compression = .deferToCallDefault,
  113. promise: EventLoopPromise<Void>?
  114. ) where S: Sequence, S.Element == RequestPayload {
  115. self.call.sendMessages(messages, compression: compression, promise: promise)
  116. }
  117. /// Terminates a stream of messages sent to the service.
  118. ///
  119. /// - Important: This should only ever be called once.
  120. /// - Parameter promise: A promise to be fulfilled when the end has been sent.
  121. public func sendEnd(promise: EventLoopPromise<Void>?) {
  122. self.call.send(.end, promise: promise)
  123. }
  124. }