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