Skip to content

Commit ede40a2

Browse files
committed
Merge branch 'PacketTunnel-State-refactor-2'
2 parents 8242e28 + 36a2a27 commit ede40a2

5 files changed

+37
-35
lines changed

ios/PacketTunnelCore/Actor/PacketTunnelActor+ErrorState.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ extension PacketTunnelActor {
9595
private func mapConnectionState(
9696
_ connState: State.ConnectionData,
9797
reason: BlockedStateReason,
98-
priorState: StatePriorToBlockedState
98+
priorState: State.BlockingData.PriorState
9999
) -> State.BlockingData {
100100
State.BlockingData(
101101
reason: reason,

ios/PacketTunnelCore/Actor/PacketTunnelActor+KeyPolicy.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ extension PacketTunnelActor {
9090
- Parameter keyPolicy: a reference to key policy held either in connection state or blocked state struct.
9191
- Returns: `true` when the policy was modified, otherwise `false`.
9292
*/
93-
private func setCurrentKeyPolicy(_ keyPolicy: inout KeyPolicy) {
93+
private func setCurrentKeyPolicy(_ keyPolicy: inout State.KeyPolicy) {
9494
if case .usePrior = keyPolicy {
9595
keyPolicy = .useCurrent
9696
}

ios/PacketTunnelCore/Actor/PacketTunnelActor.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ public actor PacketTunnelActor {
124124
// MARK: -
125125

126126
extension PacketTunnelActor {
127+
/// Describes the reason for reconnection request.
128+
enum ReconnectReason {
129+
/// Initiated by user.
130+
case userInitiated
131+
132+
/// Initiated by tunnel monitor due to loss of connectivity.
133+
/// Actor will increment the connection attempt counter before picking next relay.
134+
case connectionLoss
135+
}
136+
127137
/**
128138
Start the tunnel.
129139

@@ -295,7 +305,7 @@ extension PacketTunnelActor {
295305
settings: Settings,
296306
reason: ReconnectReason
297307
) throws -> State.ConnectionData? {
298-
var keyPolicy: KeyPolicy = .useCurrent
308+
var keyPolicy: State.KeyPolicy = .useCurrent
299309
var networkReachability = defaultPathObserver.defaultPath?.networkReachability ?? .undetermined
300310
var lastKeyRotation: Date?
301311

ios/PacketTunnelCore/Actor/State+Extensions.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import MullvadTypes
1111
import WireGuardKitTypes
1212

1313
extension State {
14+
/// Target state the actor should transition into upon request to either start (connect) or reconnect.
15+
enum TargetStateForReconnect {
16+
case reconnecting, connecting
17+
}
18+
1419
/// Returns the target state to which the actor state should transition when requested to reconnect.
1520
/// It returns `nil` when reconnection is not supported such as when already `.disconnecting` or `.disconnected` states.
1621
var targetStateForReconnect: TargetStateForReconnect? {
@@ -149,7 +154,7 @@ extension State {
149154
}
150155
}
151156

152-
extension KeyPolicy {
157+
extension State.KeyPolicy {
153158
func logFormat() -> String {
154159
switch self {
155160
case .useCurrent:
@@ -160,8 +165,8 @@ extension KeyPolicy {
160165
}
161166
}
162167

163-
extension KeyPolicy: Equatable {
164-
static func == (lhs: KeyPolicy, rhs: KeyPolicy) -> Bool {
168+
extension State.KeyPolicy: Equatable {
169+
static func == (lhs: State.KeyPolicy, rhs: State.KeyPolicy) -> Bool {
165170
switch (lhs, rhs) {
166171
case (.useCurrent, .useCurrent): true
167172
case let (.usePrior(priorA, _), .usePrior(priorB, _)): priorA == priorB

ios/PacketTunnelCore/Actor/State.swift

+16-29
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,28 @@ enum State: Equatable {
8282
case error(BlockingData)
8383
}
8484

85-
/// Policy describing what WG key to use for tunnel communication.
86-
enum KeyPolicy {
87-
/// Use current key stored in device data.
88-
case useCurrent
89-
90-
/// Use prior key until timer fires.
91-
case usePrior(_ priorKey: PrivateKey, _ timerTask: AutoCancellingTask)
92-
}
93-
9485
/// Enum describing network availability.
9586
public enum NetworkReachability: Equatable, Codable {
9687
case undetermined, reachable, unreachable
9788
}
9889

9990
protocol StateAssociatedData {
10091
var currentKey: PrivateKey? { get set }
101-
var keyPolicy: KeyPolicy { get set }
92+
var keyPolicy: State.KeyPolicy { get set }
10293
var networkReachability: NetworkReachability { get set }
10394
var lastKeyRotation: Date? { get set }
10495
}
10596

10697
extension State {
98+
/// Policy describing what WG key to use for tunnel communication.
99+
enum KeyPolicy {
100+
/// Use current key stored in device data.
101+
case useCurrent
102+
103+
/// Use prior key until timer fires.
104+
case usePrior(_ priorKey: PrivateKey, _ timerTask: AutoCancellingTask)
105+
}
106+
107107
/// Data associated with states that hold connection data.
108108
struct ConnectionData: Equatable, StateAssociatedData {
109109
/// Current selected relay.
@@ -173,7 +173,7 @@ extension State {
173173
public var recoveryTask: AutoCancellingTask?
174174

175175
/// Prior state of the actor before entering blocked state
176-
public var priorState: StatePriorToBlockedState
176+
public var priorState: PriorState
177177
}
178178
}
179179

@@ -214,14 +214,11 @@ public enum BlockedStateReason: String, Codable, Equatable {
214214
case unknown
215215
}
216216

217-
/// Legal states that can precede error state.
218-
enum StatePriorToBlockedState: Equatable {
219-
case initial, connecting, connected, reconnecting
220-
}
221-
222-
/// Target state the actor should transition into upon request to either start (connect) or reconnect.
223-
enum TargetStateForReconnect {
224-
case reconnecting, connecting
217+
extension State.BlockingData {
218+
/// Legal states that can precede error state.
219+
enum PriorState: Equatable {
220+
case initial, connecting, connected, reconnecting
221+
}
225222
}
226223

227224
/// Describes which relay the tunnel should connect to next.
@@ -235,13 +232,3 @@ public enum NextRelay: Equatable, Codable {
235232
/// Use pre-selected relay.
236233
case preSelected(SelectedRelay)
237234
}
238-
239-
/// Describes the reason for reconnection request.
240-
enum ReconnectReason {
241-
/// Initiated by user.
242-
case userInitiated
243-
244-
/// Initiated by tunnel monitor due to loss of connectivity.
245-
/// Actor will increment the connection attempt counter before picking next relay.
246-
case connectionLoss
247-
}

0 commit comments

Comments
 (0)