ServerHandlerStateMachine+Actions.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. @usableFromInline
  20. enum HandleMetadataAction: Hashable {
  21. /// Invoke the user handler.
  22. case invokeHandler
  23. /// Cancel the RPC, the metadata was not expected.
  24. case cancel
  25. }
  26. @usableFromInline
  27. enum HandleMessageAction: Hashable {
  28. /// Forward the message to the interceptors, via the interceptor state machine.
  29. case forward
  30. /// Cancel the RPC, the message was not expected.
  31. case cancel
  32. }
  33. /// The same as 'HandleMessageAction.
  34. @usableFromInline
  35. typealias HandleEndAction = HandleMessageAction
  36. @usableFromInline
  37. enum SendMessageAction: Equatable {
  38. /// Intercept the message, but first intercept the headers if they are non-nil. Must go via
  39. /// the interceptor state machine first.
  40. case intercept(headers: HPACKHeaders?)
  41. /// Drop the message.
  42. case drop
  43. }
  44. @usableFromInline
  45. enum SendStatusAction: Equatable {
  46. /// Intercept the status, providing the given trailers.
  47. case intercept(requestHeaders: HPACKHeaders, trailers: HPACKHeaders)
  48. /// Drop the status.
  49. case drop
  50. }
  51. @usableFromInline
  52. enum CancelAction: Hashable {
  53. /// Cancel and nil out the handler 'bits'.
  54. case cancelAndNilOutHandlerComponents
  55. /// Don't do anything.
  56. case none
  57. }
  58. /// Tracks whether response metadata has been written.
  59. @usableFromInline
  60. internal enum ResponseMetadata {
  61. case notWritten(HPACKHeaders)
  62. case written
  63. /// Update the metadata. It must not have been written yet.
  64. @inlinable
  65. mutating func update(_ metadata: HPACKHeaders) -> Bool {
  66. switch self {
  67. case .notWritten:
  68. self = .notWritten(metadata)
  69. return true
  70. case .written:
  71. return false
  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. }