ServerHandlerStateMachine+Handling.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2022, 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 NIOHPACK
  17. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  18. extension ServerHandlerStateMachine {
  19. /// In the 'Handling' state the user handler has been invoked and the request stream is open (but
  20. /// the request metadata has already been seen). We can transition to a new state either by
  21. /// receiving the end of the request stream or by closing the response stream. Cancelling also
  22. /// moves us to the finished state.
  23. @usableFromInline
  24. internal struct Handling {
  25. @usableFromInline
  26. typealias NextStateAndOutput<Output> = ServerHandlerStateMachine.NextStateAndOutput<
  27. ServerHandlerStateMachine.Handling.NextState,
  28. Output
  29. >
  30. /// The response headers.
  31. @usableFromInline
  32. internal private(set) var responseHeaders: ResponseMetadata
  33. /// The response trailers.
  34. @usableFromInline
  35. internal private(set) var responseTrailers: ResponseMetadata
  36. /// The request headers.
  37. @usableFromInline
  38. internal let requestHeaders: HPACKHeaders
  39. /// Transition from the 'Idle' state.
  40. @inlinable
  41. init(from state: ServerHandlerStateMachine.Idle, requestHeaders: HPACKHeaders) {
  42. self.responseHeaders = .notWritten([:])
  43. self.responseTrailers = .notWritten([:])
  44. self.requestHeaders = requestHeaders
  45. }
  46. @inlinable
  47. mutating func setResponseHeaders(
  48. _ metadata: HPACKHeaders
  49. ) -> Self.NextStateAndOutput<Void> {
  50. self.responseHeaders.update(metadata)
  51. return .init(nextState: .handling(self))
  52. }
  53. @inlinable
  54. mutating func setResponseTrailers(
  55. _ metadata: HPACKHeaders
  56. ) -> Self.NextStateAndOutput<Void> {
  57. self.responseTrailers.update(metadata)
  58. return .init(nextState: .handling(self))
  59. }
  60. @inlinable
  61. mutating func handleMetadata() -> Self.NextStateAndOutput<HandleMetadataAction> {
  62. // We are in the 'Handling' state because we received metadata. If we receive it again we
  63. // should cancel the RPC.
  64. return .init(nextState: .handling(self), output: .cancel)
  65. }
  66. @inlinable
  67. mutating func handleMessage() -> Self.NextStateAndOutput<HandleMessageAction> {
  68. // We can always forward a message since receiving the end of the request stream causes a
  69. // transition to the 'draining' state.
  70. return .init(nextState: .handling(self), output: .forward)
  71. }
  72. @inlinable
  73. mutating func handleEnd() -> Self.NextStateAndOutput<HandleEndAction> {
  74. // The request stream is finished: move to the draining state so the user handler can finish
  75. // executing.
  76. return .init(nextState: .draining(from: self), output: .forward)
  77. }
  78. @inlinable
  79. mutating func sendMessage() -> Self.NextStateAndOutput<SendMessageAction> {
  80. let headers = self.responseHeaders.getIfNotWritten()
  81. return .init(nextState: .handling(self), output: .intercept(headers: headers))
  82. }
  83. @inlinable
  84. mutating func sendStatus() -> Self.NextStateAndOutput<SendStatusAction> {
  85. return .init(
  86. nextState: .finished(from: self),
  87. output: .intercept(
  88. requestHeaders: self.requestHeaders,
  89. // If trailers had been written we'd already be in the finished state so
  90. // the force unwrap is okay here.
  91. trailers: self.responseTrailers.getIfNotWritten()!
  92. )
  93. )
  94. }
  95. @inlinable
  96. mutating func cancel() -> Self.NextStateAndOutput<CancelAction> {
  97. return .init(nextState: .finished(from: self), output: .cancelAndNilOutHandlerComponents)
  98. }
  99. }
  100. }