ConnectionManager+Delegates.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2021, 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. internal protocol ConnectionManagerConnectivityDelegate {
  17. /// The state of the connection changed.
  18. ///
  19. /// - Parameters:
  20. /// - connectionManager: The connection manager reporting the change of state.
  21. /// - oldState: The previous `ConnectivityState`.
  22. /// - newState: The current `ConnectivityState`.
  23. func connectionStateDidChange(
  24. _ connectionManager: ConnectionManager,
  25. from oldState: _ConnectivityState,
  26. to newState: _ConnectivityState
  27. )
  28. /// The connection is quiescing.
  29. ///
  30. /// - Parameters:
  31. /// - connectionManager: The connection manager whose connection is quiescing.
  32. func connectionIsQuiescing(_ connectionManager: ConnectionManager)
  33. }
  34. internal protocol ConnectionManagerHTTP2Delegate {
  35. /// An HTTP/2 stream was opened.
  36. ///
  37. /// - Parameters:
  38. /// - connectionManager: The connection manager reporting the opened stream.
  39. func streamOpened(_ connectionManager: ConnectionManager)
  40. /// An HTTP/2 stream was closed.
  41. ///
  42. /// - Parameters:
  43. /// - connectionManager: The connection manager reporting the closed stream.
  44. func streamClosed(_ connectionManager: ConnectionManager)
  45. /// The connection received a SETTINGS frame containing SETTINGS_MAX_CONCURRENT_STREAMS.
  46. ///
  47. /// - Parameters:
  48. /// - connectionManager: The connection manager which received the settings update.
  49. /// - maxConcurrentStreams: The value of SETTINGS_MAX_CONCURRENT_STREAMS.
  50. func receivedSettingsMaxConcurrentStreams(
  51. _ connectionManager: ConnectionManager,
  52. maxConcurrentStreams: Int
  53. )
  54. }
  55. // This mirrors `ConnectivityState` (which is public API) but adds `Error` as associated data
  56. // to a few cases.
  57. internal enum _ConnectivityState: Sendable {
  58. case idle(Error?)
  59. case connecting
  60. case ready
  61. case transientFailure(Error)
  62. case shutdown
  63. /// Returns whether this state is the same as the other state (ignoring any associated data).
  64. internal func isSameState(as other: _ConnectivityState) -> Bool {
  65. switch (self, other) {
  66. case (.idle, .idle),
  67. (.connecting, .connecting),
  68. (.ready, .ready),
  69. (.transientFailure, .transientFailure),
  70. (.shutdown, .shutdown):
  71. return true
  72. default:
  73. return false
  74. }
  75. }
  76. }
  77. extension ConnectivityState {
  78. internal init(_ state: _ConnectivityState) {
  79. switch state {
  80. case .idle:
  81. self = .idle
  82. case .connecting:
  83. self = .connecting
  84. case .ready:
  85. self = .ready
  86. case .transientFailure:
  87. self = .transientFailure
  88. case .shutdown:
  89. self = .shutdown
  90. }
  91. }
  92. }