2
0

ClientCall.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 SwiftProtobuf
  21. /// Base protocol for a client call to a gRPC service.
  22. public protocol ClientCall {
  23. /// The type of the request message for the call.
  24. associatedtype RequestMessage: Message
  25. /// The type of the response message for the call.
  26. associatedtype ResponseMessage: Message
  27. /// HTTP/2 stream that requests and responses are sent and received on.
  28. var subchannel: EventLoopFuture<Channel> { get }
  29. /// Initial response metadata.
  30. var initialMetadata: EventLoopFuture<HTTPHeaders> { get }
  31. /// Status of this call which may be populated by the server or client.
  32. ///
  33. /// The client may populate the status if, for example, it was not possible to connect to the service.
  34. ///
  35. /// Note: despite `GRPCStatus` conforming to `Error`, the value will be __always__ delivered as a __success__
  36. /// result even if the status represents a __negative__ outcome. This future will __never__ be fulfilled
  37. /// with an error.
  38. var status: EventLoopFuture<GRPCStatus> { get }
  39. /// Trailing response metadata.
  40. ///
  41. /// This is the same metadata as `GRPCStatus.trailingMetadata` returned by `status`.
  42. var trailingMetadata: EventLoopFuture<HTTPHeaders> { get }
  43. /// Cancel the current call.
  44. ///
  45. /// Closes the HTTP/2 stream once it becomes available. Additional writes to the channel will be ignored.
  46. /// Any unfulfilled promises will be failed with a cancelled status (excepting `status` which will be
  47. /// succeeded, if not already succeeded).
  48. func cancel()
  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. /// - flush: Whether the buffer should be flushed after writing the message.
  59. /// - Returns: A future which will be fullfilled when the message has been sent.
  60. func sendMessage(_ message: RequestMessage, flush: Bool) -> EventLoopFuture<Void>
  61. /// Sends a message to the service.
  62. ///
  63. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()` or `sendEnd(promise:)`.
  64. ///
  65. /// - Parameters:
  66. /// - message: The message to send.
  67. /// - promise: A promise to be fulfilled when the message has been sent.
  68. /// - flush: Whether the buffer should be flushed after writing the message.
  69. func sendMessage(_ message: RequestMessage, promise: EventLoopPromise<Void>?, flush: Bool)
  70. /// Returns a future which can be used as a message queue.
  71. ///
  72. /// Callers may use this as such:
  73. /// ```
  74. /// var queue = call.newMessageQueue()
  75. /// for message in messagesToSend {
  76. /// queue = queue.then { call.sendMessage(message) }
  77. /// }
  78. /// ```
  79. ///
  80. /// - Returns: A future which may be used as the head of a message queue.
  81. func newMessageQueue() -> EventLoopFuture<Void>
  82. /// Terminates a stream of messages sent to the service.
  83. ///
  84. /// - Important: This should only ever be called once.
  85. /// - Returns: A future which will be fullfilled when the end has been sent.
  86. func sendEnd() -> EventLoopFuture<Void>
  87. /// Terminates a stream of messages sent to the service.
  88. ///
  89. /// - Important: This should only ever be called once.
  90. /// - Parameter promise: A promise to be fulfilled when the end has been sent.
  91. func sendEnd(promise: EventLoopPromise<Void>?)
  92. }
  93. /// A `ClientCall` with a unary response; i.e. unary and client-streaming.
  94. public protocol UnaryResponseClientCall: ClientCall {
  95. /// The response message returned from the service if the call is successful. This may be failed
  96. /// if the call encounters an error.
  97. ///
  98. /// Callers should rely on the `status` of the call for the canonical outcome.
  99. var response: EventLoopFuture<ResponseMessage> { get }
  100. }
  101. extension StreamingRequestClientCall {
  102. public func sendMessage(_ message: RequestMessage, flush: Bool = true) -> EventLoopFuture<Void> {
  103. return self.subchannel.flatMap { channel in
  104. let writeFuture = channel.write(GRPCClientRequestPart.message(_Box(message)))
  105. if flush {
  106. channel.flush()
  107. }
  108. return writeFuture
  109. }
  110. }
  111. public func sendMessage(_ message: RequestMessage, promise: EventLoopPromise<Void>?, flush: Bool = true) {
  112. self.subchannel.whenSuccess { channel in
  113. channel.write(GRPCClientRequestPart.message(_Box(message)), promise: promise)
  114. if flush {
  115. channel.flush()
  116. }
  117. }
  118. }
  119. public func sendEnd() -> EventLoopFuture<Void> {
  120. return self.subchannel.flatMap { channel in
  121. return channel.writeAndFlush(GRPCClientRequestPart<RequestMessage>.end)
  122. }
  123. }
  124. public func sendEnd(promise: EventLoopPromise<Void>?) {
  125. self.subchannel.whenSuccess { channel in
  126. channel.writeAndFlush(GRPCClientRequestPart<RequestMessage>.end, promise: promise)
  127. }
  128. }
  129. }