Skip to content

Commit 4ab6585

Browse files
committed
Fix bug of keepAlive
1 parent fe85909 commit 4ab6585

File tree

5 files changed

+28
-41
lines changed

5 files changed

+28
-41
lines changed

Demo/MQTTDemo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"kind" : "remoteSourceControl",
66
"location" : "https://github.com/sutext/swift-promise",
77
"state" : {
8-
"revision" : "a40b8d605aa55c74ec1631daad7a59a5ce5fb075",
9-
"version" : "2.0.2"
8+
"revision" : "7c60757445266d3432eff3cff05d479e16f6bbc6",
9+
"version" : "2.1.0"
1010
}
1111
}
1212
],

Sources/MQTT/Client.swift

+3-5
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,8 @@ extension MQTTClient{
303303
pinging?.update()
304304
var buffer = DataBuffer()
305305
try packet.write(version: config.version, to: &buffer)
306-
return socket.send(data: buffer.data).then { _ in
307-
self.pinging?.update()
308-
}
306+
self.pinging?.update()
307+
return socket.send(data: buffer.data).then { _ in }
309308
} catch {
310309
return .init(error)
311310
}
@@ -336,9 +335,8 @@ extension MQTTClient{
336335
Logger.debug("SEND: \(packet)")
337336
var buffer = DataBuffer()
338337
try packet.write(version: config.version, to: &buffer)
339-
338+
self.pinging?.update()
340339
return socket.send(data: buffer.data).then { _ in
341-
self.pinging?.update()
342340
return task.start(in: self.queue,timeout:timeout)
343341
}
344342
} catch {

Sources/MQTT/MQTT.swift

-7
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ public final class Config:@unchecked Sendable{
7979
assert(pingTimeout>0, "pingTimeout must be greater than zero!")
8080
}
8181
}
82-
///The max times the connection consecutive ping timeouts
83-
/// It will take effect immediately
84-
public var maxPingCount:UInt8 = 1{
85-
didSet{
86-
assert(maxPingCount>0, "maxPingCount must be greater than zero!")
87-
}
88-
}
8982
/// timeout second for connecting to server
9083
/// - Important: This setting does not take effect in the quic protocol. In the quic protocol is fixed at 30s and cannot be modified
9184
/// - Note:Please set this value before client open, otherwise it will take effect on the next open

Sources/MQTT/Retrier.swift

+22-26
Original file line numberDiff line numberDiff line change
@@ -102,51 +102,47 @@ final class Monitor:@unchecked Sendable{
102102
final class Pinging:@unchecked Sendable{
103103
private let queue:DispatchQueue = .init(label: "mqtt.ping.queue")
104104
private let config:Config
105-
private var tryCount:UInt8 = 0
106-
private var lastTime:DispatchTime
107-
private var item:DispatchWorkItem?
105+
private var execTime:DispatchTime
106+
private var worker:DispatchWorkItem?
108107
private weak var client:MQTTClient?
109108
init(client:MQTTClient){
110-
lastTime = .now()
109+
execTime = .now()
111110
config = client.config
112111
self.client = client
113112
}
114113
func start(){
115114
guard config.pingEnabled else { return }
116-
guard item == nil else{ return }
117-
tryCount = 0
118-
lastTime = .now()
115+
guard worker == nil else{ return }
116+
execTime = .now()
119117
schedule()
120118
}
121119
func cancel(){
122-
if item != nil{
123-
item?.cancel()
124-
item = nil
120+
queue.async {
121+
if self.worker != nil{
122+
self.worker?.cancel()
123+
self.worker = nil
124+
}
125125
}
126126
}
127127
func update(){
128-
lastTime = .now()
128+
self.execTime = .now()
129129
}
130130
private func schedule(){
131-
let item = DispatchWorkItem{[weak self] in
131+
let worker = DispatchWorkItem{[weak self] in
132132
guard let self else{ return }
133133
guard let client = self.client else{ return }
134-
if self.lastTime+TimeInterval(self.config.keepAlive) <= .now(){
135-
client.ping().finally{result in
136-
if case .failure = result{
137-
self.tryCount += 1
138-
if self.tryCount >= self.config.maxPingCount{
139-
client.pingTimeout()
140-
}
141-
}
142-
self.update()
143-
self.schedule()
144-
}
145-
}else{
134+
guard self.execTime+TimeInterval(self.config.keepAlive) <= .now() else{
146135
self.schedule()
136+
return
137+
}
138+
client.ping().finally{result in
139+
if case .failure = result{
140+
client.pingTimeout()
141+
}
147142
}
143+
self.schedule()
148144
}
149-
queue.asyncAfter(deadline: lastTime + TimeInterval(config.keepAlive), execute: item)
150-
self.item = item
145+
self.queue.asyncAfter(deadline: execTime + TimeInterval(config.keepAlive), execute: worker)
146+
self.worker = worker
151147
}
152148
}

Sources/MQTT/Socket.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Connection.swift
2+
// Socket.swift
33
// swift-mqtt
44
//
55
// Created by supertext on 2025/3/7.

0 commit comments

Comments
 (0)