ServerHandlerStateMachine+Handling.swift 4.1 KB

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