ServerStreamingCall.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 server-streaming gRPC call. The request is sent on initialization, each response is passed to
  21. /// the provided observer block.
  22. ///
  23. /// Note: while this object is a `struct`, its implementation delegates to `Call`. It therefore
  24. /// has reference semantics.
  25. public struct ServerStreamingCall<RequestPayload, ResponsePayload>: ClientCall {
  26. private let call: Call<RequestPayload, ResponsePayload>
  27. private let responseParts: StreamingResponseParts<ResponsePayload>
  28. /// The options used to make the RPC.
  29. public var options: CallOptions {
  30. return self.call.options
  31. }
  32. /// The `Channel` used to transport messages for this RPC.
  33. public var subchannel: EventLoopFuture<Channel> {
  34. return self.call.channel
  35. }
  36. /// The `EventLoop` this call is running on.
  37. public var eventLoop: EventLoop {
  38. return self.call.eventLoop
  39. }
  40. /// Cancel this RPC if it hasn't already completed.
  41. public func cancel(promise: EventLoopPromise<Void>?) {
  42. self.call.cancel(promise: promise)
  43. }
  44. // MARK: - Response Parts
  45. /// The initial metadata returned from the server.
  46. public var initialMetadata: EventLoopFuture<HPACKHeaders> {
  47. return self.responseParts.initialMetadata
  48. }
  49. /// The trailing metadata returned from the server.
  50. public var trailingMetadata: EventLoopFuture<HPACKHeaders> {
  51. return self.responseParts.trailingMetadata
  52. }
  53. /// The final status of the the RPC.
  54. public var status: EventLoopFuture<GRPCStatus> {
  55. return self.responseParts.status
  56. }
  57. internal init(
  58. call: Call<RequestPayload, ResponsePayload>,
  59. callback: @escaping (ResponsePayload) -> Void
  60. ) {
  61. self.call = call
  62. self.responseParts = StreamingResponseParts(on: call.eventLoop, callback)
  63. }
  64. internal func invoke(_ request: RequestPayload) {
  65. self.call.invokeUnaryRequest(
  66. request,
  67. onError: self.responseParts.handleError(_:),
  68. onResponsePart: self.responseParts.handle(_:)
  69. )
  70. }
  71. }