ConnectionManager+Delegates.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 closed.
  36. ///
  37. /// - Parameters:
  38. /// - connectionManager: The connection manager reporting the closed stream.
  39. func streamClosed(_ connectionManager: ConnectionManager)
  40. /// The connection received a SETTINGS frame containing SETTINGS_MAX_CONCURRENT_STREAMS.
  41. ///
  42. /// - Parameters:
  43. /// - connectionManager: The connection manager which received the settings update.
  44. /// - maxConcurrentStreams: The value of SETTINGS_MAX_CONCURRENT_STREAMS.
  45. func receivedSettingsMaxConcurrentStreams(
  46. _ connectionManager: ConnectionManager,
  47. maxConcurrentStreams: Int
  48. )
  49. }
  50. // This mirrors `ConnectivityState` (which is public API) but adds `Error` as associated data
  51. // to a few cases.
  52. internal enum _ConnectivityState: GRPCSendable {
  53. case idle(Error?)
  54. case connecting
  55. case ready
  56. case transientFailure(Error)
  57. case shutdown
  58. /// Returns whether this state is the same as the other state (ignoring any associated data).
  59. internal func isSameState(as other: _ConnectivityState) -> Bool {
  60. switch (self, other) {
  61. case (.idle, .idle),
  62. (.connecting, .connecting),
  63. (.ready, .ready),
  64. (.transientFailure, .transientFailure),
  65. (.shutdown, .shutdown):
  66. return true
  67. default:
  68. return false
  69. }
  70. }
  71. }
  72. extension ConnectivityState {
  73. internal init(_ state: _ConnectivityState) {
  74. switch state {
  75. case .idle:
  76. self = .idle
  77. case .connecting:
  78. self = .connecting
  79. case .ready:
  80. self = .ready
  81. case .transientFailure:
  82. self = .transientFailure
  83. case .shutdown:
  84. self = .shutdown
  85. }
  86. }
  87. }