ServerHandlerStateMachine+Actions.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. @usableFromInline
  21. enum HandleMetadataAction: Hashable {
  22. /// Invoke the user handler.
  23. case invokeHandler
  24. /// Cancel the RPC, the metadata was not expected.
  25. case cancel
  26. }
  27. @usableFromInline
  28. enum HandleMessageAction: Hashable {
  29. /// Forward the message to the interceptors, via the interceptor state machine.
  30. case forward
  31. /// Cancel the RPC, the message was not expected.
  32. case cancel
  33. }
  34. /// The same as 'HandleMessageAction.
  35. @usableFromInline
  36. typealias HandleEndAction = HandleMessageAction
  37. @usableFromInline
  38. enum SendMessageAction: Equatable {
  39. /// Intercept the message, but first intercept the headers if they are non-nil. Must go via
  40. /// the interceptor state machine first.
  41. case intercept(headers: HPACKHeaders?)
  42. /// Drop the message.
  43. case drop
  44. }
  45. @usableFromInline
  46. enum SendStatusAction: Equatable {
  47. /// Intercept the status, providing the given trailers.
  48. case intercept(requestHeaders: HPACKHeaders, trailers: HPACKHeaders)
  49. /// Drop the status.
  50. case drop
  51. }
  52. @usableFromInline
  53. enum CancelAction: Hashable {
  54. /// Cancel and nil out the handler 'bits'.
  55. case cancelAndNilOutHandlerComponents
  56. /// Don't do anything.
  57. case none
  58. }
  59. /// Tracks whether response metadata has been written.
  60. @usableFromInline
  61. internal enum ResponseMetadata {
  62. case notWritten(HPACKHeaders)
  63. case written
  64. /// Update the metadata. It must not have been written yet.
  65. @inlinable
  66. mutating func update(_ metadata: HPACKHeaders) {
  67. switch self {
  68. case .notWritten:
  69. self = .notWritten(metadata)
  70. case .written:
  71. assertionFailure("Metadata must not be set after it has been sent")
  72. }
  73. }
  74. /// Returns the metadata if it has not been written and moves the state to
  75. /// `written`. Returns `nil` if it has already been written.
  76. @inlinable
  77. mutating func getIfNotWritten() -> HPACKHeaders? {
  78. switch self {
  79. case let .notWritten(metadata):
  80. self = .written
  81. return metadata
  82. case .written:
  83. return nil
  84. }
  85. }
  86. }
  87. }
  88. #endif // compiler(>=5.6)