2
0

ClientCall.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 NIO
  18. import NIOHTTP1
  19. import NIOHTTP2
  20. import NIOHPACK
  21. import SwiftProtobuf
  22. /// Base protocol for a client call to a gRPC service.
  23. public protocol ClientCall {
  24. /// The type of the request message for the call.
  25. associatedtype RequestMessage: Message
  26. /// The type of the response message for the call.
  27. associatedtype ResponseMessage: Message
  28. /// HTTP/2 stream that requests and responses are sent and received on.
  29. var subchannel: EventLoopFuture<Channel> { get }
  30. /// Initial response metadata.
  31. var initialMetadata: EventLoopFuture<HPACKHeaders> { get }
  32. /// Status of this call which may be populated by the server or client.
  33. ///
  34. /// The client may populate the status if, for example, it was not possible to connect to the service.
  35. ///
  36. /// Note: despite `GRPCStatus` conforming to `Error`, the value will be __always__ delivered as a __success__
  37. /// result even if the status represents a __negative__ outcome. This future will __never__ be fulfilled
  38. /// with an error.
  39. var status: EventLoopFuture<GRPCStatus> { get }
  40. /// Trailing response metadata.
  41. var trailingMetadata: EventLoopFuture<HPACKHeaders> { get }
  42. /// Cancel the current call.
  43. ///
  44. /// Closes the HTTP/2 stream once it becomes available. Additional writes to the channel will be ignored.
  45. /// Any unfulfilled promises will be failed with a cancelled status (excepting `status` which will be
  46. /// succeeded, if not already succeeded).
  47. func cancel() -> EventLoopFuture<Void>
  48. func cancel(promise: EventLoopPromise<Void>?)
  49. }
  50. /// A `ClientCall` with request streaming; i.e. client-streaming and bidirectional-streaming.
  51. public protocol StreamingRequestClientCall: ClientCall {
  52. /// Sends a message to the service.
  53. ///
  54. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()` or `sendEnd(promise:)`.
  55. ///
  56. /// - Parameters:
  57. /// - message: The message to
  58. /// - Returns: A future which will be fullfilled when the message has been sent.
  59. func sendMessage(_ message: RequestMessage) -> EventLoopFuture<Void>
  60. /// Sends a message to the service.
  61. ///
  62. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()` or `sendEnd(promise:)`.
  63. ///
  64. /// - Parameters:
  65. /// - message: The message to send.
  66. /// - promise: A promise to be fulfilled when the message has been sent.
  67. func sendMessage(_ message: RequestMessage, promise: EventLoopPromise<Void>?)
  68. /// Sends a sequence of messages to the service.
  69. ///
  70. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()` or `sendEnd(promise:)`.
  71. ///
  72. /// - Parameters:
  73. /// - messages: The sequence of messages to send.
  74. func sendMessages<S: Sequence>(_ messages: S) -> EventLoopFuture<Void> where S.Element == RequestMessage
  75. /// Sends a sequence of messages to the service.
  76. ///
  77. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()` or `sendEnd(promise:)`.
  78. ///
  79. /// - Parameters:
  80. /// - messages: The sequence of messages to send.
  81. /// - promise: A promise to be fulfilled when all messages have been sent successfully.
  82. func sendMessages<S: Sequence>(_ messages: S, promise: EventLoopPromise<Void>?) where S.Element == RequestMessage
  83. /// Returns a future which can be used as a message queue.
  84. ///
  85. /// Callers may use this as such:
  86. /// ```
  87. /// var queue = call.newMessageQueue()
  88. /// for message in messagesToSend {
  89. /// queue = queue.then { call.sendMessage(message) }
  90. /// }
  91. /// ```
  92. ///
  93. /// - Returns: A future which may be used as the head of a message queue.
  94. func newMessageQueue() -> EventLoopFuture<Void>
  95. /// Terminates a stream of messages sent to the service.
  96. ///
  97. /// - Important: This should only ever be called once.
  98. /// - Returns: A future which will be fullfilled when the end has been sent.
  99. func sendEnd() -> EventLoopFuture<Void>
  100. /// Terminates a stream of messages sent to the service.
  101. ///
  102. /// - Important: This should only ever be called once.
  103. /// - Parameter promise: A promise to be fulfilled when the end has been sent.
  104. func sendEnd(promise: EventLoopPromise<Void>?)
  105. }
  106. /// A `ClientCall` with a unary response; i.e. unary and client-streaming.
  107. public protocol UnaryResponseClientCall: ClientCall {
  108. /// The response message returned from the service if the call is successful. This may be failed
  109. /// if the call encounters an error.
  110. ///
  111. /// Callers should rely on the `status` of the call for the canonical outcome.
  112. var response: EventLoopFuture<ResponseMessage> { get }
  113. }
  114. extension StreamingRequestClientCall {
  115. public func sendMessage(_ message: RequestMessage) -> EventLoopFuture<Void> {
  116. return self.subchannel.flatMap { channel in
  117. return channel.writeAndFlush(_GRPCClientRequestPart.message(.init(message)))
  118. }
  119. }
  120. public func sendMessage(_ message: RequestMessage, promise: EventLoopPromise<Void>?) {
  121. self.subchannel.whenSuccess { channel in
  122. channel.writeAndFlush(_GRPCClientRequestPart.message(.init(message)), promise: promise)
  123. }
  124. }
  125. public func sendMessages<S: Sequence>(_ messages: S) -> EventLoopFuture<Void> where S.Element == RequestMessage {
  126. return self.subchannel.flatMap { channel -> EventLoopFuture<Void> in
  127. let writeFutures = messages.map { message in
  128. channel.write(_GRPCClientRequestPart.message(.init(message)))
  129. }
  130. channel.flush()
  131. return EventLoopFuture.andAllSucceed(writeFutures, on: channel.eventLoop)
  132. }
  133. }
  134. public func sendMessages<S: Sequence>(_ messages: S, promise: EventLoopPromise<Void>?) where S.Element == RequestMessage {
  135. if let promise = promise {
  136. self.sendMessages(messages).cascade(to: promise)
  137. } else {
  138. self.subchannel.whenSuccess { channel in
  139. for message in messages {
  140. channel.write(_GRPCClientRequestPart.message(.init(message)), promise: nil)
  141. }
  142. channel.flush()
  143. }
  144. }
  145. }
  146. public func sendEnd() -> EventLoopFuture<Void> {
  147. return self.subchannel.flatMap { channel in
  148. return channel.writeAndFlush(_GRPCClientRequestPart<RequestMessage>.end)
  149. }
  150. }
  151. public func sendEnd(promise: EventLoopPromise<Void>?) {
  152. self.subchannel.whenSuccess { channel in
  153. channel.writeAndFlush(_GRPCClientRequestPart<RequestMessage>.end, promise: promise)
  154. }
  155. }
  156. }