@@ -120,18 +120,19 @@ public extension SSH {
120
120
}
121
121
122
122
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)
134
131
}
132
+ flowSource? . setCancelHandler {
133
+ self . flowSource = nil
134
+ }
135
+ flowSource? . resume ( )
135
136
}
136
137
137
138
/// Starts a keepalive mechanism for the SSH session.
@@ -142,29 +143,26 @@ public extension SSH {
142
143
///
143
144
/// - Parameter keepaliveInterval: The interval in seconds between keepalive messages. The default value is 5 seconds.
144
145
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)
152
153
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) )
157
155
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
167
164
}
165
+ keepAliveSource? . resume ( )
168
166
}
169
167
170
168
/// Cancels the keep-alive mechanism for the SSH session.
@@ -173,16 +171,14 @@ public extension SSH {
173
171
/// It should be called when the keep-alive mechanism is no longer needed or before
174
172
/// the session is terminated to clean up resources.
175
173
func cancelKeepalive( ) {
176
- call { [ self ] in
174
+ lock . withLock {
177
175
keepAliveSource? . cancel ( )
178
- keepAliveSource = nil
179
176
}
180
177
}
181
178
182
179
func cancelFlowSource( ) {
183
- call { [ self ] in
180
+ lock . withLock {
184
181
flowSource? . cancel ( )
185
- flowSource = nil
186
182
}
187
183
}
188
184
@@ -193,7 +189,7 @@ public extension SSH {
193
189
/// Use this function when you want to temporarily stop sending keep-alive
194
190
/// messages, for example, during a period of inactivity.
195
191
func suspendKeepalive( ) {
196
- call { [ self ] in
192
+ lock . withLock {
197
193
keepAliveSource? . suspend ( )
198
194
}
199
195
}
@@ -203,7 +199,7 @@ public extension SSH {
203
199
/// continues to operate, preventing the session from timing out
204
200
/// due to inactivity.
205
201
func resumeKeepalive( ) {
206
- call { [ self ] in
202
+ lock . withLock {
207
203
keepAliveSource? . resume ( )
208
204
}
209
205
}
0 commit comments