StreamState.swift 2.3 KB

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