Skip to content

Commit b31130a

Browse files
authored
update p2p reconnect & test (#214)
* update reconnect test * update reconnect cases * update reconnect * update peer test * update peer test * update peer test * update peer * update connection * update reconnect * update p2p reconnect & test * update peer * update peer test * update test
1 parent 875a7f7 commit b31130a

File tree

3 files changed

+309
-83
lines changed

3 files changed

+309
-83
lines changed

Networking/Sources/Networking/Connection.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ enum ConnectionError: Error {
2020
case invalidLength
2121
case unexpectedState
2222
case closed
23+
case reconnect
2324
}
2425

2526
enum ConnectionState {
2627
case connecting(continuations: [CheckedContinuation<Void, Error>])
2728
case connected(publicKey: Data)
2829
case closed
30+
case reconnect(publicKey: Data)
2931
}
3032

3133
public final class Connection<Handler: StreamHandler>: Sendable, ConnectionInfoProtocol {
@@ -50,6 +52,8 @@ public final class Connection<Handler: StreamHandler>: Sendable, ConnectionInfoP
5052
publicKey
5153
case .closed:
5254
nil
55+
case let .reconnect(publicKey):
56+
publicKey
5357
}
5458
}
5559
}
@@ -91,6 +95,18 @@ public final class Connection<Handler: StreamHandler>: Sendable, ConnectionInfoP
9195
}
9296
}
9397

98+
func reconnect(publicKey: Data) {
99+
state.write { state in
100+
if case let .connecting(continuations) = state {
101+
for continuation in continuations {
102+
continuation.resume(throwing: ConnectionError.reconnect)
103+
}
104+
state = .reconnect(publicKey: publicKey)
105+
}
106+
state = .reconnect(publicKey: publicKey)
107+
}
108+
}
109+
94110
public var isClosed: Bool {
95111
state.read {
96112
switch $0 {
@@ -100,7 +116,18 @@ public final class Connection<Handler: StreamHandler>: Sendable, ConnectionInfoP
100116
false
101117
case .closed:
102118
true
119+
case .reconnect:
120+
false
121+
}
122+
}
123+
}
124+
125+
public var needReconnect: Bool {
126+
state.read {
127+
if case .reconnect = $0 {
128+
return true
103129
}
130+
return false
104131
}
105132
}
106133

@@ -113,6 +140,8 @@ public final class Connection<Handler: StreamHandler>: Sendable, ConnectionInfoP
113140
true
114141
case .closed:
115142
true
143+
case .reconnect:
144+
true
116145
}
117146
}
118147

0 commit comments

Comments
 (0)