ServerHandlerStateMachine+Handling.swift 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. internal struct Handling {
  25. typealias NextStateAndOutput<Output> = ServerHandlerStateMachine.NextStateAndOutput<
  26. ServerHandlerStateMachine.Handling.NextState,
  27. Output
  28. >
  29. /// Whether response headers have been written (they are written lazily rather than on receipt
  30. /// of the request headers).
  31. internal private(set) var headersWritten: Bool
  32. /// A context held by user handler which may be used to alter the response headers or trailers.
  33. internal let context: GRPCAsyncServerCallContext
  34. /// Transition from the 'Idle' state.
  35. init(from state: ServerHandlerStateMachine.Idle, context: GRPCAsyncServerCallContext) {
  36. self.headersWritten = false
  37. self.context = context
  38. }
  39. mutating func handleMetadata() -> Self.NextStateAndOutput<HandleMetadataAction> {
  40. // We are in the 'Handling' state because we received metadata. If we receive it again we
  41. // should cancel the RPC.
  42. return .init(nextState: .handling(self), output: .cancel)
  43. }
  44. mutating func handleMessage() -> Self.NextStateAndOutput<HandleMessageAction> {
  45. // We can always forward a message since receiving the end of the request stream causes a
  46. // transition to the 'draining' state.
  47. return .init(nextState: .handling(self), output: .forward)
  48. }
  49. mutating func handleEnd() -> Self.NextStateAndOutput<HandleEndAction> {
  50. // The request stream is finished: move to the draining state so the user handler can finish
  51. // executing.
  52. return .init(nextState: .draining(from: self), output: .forward)
  53. }
  54. mutating func sendMessage() -> Self.NextStateAndOutput<SendMessageAction> {
  55. let headers: HPACKHeaders?
  56. // We send headers once, lazily, when the first message is sent back.
  57. if self.headersWritten {
  58. headers = nil
  59. } else {
  60. self.headersWritten = true
  61. headers = self.context.initialResponseMetadata
  62. }
  63. return .init(nextState: .handling(self), output: .intercept(headers: headers))
  64. }
  65. mutating func sendStatus() -> Self.NextStateAndOutput<SendStatusAction> {
  66. // Sending the status is the final action taken by the user handler. We can always send
  67. // them from this state and doing so means the user handler has completed.
  68. let trailers = self.context.trailingResponseMetadata
  69. return .init(nextState: .finished(from: self), output: .intercept(trailers: trailers))
  70. }
  71. mutating func cancel() -> Self.NextStateAndOutput<CancelAction> {
  72. return .init(nextState: .finished(from: self), output: .cancelAndNilOutHandlerComponents)
  73. }
  74. }
  75. }
  76. #endif // compiler(>=5.6)