ServerHandlerStateMachine.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. @usableFromInline
  19. internal struct ServerHandlerStateMachine {
  20. @usableFromInline
  21. internal private(set) var state: Self.State
  22. @inlinable
  23. init() {
  24. self.state = .idle(.init())
  25. }
  26. @inlinable
  27. mutating func setResponseHeaders(_ headers: HPACKHeaders) -> Bool {
  28. switch self.state {
  29. case var .handling(handling):
  30. let nextStateAndOutput = handling.setResponseHeaders(headers)
  31. self.state = nextStateAndOutput.nextState.state
  32. return nextStateAndOutput.output
  33. case var .draining(draining):
  34. let nextStateAndOutput = draining.setResponseHeaders(headers)
  35. self.state = nextStateAndOutput.nextState.state
  36. return nextStateAndOutput.output
  37. case var .finished(finished):
  38. let nextStateAndOutput = finished.setResponseHeaders(headers)
  39. self.state = nextStateAndOutput.nextState.state
  40. return nextStateAndOutput.output
  41. case .idle:
  42. preconditionFailure()
  43. }
  44. }
  45. @inlinable
  46. mutating func setResponseTrailers(_ trailers: HPACKHeaders) {
  47. switch self.state {
  48. case var .handling(handling):
  49. let nextStateAndOutput = handling.setResponseTrailers(trailers)
  50. self.state = nextStateAndOutput.nextState.state
  51. return nextStateAndOutput.output
  52. case var .draining(draining):
  53. let nextStateAndOutput = draining.setResponseTrailers(trailers)
  54. self.state = nextStateAndOutput.nextState.state
  55. return nextStateAndOutput.output
  56. case var .finished(finished):
  57. let nextStateAndOutput = finished.setResponseTrailers(trailers)
  58. self.state = nextStateAndOutput.nextState.state
  59. return nextStateAndOutput.output
  60. case .idle:
  61. preconditionFailure()
  62. }
  63. }
  64. @inlinable
  65. mutating func handleMetadata() -> HandleMetadataAction {
  66. switch self.state {
  67. case var .idle(idle):
  68. let nextStateAndOutput = idle.handleMetadata()
  69. self.state = nextStateAndOutput.nextState.state
  70. return nextStateAndOutput.output
  71. case var .handling(handling):
  72. let nextStateAndOutput = handling.handleMetadata()
  73. self.state = nextStateAndOutput.nextState.state
  74. return nextStateAndOutput.output
  75. case var .draining(draining):
  76. let nextStateAndOutput = draining.handleMetadata()
  77. self.state = nextStateAndOutput.nextState.state
  78. return nextStateAndOutput.output
  79. case var .finished(finished):
  80. let nextStateAndOutput = finished.handleMetadata()
  81. self.state = nextStateAndOutput.nextState.state
  82. return nextStateAndOutput.output
  83. }
  84. }
  85. @inlinable
  86. mutating func handleMessage() -> HandleMessageAction {
  87. switch self.state {
  88. case var .idle(idle):
  89. let nextStateAndOutput = idle.handleMessage()
  90. self.state = nextStateAndOutput.nextState.state
  91. return nextStateAndOutput.output
  92. case var .handling(handling):
  93. let nextStateAndOutput = handling.handleMessage()
  94. self.state = nextStateAndOutput.nextState.state
  95. return nextStateAndOutput.output
  96. case var .draining(draining):
  97. let nextStateAndOutput = draining.handleMessage()
  98. self.state = nextStateAndOutput.nextState.state
  99. return nextStateAndOutput.output
  100. case var .finished(finished):
  101. let nextStateAndOutput = finished.handleMessage()
  102. self.state = nextStateAndOutput.nextState.state
  103. return nextStateAndOutput.output
  104. }
  105. }
  106. @inlinable
  107. mutating func handleEnd() -> HandleEndAction {
  108. switch self.state {
  109. case var .idle(idle):
  110. let nextStateAndOutput = idle.handleEnd()
  111. self.state = nextStateAndOutput.nextState.state
  112. return nextStateAndOutput.output
  113. case var .handling(handling):
  114. let nextStateAndOutput = handling.handleEnd()
  115. self.state = nextStateAndOutput.nextState.state
  116. return nextStateAndOutput.output
  117. case var .draining(draining):
  118. let nextStateAndOutput = draining.handleEnd()
  119. self.state = nextStateAndOutput.nextState.state
  120. return nextStateAndOutput.output
  121. case var .finished(finished):
  122. let nextStateAndOutput = finished.handleEnd()
  123. self.state = nextStateAndOutput.nextState.state
  124. return nextStateAndOutput.output
  125. }
  126. }
  127. @inlinable
  128. mutating func sendMessage() -> SendMessageAction {
  129. switch self.state {
  130. case var .handling(handling):
  131. let nextStateAndOutput = handling.sendMessage()
  132. self.state = nextStateAndOutput.nextState.state
  133. return nextStateAndOutput.output
  134. case var .draining(draining):
  135. let nextStateAndOutput = draining.sendMessage()
  136. self.state = nextStateAndOutput.nextState.state
  137. return nextStateAndOutput.output
  138. case var .finished(finished):
  139. let nextStateAndOutput = finished.sendMessage()
  140. self.state = nextStateAndOutput.nextState.state
  141. return nextStateAndOutput.output
  142. case .idle:
  143. preconditionFailure()
  144. }
  145. }
  146. @inlinable
  147. mutating func sendStatus() -> SendStatusAction {
  148. switch self.state {
  149. case var .handling(handling):
  150. let nextStateAndOutput = handling.sendStatus()
  151. self.state = nextStateAndOutput.nextState.state
  152. return nextStateAndOutput.output
  153. case var .draining(draining):
  154. let nextStateAndOutput = draining.sendStatus()
  155. self.state = nextStateAndOutput.nextState.state
  156. return nextStateAndOutput.output
  157. case var .finished(finished):
  158. let nextStateAndOutput = finished.sendStatus()
  159. self.state = nextStateAndOutput.nextState.state
  160. return nextStateAndOutput.output
  161. case .idle:
  162. preconditionFailure()
  163. }
  164. }
  165. @inlinable
  166. mutating func cancel() -> CancelAction {
  167. switch self.state {
  168. case var .idle(idle):
  169. let nextStateAndOutput = idle.cancel()
  170. self.state = nextStateAndOutput.nextState.state
  171. return nextStateAndOutput.output
  172. case var .handling(handling):
  173. let nextStateAndOutput = handling.cancel()
  174. self.state = nextStateAndOutput.nextState.state
  175. return nextStateAndOutput.output
  176. case var .draining(draining):
  177. let nextStateAndOutput = draining.cancel()
  178. self.state = nextStateAndOutput.nextState.state
  179. return nextStateAndOutput.output
  180. case var .finished(finished):
  181. let nextStateAndOutput = finished.cancel()
  182. self.state = nextStateAndOutput.nextState.state
  183. return nextStateAndOutput.output
  184. }
  185. }
  186. @inlinable
  187. mutating func handlerInvoked(requestHeaders: HPACKHeaders) {
  188. switch self.state {
  189. case var .idle(idle):
  190. let nextStateAndOutput = idle.handlerInvoked(requestHeaders: requestHeaders)
  191. self.state = nextStateAndOutput.nextState.state
  192. return nextStateAndOutput.output
  193. case .handling:
  194. preconditionFailure()
  195. case .draining:
  196. preconditionFailure()
  197. case .finished:
  198. preconditionFailure()
  199. }
  200. }
  201. }
  202. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  203. extension ServerHandlerStateMachine {
  204. /// The possible states the state machine may be in.
  205. @usableFromInline
  206. internal enum State {
  207. case idle(ServerHandlerStateMachine.Idle)
  208. case handling(ServerHandlerStateMachine.Handling)
  209. case draining(ServerHandlerStateMachine.Draining)
  210. case finished(ServerHandlerStateMachine.Finished)
  211. }
  212. }
  213. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  214. extension ServerHandlerStateMachine {
  215. /// The next state to transition to and any output which may be produced as a
  216. /// result of a substate handling an action.
  217. @usableFromInline
  218. internal struct NextStateAndOutput<NextState, Output> {
  219. @usableFromInline
  220. internal var nextState: NextState
  221. @usableFromInline
  222. internal var output: Output
  223. @inlinable
  224. internal init(nextState: NextState, output: Output) {
  225. self.nextState = nextState
  226. self.output = output
  227. }
  228. }
  229. }
  230. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  231. extension ServerHandlerStateMachine.NextStateAndOutput where Output == Void {
  232. @inlinable
  233. internal init(nextState: NextState) {
  234. self.nextState = nextState
  235. self.output = ()
  236. }
  237. }
  238. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  239. extension ServerHandlerStateMachine.Idle {
  240. /// States which can be reached directly from 'Idle'.
  241. @usableFromInline
  242. internal struct NextState {
  243. @usableFromInline
  244. let state: ServerHandlerStateMachine.State
  245. @inlinable
  246. internal init(_state: ServerHandlerStateMachine.State) {
  247. self.state = _state
  248. }
  249. @inlinable
  250. internal static func idle(_ state: ServerHandlerStateMachine.Idle) -> Self {
  251. return Self(_state: .idle(state))
  252. }
  253. @inlinable
  254. internal static func handling(
  255. from: ServerHandlerStateMachine.Idle,
  256. requestHeaders: HPACKHeaders
  257. ) -> Self {
  258. return Self(_state: .handling(.init(from: from, requestHeaders: requestHeaders)))
  259. }
  260. @inlinable
  261. internal static func finished(from: ServerHandlerStateMachine.Idle) -> Self {
  262. return Self(_state: .finished(.init(from: from)))
  263. }
  264. }
  265. }
  266. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  267. extension ServerHandlerStateMachine.Handling {
  268. /// States which can be reached directly from 'Handling'.
  269. @usableFromInline
  270. internal struct NextState {
  271. @usableFromInline
  272. let state: ServerHandlerStateMachine.State
  273. @inlinable
  274. internal init(_state: ServerHandlerStateMachine.State) {
  275. self.state = _state
  276. }
  277. @inlinable
  278. internal static func handling(_ state: ServerHandlerStateMachine.Handling) -> Self {
  279. return Self(_state: .handling(state))
  280. }
  281. @inlinable
  282. internal static func draining(from: ServerHandlerStateMachine.Handling) -> Self {
  283. return Self(_state: .draining(.init(from: from)))
  284. }
  285. @inlinable
  286. internal static func finished(from: ServerHandlerStateMachine.Handling) -> Self {
  287. return Self(_state: .finished(.init(from: from)))
  288. }
  289. }
  290. }
  291. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  292. extension ServerHandlerStateMachine.Draining {
  293. /// States which can be reached directly from 'Draining'.
  294. @usableFromInline
  295. internal struct NextState {
  296. @usableFromInline
  297. let state: ServerHandlerStateMachine.State
  298. @inlinable
  299. internal init(_state: ServerHandlerStateMachine.State) {
  300. self.state = _state
  301. }
  302. @inlinable
  303. internal static func draining(_ state: ServerHandlerStateMachine.Draining) -> Self {
  304. return Self(_state: .draining(state))
  305. }
  306. @inlinable
  307. internal static func finished(from: ServerHandlerStateMachine.Draining) -> Self {
  308. return Self(_state: .finished(.init(from: from)))
  309. }
  310. }
  311. }
  312. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  313. extension ServerHandlerStateMachine.Finished {
  314. /// States which can be reached directly from 'Finished'.
  315. @usableFromInline
  316. internal struct NextState {
  317. @usableFromInline
  318. let state: ServerHandlerStateMachine.State
  319. @inlinable
  320. init(_state: ServerHandlerStateMachine.State) {
  321. self.state = _state
  322. }
  323. @inlinable
  324. internal static func finished(_ state: ServerHandlerStateMachine.Finished) -> Self {
  325. return Self(_state: .finished(state))
  326. }
  327. }
  328. }