Skip to content

Commit e330802

Browse files
committed
dev
1 parent fbd5bef commit e330802

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

Sources/SSH/Session.swift

+33-37
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,19 @@ public extension SSH {
120120
}
121121

122122
func keepaliveFlow(_ keepaliveInterval: Int = 1) {
123-
call { [self] in
124-
cancelFlowSource()
125-
flowSource = DispatchSource.makeTimerSource(queue: queueKeep)
126-
flowSource?.schedule(deadline: DispatchTime.now() + .seconds(keepaliveInterval), repeating: .seconds(keepaliveInterval), leeway: .seconds(keepaliveInterval))
127-
128-
flowSource?.setEventHandler { [self] in
129-
self.sessionDelegate?.send(ssh: self, size: send)
130-
self.sessionDelegate?.recv(ssh: self, size: recv)
131-
}
132-
flowSource?.setCancelHandler {}
133-
flowSource?.resume()
123+
cancelFlowSource()
124+
flowSource = nil
125+
flowSource = DispatchSource.makeTimerSource(queue: queueKeep)
126+
flowSource?.schedule(deadline: DispatchTime.now() + .seconds(keepaliveInterval), repeating: .seconds(keepaliveInterval), leeway: .seconds(keepaliveInterval))
127+
128+
flowSource?.setEventHandler { [self] in
129+
self.sessionDelegate?.send(ssh: self, size: send)
130+
self.sessionDelegate?.recv(ssh: self, size: recv)
134131
}
132+
flowSource?.setCancelHandler {
133+
self.flowSource = nil
134+
}
135+
flowSource?.resume()
135136
}
136137

137138
/// Starts a keepalive mechanism for the SSH session.
@@ -142,29 +143,26 @@ public extension SSH {
142143
///
143144
/// - Parameter keepaliveInterval: The interval in seconds between keepalive messages. The default value is 5 seconds.
144145
func keepalive(_ keepaliveInterval: Int = 5) {
145-
call { [self] in
146-
guard let rawSession, isAuthenticated else {
147-
return
148-
}
149-
libssh2_keepalive_config(rawSession, 1, keepaliveInterval.load())
150-
cancelKeepalive()
151-
keepAliveSource = DispatchSource.makeTimerSource(queue: queueKeep)
146+
guard let rawSession, isAuthenticated else {
147+
return
148+
}
149+
libssh2_keepalive_config(rawSession, 1, keepaliveInterval.load())
150+
cancelKeepalive()
151+
self.keepAliveSource = nil
152+
keepAliveSource = DispatchSource.makeTimerSource(queue: queueKeep)
152153

153-
guard let keepAliveSource else {
154-
return
155-
}
156-
keepAliveSource.schedule(deadline: DispatchTime.now() + .seconds(keepaliveInterval), repeating: .seconds(keepaliveInterval), leeway: .seconds(keepaliveInterval))
154+
keepAliveSource?.schedule(deadline: DispatchTime.now() + .seconds(keepaliveInterval), repeating: .seconds(keepaliveInterval), leeway: .seconds(keepaliveInterval))
157155

158-
keepAliveSource.setEventHandler { [self] in
159-
sendKeepalive()
160-
}
161-
keepAliveSource.setCancelHandler {
162-
#if DEBUG
163-
print("心跳取消")
164-
#endif
165-
}
166-
keepAliveSource.resume()
156+
keepAliveSource?.setEventHandler { [self] in
157+
sendKeepalive()
158+
}
159+
keepAliveSource?.setCancelHandler {
160+
#if DEBUG
161+
print("心跳取消")
162+
#endif
163+
self.keepAliveSource = nil
167164
}
165+
keepAliveSource?.resume()
168166
}
169167

170168
/// Cancels the keep-alive mechanism for the SSH session.
@@ -173,16 +171,14 @@ public extension SSH {
173171
/// It should be called when the keep-alive mechanism is no longer needed or before
174172
/// the session is terminated to clean up resources.
175173
func cancelKeepalive() {
176-
call { [self] in
174+
lock.withLock {
177175
keepAliveSource?.cancel()
178-
keepAliveSource = nil
179176
}
180177
}
181178

182179
func cancelFlowSource() {
183-
call { [self] in
180+
lock.withLock {
184181
flowSource?.cancel()
185-
flowSource = nil
186182
}
187183
}
188184

@@ -193,7 +189,7 @@ public extension SSH {
193189
/// Use this function when you want to temporarily stop sending keep-alive
194190
/// messages, for example, during a period of inactivity.
195191
func suspendKeepalive() {
196-
call { [self] in
192+
lock.withLock {
197193
keepAliveSource?.suspend()
198194
}
199195
}
@@ -203,7 +199,7 @@ public extension SSH {
203199
/// continues to operate, preventing the session from timing out
204200
/// due to inactivity.
205201
func resumeKeepalive() {
206-
call { [self] in
202+
lock.withLock {
207203
keepAliveSource?.resume()
208204
}
209205
}

Sources/SSH/Shell.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public extension SSH {
119119
}
120120
socketShell?.setCancelHandler {
121121
self.channelDelegate?.connect(ssh: self, online: false)
122+
self.socketShell = nil
122123
}
123124
socketShell?.resume()
124125
}
@@ -179,7 +180,6 @@ public extension SSH {
179180
func closeShell() {
180181
lock.withLock {
181182
socketShell?.cancel()
182-
socketShell = nil
183183
closed(channel: rawChannel)
184184
rawChannel = nil
185185
}

0 commit comments

Comments
 (0)