ServerHandlerStateMachine+Idle.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 'Idle' state nothing has happened. To advance we must either receive metadata (i.e.
  21. /// the request headers) and invoke the handler, or we are cancelled.
  22. @usableFromInline
  23. internal struct Idle {
  24. @usableFromInline
  25. typealias NextStateAndOutput<Output> = ServerHandlerStateMachine.NextStateAndOutput<
  26. ServerHandlerStateMachine.Idle.NextState,
  27. Output
  28. >
  29. /// The state of the inbound stream, i.e. the request stream.
  30. @usableFromInline
  31. internal private(set) var inboundState: ServerInterceptorStateMachine.InboundStreamState
  32. @inlinable
  33. init() {
  34. self.inboundState = .idle
  35. }
  36. @inlinable
  37. mutating func handleMetadata() -> Self.NextStateAndOutput<HandleMetadataAction> {
  38. let action: HandleMetadataAction
  39. switch self.inboundState.receiveMetadata() {
  40. case .accept:
  41. // We tell the caller to invoke the handler immediately: they should then call
  42. // 'handlerInvoked' on the state machine which will cause a transition to the next state.
  43. action = .invokeHandler
  44. case .reject:
  45. action = .cancel
  46. }
  47. return .init(nextState: .idle(self), output: action)
  48. }
  49. @inlinable
  50. mutating func handleMessage() -> Self.NextStateAndOutput<HandleMessageAction> {
  51. // We can't receive a message before the metadata, doing so is a protocol violation.
  52. return .init(nextState: .idle(self), output: .cancel)
  53. }
  54. @inlinable
  55. mutating func handleEnd() -> Self.NextStateAndOutput<HandleEndAction> {
  56. // Receiving 'end' before we start is odd but okay, just cancel.
  57. return .init(nextState: .idle(self), output: .cancel)
  58. }
  59. @inlinable
  60. mutating func handlerInvoked(requestHeaders: HPACKHeaders) -> Self.NextStateAndOutput<Void> {
  61. // The handler was invoked as a result of receiving metadata. Move to the next state.
  62. return .init(nextState: .handling(from: self, requestHeaders: requestHeaders))
  63. }
  64. @inlinable
  65. mutating func cancel() -> Self.NextStateAndOutput<CancelAction> {
  66. // There's no handler to cancel. Move straight to finished.
  67. return .init(nextState: .finished(from: self), output: .none)
  68. }
  69. }
  70. }
  71. #endif // compiler(>=5.6)