ServerHandlerStateMachine+Idle.swift 2.9 KB

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