ClientCall.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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` being an `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. extension ClientCall {
  51. public var trailingMetadata: EventLoopFuture<HTTPHeaders> {
  52. return status.map { $0.trailingMetadata }
  53. }
  54. }
  55. /// A `ClientCall` with request streaming; i.e. client-streaming and bidirectional-streaming.
  56. public protocol StreamingRequestClientCall: ClientCall {
  57. /// Sends a message to the service.
  58. ///
  59. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()` or `sendEnd(promise:)`.
  60. ///
  61. /// - Parameter message: The message to send.
  62. /// - Returns: A future which will be fullfilled when the message has been sent.
  63. func sendMessage(_ message: RequestMessage) -> EventLoopFuture<Void>
  64. /// Sends a message to the service.
  65. ///
  66. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()` or `sendEnd(promise:)`.
  67. ///
  68. /// - Parameters:
  69. /// - message: The message to send.
  70. /// - promise: A promise to be fulfilled when the message has been sent.
  71. func sendMessage(_ message: RequestMessage, promise: EventLoopPromise<Void>?)
  72. /// Returns a future which can be used as a message queue.
  73. ///
  74. /// Callers may use this as such:
  75. /// ```
  76. /// var queue = call.newMessageQueue()
  77. /// for message in messagesToSend {
  78. /// queue = queue.then { call.sendMessage(message) }
  79. /// }
  80. /// ```
  81. ///
  82. /// - Returns: A future which may be used as the head of a message queue.
  83. func newMessageQueue() -> EventLoopFuture<Void>
  84. /// Terminates a stream of messages sent to the service.
  85. ///
  86. /// - Important: This should only ever be called once.
  87. /// - Returns: A future which will be fullfilled when the end has been sent.
  88. func sendEnd() -> EventLoopFuture<Void>
  89. /// Terminates a stream of messages sent to the service.
  90. ///
  91. /// - Important: This should only ever be called once.
  92. /// - Parameter promise: A promise to be fulfilled when the end has been sent.
  93. func sendEnd(promise: EventLoopPromise<Void>?)
  94. }
  95. /// A `ClientCall` with a unary response; i.e. unary and client-streaming.
  96. public protocol UnaryResponseClientCall: ClientCall {
  97. /// The response message returned from the service if the call is successful. This may be failed
  98. /// if the call encounters an error.
  99. ///
  100. /// Callers should rely on the `status` of the call for the canonical outcome.
  101. var response: EventLoopFuture<ResponseMessage> { get }
  102. }