Skip to content

Commit

Permalink
Handle courier event message send success event not being triggered f…
Browse files Browse the repository at this point in the history
…or QoS 3/4 (#31)

* Handle courier event message send success event not being triggered for qos 3 and 4
  • Loading branch information
alfianlosari authored Feb 20, 2024
1 parent eb800a9 commit e367fb2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CourierE2EApp/View Models/ConnectionObservableObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ final class ConnectionObservableObject: ObservableObject {
extension ConnectionObservableObject: ICourierEventHandler {

func onEvent(_ event: CourierEvent) {
// print("EVENT: \(event.type)")
print("EVENT: \(event.type)")
switch event.type {
case .connectionSuccess:
if connectionServiceProvider.isCleanSession {
Expand Down
2 changes: 1 addition & 1 deletion CourierE2EApp/Views/ConnectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct ConnectionView: View {
HStack {
Text("QoS")
Picker(selection: $connectionVM.publishQoS) {
ForEach(0..<3) { index in
ForEach(0..<5) { index in
Text(String(index)).tag(index)
}
} label: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,27 @@ class MQTTClientFrameworkSessionManager: NSObject, IMQTTClientFrameworkSessionMa

@discardableResult
private func sendData(_ data: Data, topic: String, qos: MQTTQosLevel, retainFlag: Bool) -> UInt16 {
let msgId = session?.publishData(data, onTopic: topic, retain: retainFlag, qos: qos, publishHandler: nil) ?? 0

var midProvider: (() -> UInt16)?
var publishHandler: MQTTPublishHandler?
if qos == MQTTQosLevel.atLeastOnceWithoutPersistenceAndNoRetry || qos == MQTTQosLevel.atLeastOnceWithoutPersistenceAndNoRetry {
publishHandler = { [weak self] error in
guard let self = self else { return }
printDebug("COURIER: Puback Handler for Special QoSes")
if error == nil {
let mid = midProvider?() ?? 0
self.delegate?.sessionManager(self, didDeliverMessageID: mid, topic: topic, data: data, qos: qos, retainFlag: retainFlag)
}
}
}

let msgId = session?.publishData(data, onTopic: topic, retain: retainFlag, qos: qos, publishHandler: publishHandler) ?? 0
if publishHandler != nil {
midProvider = { [weak self] in
guard self != nil else { return 0 }
return msgId
}
}
return msgId
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ - (UInt16)publishData:(NSData *)data
if (qos == MQTTQosLevelAtLeastOnceWithoutPersistenceAndRetry || qos == MQTTQosLevelAtLeastOnceWithoutPersistenceAndNoRetry) {
qos = MQTTQosLevelAtLeastOnce;
msgId = [self nextMsgId];
if (publishHandler) {
(self.publishHandlers)[@(msgId)] = [publishHandler copy];
} else {
[self.publishHandlers removeObjectForKey:@(msgId)];
}
}
MQTTMessage *msg = [MQTTMessage publishMessageWithData:data
onTopic:topic
Expand All @@ -422,7 +427,7 @@ - (UInt16)publishData:(NSData *)data
code:MQTTSessionErrorEncoderNotReady
userInfo:@{NSLocalizedDescriptionKey : @"Encoder not ready"}];
}
if (publishHandler) {
if (!qos && publishHandler) {
[self onPublish:publishHandler error:error];
}
} else {
Expand Down Expand Up @@ -1161,6 +1166,12 @@ - (void)handlePuback:(MQTTMessage*)msg {
[self.persistence sync];
[self tell];
}
} else {
MQTTPublishHandler publishHandler = (self.publishHandlers)[@(msg.mid)];
if (publishHandler) {
[self.publishHandlers removeObjectForKey:@(msg.mid)];
[self onPublish:publishHandler error:nil];
}
}
}

Expand Down

0 comments on commit e367fb2

Please sign in to comment.