StreamState.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. extension ServerInterceptorStateMachine {
  17. @usableFromInline
  18. internal enum StreamFilter: Hashable {
  19. case accept
  20. case reject
  21. }
  22. @usableFromInline
  23. internal enum InboundStreamState: Hashable {
  24. case idle
  25. case receivingMessages
  26. case done
  27. @inlinable
  28. mutating func receiveMetadata() -> StreamFilter {
  29. switch self {
  30. case .idle:
  31. self = .receivingMessages
  32. return .accept
  33. case .receivingMessages, .done:
  34. return .reject
  35. }
  36. }
  37. @inlinable
  38. func receiveMessage() -> StreamFilter {
  39. switch self {
  40. case .receivingMessages:
  41. return .accept
  42. case .idle, .done:
  43. return .reject
  44. }
  45. }
  46. @inlinable
  47. mutating func receiveEnd() -> StreamFilter {
  48. switch self {
  49. case .idle, .receivingMessages:
  50. self = .done
  51. return .accept
  52. case .done:
  53. return .reject
  54. }
  55. }
  56. }
  57. @usableFromInline
  58. internal enum OutboundStreamState: Hashable {
  59. case idle
  60. case writingMessages
  61. case done
  62. @inlinable
  63. mutating func sendMetadata() -> StreamFilter {
  64. switch self {
  65. case .idle:
  66. self = .writingMessages
  67. return .accept
  68. case .writingMessages, .done:
  69. return .reject
  70. }
  71. }
  72. @inlinable
  73. func sendMessage() -> StreamFilter {
  74. switch self {
  75. case .writingMessages:
  76. return .accept
  77. case .idle, .done:
  78. return .reject
  79. }
  80. }
  81. @inlinable
  82. mutating func sendEnd() -> StreamFilter {
  83. switch self {
  84. case .idle, .writingMessages:
  85. self = .done
  86. return .accept
  87. case .done:
  88. return .reject
  89. }
  90. }
  91. }
  92. }