Coverage Report

Created: 2026-06-07 06:20

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