StreamState.swift 2.2 KB

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