ClientCall.swift 4.3 KB

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