Skip to content

Commit

Permalink
add new mqtt5 will message class and fix websocket reconnect bug
Browse files Browse the repository at this point in the history
  • Loading branch information
leeway1208 committed Nov 12, 2021
1 parent 4101217 commit 8f06f07
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 105 deletions.
8 changes: 8 additions & 0 deletions CocoaMQTT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
92A66A3127058123005E5107 /* CocoaMQTT5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92A66A3027058123005E5107 /* CocoaMQTT5.swift */; };
92A66A3227058123005E5107 /* CocoaMQTT5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92A66A3027058123005E5107 /* CocoaMQTT5.swift */; };
92A66A3327058123005E5107 /* CocoaMQTT5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92A66A3027058123005E5107 /* CocoaMQTT5.swift */; };
92C6757D273BF45200A3A4A3 /* CocoaMQTT5Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92C6757C273BF45200A3A4A3 /* CocoaMQTT5Message.swift */; };
92C6757E273BF45200A3A4A3 /* CocoaMQTT5Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92C6757C273BF45200A3A4A3 /* CocoaMQTT5Message.swift */; };
92C6757F273BF45200A3A4A3 /* CocoaMQTT5Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92C6757C273BF45200A3A4A3 /* CocoaMQTT5Message.swift */; };
92EF632026F5CE2000AB9C6D /* MqttPublishProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92EF631426F5CE1F00AB9C6D /* MqttPublishProperties.swift */; };
92EF632126F5CE2000AB9C6D /* MqttSubscription.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92EF631526F5CE1F00AB9C6D /* MqttSubscription.swift */; };
92EF632226F5CE2000AB9C6D /* MqttDecodePubAck.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92EF631626F5CE1F00AB9C6D /* MqttDecodePubAck.swift */; };
Expand Down Expand Up @@ -252,6 +255,7 @@
8D43DE6522FAFF2500D9A06B /* FrameDisconnect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FrameDisconnect.swift; sourceTree = "<group>"; };
8D7A262D23AF2FEA00CE7442 /* client-keycert.p12 */ = {isa = PBXFileReference; lastKnownFileType = file; path = "client-keycert.p12"; sourceTree = "<group>"; };
92A66A3027058123005E5107 /* CocoaMQTT5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CocoaMQTT5.swift; sourceTree = "<group>"; };
92C6757C273BF45200A3A4A3 /* CocoaMQTT5Message.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CocoaMQTT5Message.swift; sourceTree = "<group>"; };
92EF631426F5CE1F00AB9C6D /* MqttPublishProperties.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MqttPublishProperties.swift; sourceTree = "<group>"; };
92EF631526F5CE1F00AB9C6D /* MqttSubscription.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MqttSubscription.swift; sourceTree = "<group>"; };
92EF631626F5CE1F00AB9C6D /* MqttDecodePubAck.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MqttDecodePubAck.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -358,6 +362,7 @@
0409977A1C1B1529006B5A6D /* CocoaMQTT.swift */,
92A66A3027058123005E5107 /* CocoaMQTT5.swift */,
0409977C1C1B1529006B5A6D /* CocoaMQTTMessage.swift */,
92C6757C273BF45200A3A4A3 /* CocoaMQTT5Message.swift */,
8225B4D0227B2F2900E4DB51 /* CocoaMQTTDeliver.swift */,
82CF1FAF22943B2100DF539A /* CocoaMQTTReader.swift */,
040997791C1B14F0006B5A6D /* Supporting Files */,
Expand Down Expand Up @@ -780,6 +785,7 @@
1111249D24BB515E00E2DFBC /* FrameSubAck.swift in Sources */,
1111249E24BB515E00E2DFBC /* FrameDisconnect.swift in Sources */,
1111249F24BB515E00E2DFBC /* FramePubAck.swift in Sources */,
92C6757E273BF45200A3A4A3 /* CocoaMQTT5Message.swift in Sources */,
111124A024BB515E00E2DFBC /* FramePingReq.swift in Sources */,
111124A124BB515E00E2DFBC /* FramePubRel.swift in Sources */,
111124A224BB515E00E2DFBC /* FrameSubscribe.swift in Sources */,
Expand Down Expand Up @@ -828,6 +834,7 @@
111124C724BB516300E2DFBC /* FrameSubAck.swift in Sources */,
111124C824BB516300E2DFBC /* FrameDisconnect.swift in Sources */,
111124C924BB516300E2DFBC /* FramePubAck.swift in Sources */,
92C6757F273BF45200A3A4A3 /* CocoaMQTT5Message.swift in Sources */,
111124CA24BB516300E2DFBC /* FramePingReq.swift in Sources */,
111124CB24BB516300E2DFBC /* FramePubRel.swift in Sources */,
111124CC24BB516300E2DFBC /* FrameSubscribe.swift in Sources */,
Expand Down Expand Up @@ -876,6 +883,7 @@
8D43DE6222FAFDA700D9A06B /* FramePingReq.swift in Sources */,
8D43DE5822FAEDE800D9A06B /* FramePubRel.swift in Sources */,
8D43DE5A22FAF37500D9A06B /* FrameSubscribe.swift in Sources */,
92C6757D273BF45200A3A4A3 /* CocoaMQTT5Message.swift in Sources */,
8D43DE6422FAFE5F00D9A06B /* FramePingResp.swift in Sources */,
92EF632126F5CE2000AB9C6D /* MqttSubscription.swift in Sources */,
92EF632826F5CE2000AB9C6D /* MqttDecodePublish.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TVOS Target: 10.0 or above
Install using [CocoaPods](http://cocoapods.org) by adding this line to your Podfile:

```ruby
pod 'CocoaMQTT5, '~> 1.0.4''
pod 'CocoaMQTT5, '~> 1.0.5''
```

Then, run the following command:
Expand Down
24 changes: 12 additions & 12 deletions Source/CocoaMQTT5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import CocoaAsyncSocket
func mqtt5(_ mqtt5: CocoaMQTT5, didConnectAck ack: CocoaMQTTCONNACKReasonCode, connAckData: MqttDecodeConnAck)

///
func mqtt5(_ mqtt5: CocoaMQTT5, didPublishMessage message: CocoaMQTTMessage, id: UInt16)
func mqtt5(_ mqtt5: CocoaMQTT5, didPublishMessage message: CocoaMQTT5Message, id: UInt16)

///
func mqtt5(_ mqtt5: CocoaMQTT5, didPublishAck id: UInt16, pubAckData: MqttDecodePubAck)
Expand All @@ -43,7 +43,7 @@ import CocoaAsyncSocket
func mqtt5(_ mqtt5: CocoaMQTT5, didPublishRec id: UInt16, pubRecData: MqttDecodePubRec)

///
func mqtt5(_ mqtt5: CocoaMQTT5, didReceiveMessage message: CocoaMQTTMessage, id: UInt16, publishData: MqttDecodePublish)
func mqtt5(_ mqtt5: CocoaMQTT5, didReceiveMessage message: CocoaMQTT5Message, id: UInt16, publishData: MqttDecodePublish)

///
func mqtt5(_ mqtt5: CocoaMQTT5, didSubscribeTopics success: NSDictionary, failed: [String], subAckData: MqttDecodeSubAck)
Expand Down Expand Up @@ -92,7 +92,7 @@ protocol CocoaMQTT5Client {
var password: String? {get set}
var cleanSession: Bool {get set}
var keepAlive: UInt16 {get set}
var willMessage: CocoaMQTTMessage? {get set}
var willMessage: CocoaMQTT5Message? {get set}
var connectProperties: MqttConnectProperties? {get set}
var authProperties: MqttAuthProperties? {get set}

Expand All @@ -116,7 +116,7 @@ protocol CocoaMQTT5Client {
func unsubscribe(_ topics: [MqttSubscription])

func publish(_ topic: String, withString string: String, qos: CocoaMQTTQoS, DUP: Bool, retained: Bool, properties: MqttPublishProperties) -> Int
func publish(_ message: CocoaMQTTMessage, DUP: Bool, retained: Bool, properties: MqttPublishProperties) -> Int
func publish(_ message: CocoaMQTT5Message, DUP: Bool, retained: Bool, properties: MqttPublishProperties) -> Int

/* PUBLISH/SUBSCRIBE */
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public class CocoaMQTT5: NSObject, CocoaMQTT5Client {
public var cleanSession = true

/// Setup a **Last Will Message** to client before connecting to broker
public var willMessage: CocoaMQTTMessage?
public var willMessage: CocoaMQTT5Message?

/// Enable backgounding socket if running on iOS platform. Default is true
///
Expand Down Expand Up @@ -264,7 +264,7 @@ public class CocoaMQTT5: NSObject, CocoaMQTT5Client {


/// Sending messages
fileprivate var sendingMessages: [UInt16: CocoaMQTTMessage] = [:]
fileprivate var sendingMessages: [UInt16: CocoaMQTT5Message] = [:]

/// message id counter
private var _msgid: UInt16 = 0
Expand All @@ -273,10 +273,10 @@ public class CocoaMQTT5: NSObject, CocoaMQTT5Client {

// Closures
public var didConnectAck: (CocoaMQTT5, CocoaMQTTCONNACKReasonCode, MqttDecodeConnAck) -> Void = { _, _, _ in }
public var didPublishMessage: (CocoaMQTT5, CocoaMQTTMessage, UInt16) -> Void = { _, _, _ in }
public var didPublishMessage: (CocoaMQTT5, CocoaMQTT5Message, UInt16) -> Void = { _, _, _ in }
public var didPublishAck: (CocoaMQTT5, UInt16, MqttDecodePubAck) -> Void = { _, _, _ in }
public var didPublishRec: (CocoaMQTT5, UInt16, MqttDecodePubRec) -> Void = { _, _, _ in }
public var didReceiveMessage: (CocoaMQTT5, CocoaMQTTMessage, UInt16, MqttDecodePublish) -> Void = { _, _, _, _ in }
public var didReceiveMessage: (CocoaMQTT5, CocoaMQTT5Message, UInt16, MqttDecodePublish) -> Void = { _, _, _, _ in }
public var didSubscribeTopics: (CocoaMQTT5, NSDictionary, [String], MqttDecodeSubAck) -> Void = { _, _, _, _ in }
public var didUnsubscribeTopics: (CocoaMQTT5, [String], MqttDecodeUnsubAck) -> Void = { _, _, _ in }
public var didPing: (CocoaMQTT5) -> Void = { _ in }
Expand Down Expand Up @@ -330,7 +330,7 @@ public class CocoaMQTT5: NSObject, CocoaMQTT5Client {
connect.keepAlive = keepAlive
connect.username = username
connect.password = password
connect.willMsg = willMessage
connect.willMsg5 = willMessage
connect.cleansess = cleanSession

connect.connectProperties = connectProperties
Expand Down Expand Up @@ -450,7 +450,7 @@ public class CocoaMQTT5: NSObject, CocoaMQTT5Client {
@discardableResult
public func publish(_ topic: String, withString string: String, qos: CocoaMQTTQoS = .qos1, DUP: Bool = false
, retained: Bool = false, properties: MqttPublishProperties) -> Int {
let message = CocoaMQTTMessage(topic: topic, string: string, qos: qos, retained: retained)
let message = CocoaMQTT5Message(topic: topic, string: string, qos: qos, retained: retained)
return publish(message, DUP: DUP, retained: retained, properties: properties)
}

Expand All @@ -459,7 +459,7 @@ public class CocoaMQTT5: NSObject, CocoaMQTT5Client {
/// - Parameters:
/// - message: Message
@discardableResult
public func publish(_ message: CocoaMQTTMessage, DUP: Bool = false, retained: Bool = false, properties: MqttPublishProperties) -> Int {
public func publish(_ message: CocoaMQTT5Message, DUP: Bool = false, retained: Bool = false, properties: MqttPublishProperties) -> Int {
let msgid: UInt16

if message.qos == .qos0 {
Expand Down Expand Up @@ -725,7 +725,7 @@ extension CocoaMQTT5: CocoaMQTTReaderDelegate {
func didReceive(_ reader: CocoaMQTTReader, publish: FramePublish) {
printDebug("RECV: \(publish)")

let message = CocoaMQTTMessage(topic: publish.mqtt5Topic, payload: publish.payload5(), qos: publish.qos, retained: publish.retained)
let message = CocoaMQTT5Message(topic: publish.mqtt5Topic, payload: publish.payload5(), qos: publish.qos, retained: publish.retained)

message.duplicated = publish.dup

Expand Down
147 changes: 147 additions & 0 deletions Source/CocoaMQTT5Message.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
//
// CocoaMQTT5Message.swift
// CocoaMQTT
//
// Created by Created by liwei wang on 2021/11/10.
// Copyright (c) 2015 emqx.io. All rights reserved.
//

import Foundation


/// MQTT Message
public class CocoaMQTT5Message: NSObject {

public var qos = CocoaMQTTQoS.qos1

public var topic: String

public var payload: [UInt8]

public var retained = false

/// The `duplicated` property show that this message maybe has be received before
///
/// - note: Readonly property
public var duplicated = false


///3.1.3.2.3 Payload Format Indicator
public var isUTF8EncodedData: Bool = true
///3.1.3.2.2 Will Delay Interval
public var willDelayInterval: UInt32? = 0
///3.1.3.2.4 Message Expiry Interval
public var willExpiryInterval: UInt32? = 0
///3.1.3.2.5 Content Type
public var contentType: String?
///3.1.3.2.6 Response Topic
public var willResponseTopic: String?
///3.1.3.2.7 Correlation Data
public var willCorrelationData: [UInt8]?
///3.1.3.2.8 User Property
public var willUserProperty: [String: String]?

/// Return the payload as a utf8 string if possible
///
/// It will return nil if the payload is not a valid utf8 string
public var string: String? {

get {
return NSString(bytes: payload, length: payload.count, encoding: String.Encoding.utf8.rawValue) as String?
}
}

public var properties: [UInt8] {

var properties = [UInt8]()
var retVal = [UInt8]()

/// 3.1.3.2.2 Property Length
if let willDelayInterval = self.willDelayInterval {
properties += getMQTTPropertyData(type: CocoaMQTTPropertyName.willDelayInterval.rawValue, value: willDelayInterval.byteArrayLittleEndian)
}

/// 3.1.3.2.4 Message Expiry Interval
if let willExpiryInterval = self.willExpiryInterval {
properties += getMQTTPropertyData(type: CocoaMQTTPropertyName.willExpiryInterval.rawValue, value: willExpiryInterval.byteArrayLittleEndian)
}

/// 3.1.3.2.3 Payload Format Indicator
if isUTF8EncodedData {
properties += [1, 1]
}else{
properties += [1, 0]
}

/// 3.1.3.2.5 Content Type
if var contentType = self.contentType {
if isUTF8EncodedData {
contentType = contentType.stringUTF8
}
properties += getMQTTPropertyData(type: CocoaMQTTPropertyName.contentType.rawValue, value: contentType.bytesWithLength)
}

/// 3.1.3.2.6 Response Topic
if var willResponseTopic = self.willResponseTopic {
if isUTF8EncodedData {
willResponseTopic = willResponseTopic.stringUTF8
}
properties += getMQTTPropertyData(type: CocoaMQTTPropertyName.responseTopic.rawValue, value: willResponseTopic.bytesWithLength)
}

/// 3.1.3.2.7 Correlation Data
if let willCorrelationData = self.willCorrelationData {
properties += willCorrelationData
}

/// 3.1.3.2.8 User Property
if let willUserProperty = self.willUserProperty {
let dictValues = [String](willUserProperty.values)
for (value) in dictValues {
var res = value
if isUTF8EncodedData {
res = res.stringUTF8
}
properties += getMQTTPropertyData(type: CocoaMQTTPropertyName.userProperty.rawValue, value: res.bytesWithLength)
}
}

retVal += properties
return retVal
}

public init(topic: String, string: String, qos: CocoaMQTTQoS = .qos1, retained: Bool = false) {

self.topic = topic
self.payload = [UInt8](string.utf8)
self.qos = qos
self.retained = retained
}

public init(topic: String, payload: [UInt8], qos: CocoaMQTTQoS = .qos1, retained: Bool = false) {

self.topic = topic
self.payload = payload
self.qos = qos
self.retained = retained
}
}

extension CocoaMQTT5Message {

public override var description: String {
return "CocoaMQTT5Message(topic: \(topic), qos: \(qos), payload: \(payload.summary))"
}
}

// For test
extension CocoaMQTT5Message {

var t_pub_frame: FramePublish {
var frame = FramePublish(topic: topic, payload: payload, qos: qos, msgid: 0)
frame.retained = retained
frame.dup = duplicated
return frame
}

}
Loading

0 comments on commit 8f06f07

Please sign in to comment.