Skip to content

Commit

Permalink
remove FrameSubAck5 and CocoaMQTTReader5 structure
Browse files Browse the repository at this point in the history
  • Loading branch information
leeway1208 committed Jan 26, 2022
1 parent 1b19bc9 commit e3a853b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 355 deletions.
4 changes: 2 additions & 2 deletions CocoaMQTT.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "CocoaMQTT"
s.version = "2.0.3-beta1"
s.version = "2.0.3-beta2"
s.summary = "MQTT v3.1.1 client library for iOS and OS X written with Swift 5"
s.homepage = "https://github.com/emqx/CocoaMQTT"
s.license = { :type => "MIT" }
Expand All @@ -12,7 +12,7 @@ Pod::Spec.new do |s|
s.ios.deployment_target = "9.0"
s.tvos.deployment_target = "10.0"
# s.watchos.deployment_target = "2.0"
s.source = { :git => "https://github.com/emqx/CocoaMQTT.git", :tag => "2.0.3-beta1"}
s.source = { :git => "https://github.com/emqx/CocoaMQTT.git", :tag => "2.0.3-beta2"}
s.default_subspec = 'Core'

s.subspec 'Core' do |ss|
Expand Down
30 changes: 15 additions & 15 deletions Source/CocoaMQTT5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public class CocoaMQTT5: NSObject, CocoaMQTT5Client {
/// message id counter
private var _msgid: UInt16 = 0
fileprivate var socket: CocoaMQTTSocketProtocol
fileprivate var reader: CocoaMQTTReader5?
fileprivate var reader: CocoaMQTTReader?

// Closures
public var didConnectAck: (CocoaMQTT5, CocoaMQTTCONNACKReasonCode, MqttDecodeConnAck) -> Void = { _, _, _ in }
Expand Down Expand Up @@ -376,7 +376,7 @@ public class CocoaMQTT5: NSObject, CocoaMQTT5Client {
/// Not yet established correct MQTT session
public func connect(timeout: TimeInterval) -> Bool {
socket.setDelegate(self, delegateQueue: delegateQueue)
reader = CocoaMQTTReader5(socket: socket, delegate: self)
reader = CocoaMQTTReader(socket: socket, delegate: self)
do {
if timeout > 0 {
try socket.connect(toHost: self.host, onPort: self.port, withTimeout: timeout)
Expand Down Expand Up @@ -664,20 +664,20 @@ extension CocoaMQTT5: CocoaMQTTSocketDelegate {
}
}

// MARK: - CocoaMQTTReader5Delegate
extension CocoaMQTT5: CocoaMQTTReader5Delegate {
// MARK: - CocoaMQTTReaderDelegate
extension CocoaMQTT5: CocoaMQTTReaderDelegate {

func didReceive(_ reader: CocoaMQTTReader5, disconnect: FrameDisconnect) {
func didReceive(_ reader: CocoaMQTTReader, disconnect: FrameDisconnect) {
delegate?.mqtt5(self, didReceiveDisconnectReasonCode: disconnect.receiveReasonCode!)
didDisconnectReasonCode(self, disconnect.receiveReasonCode!)
}

func didReceive(_ reader: CocoaMQTTReader5, auth: FrameAuth) {
func didReceive(_ reader: CocoaMQTTReader, auth: FrameAuth) {
delegate?.mqtt5(self, didReceiveAuthReasonCode: auth.receiveReasonCode!)
didAuthReasonCode(self, auth.receiveReasonCode!)
}

func didReceive(_ reader: CocoaMQTTReader5, connack: FrameConnAck) {
func didReceive(_ reader: CocoaMQTTReader, connack: FrameConnAck) {
printDebug("RECV: \(connack)")

if connack.reasonCode == .success {
Expand Down Expand Up @@ -727,7 +727,7 @@ extension CocoaMQTT5: CocoaMQTTReader5Delegate {
didConnectAck(self, connack.reasonCode!, connack.connackProperties!)
}

func didReceive(_ reader: CocoaMQTTReader5, publish: FramePublish) {
func didReceive(_ reader: CocoaMQTTReader, publish: FramePublish) {
printDebug("RECV: \(publish)")

let message = CocoaMQTT5Message(topic: publish.mqtt5Topic, payload: publish.payload5(), qos: publish.qos, retained: publish.retained)
Expand All @@ -745,7 +745,7 @@ extension CocoaMQTT5: CocoaMQTTReader5Delegate {
}
}

func didReceive(_ reader: CocoaMQTTReader5, puback: FramePubAck) {
func didReceive(_ reader: CocoaMQTTReader, puback: FramePubAck) {
printDebug("RECV: \(puback)")

deliver.ack(by: puback)
Expand All @@ -754,7 +754,7 @@ extension CocoaMQTT5: CocoaMQTTReader5Delegate {
didPublishAck(self, puback.msgid, puback.pubAckProperties!)
}

func didReceive(_ reader: CocoaMQTTReader5, pubrec: FramePubRec) {
func didReceive(_ reader: CocoaMQTTReader, pubrec: FramePubRec) {
printDebug("RECV: \(pubrec)")

deliver.ack(by: pubrec)
Expand All @@ -763,13 +763,13 @@ extension CocoaMQTT5: CocoaMQTTReader5Delegate {
didPublishRec(self, pubrec.msgid, pubrec.pubRecProperties!)
}

func didReceive(_ reader: CocoaMQTTReader5, pubrel: FramePubRel) {
func didReceive(_ reader: CocoaMQTTReader, pubrel: FramePubRel) {
printDebug("RECV: \(pubrel)")

puback(FrameType.pubcomp, msgid: pubrel.msgid)
}

func didReceive(_ reader: CocoaMQTTReader5, pubcomp: FramePubComp) {
func didReceive(_ reader: CocoaMQTTReader, pubcomp: FramePubComp) {
printDebug("RECV: \(pubcomp)")

deliver.ack(by: pubcomp)
Expand All @@ -778,7 +778,7 @@ extension CocoaMQTT5: CocoaMQTTReader5Delegate {
didCompletePublish(self, pubcomp.msgid, pubcomp.pubCompProperties!)
}

func didReceive(_ reader: CocoaMQTTReader5, suback: FrameSubAck5) {
func didReceive(_ reader: CocoaMQTTReader, suback: FrameSubAck) {
printDebug("RECV: \(suback)")
guard let topicsAndQos = subscriptionsWaitingAck.removeValue(forKey: suback.msgid) else {
printWarning("UNEXPECT SUBACK Received: \(suback)")
Expand All @@ -805,7 +805,7 @@ extension CocoaMQTT5: CocoaMQTTReader5Delegate {
didSubscribeTopics(self, success, failed, suback.subAckProperties!)
}

func didReceive(_ reader: CocoaMQTTReader5, unsuback: FrameUnsubAck) {
func didReceive(_ reader: CocoaMQTTReader, unsuback: FrameUnsubAck) {
printDebug("RECV: \(unsuback)")

guard let topics = unsubscriptionsWaitingAck.removeValue(forKey: unsuback.msgid) else {
Expand All @@ -823,7 +823,7 @@ extension CocoaMQTT5: CocoaMQTTReader5Delegate {
didUnsubscribeTopics(self, removeTopics, unsuback.unSubAckProperties!)
}

func didReceive(_ reader: CocoaMQTTReader5, pingresp: FramePingResp) {
func didReceive(_ reader: CocoaMQTTReader, pingresp: FramePingResp) {
printDebug("RECV: \(pingresp)")

delegate?.mqtt5DidReceivePong(self)
Expand Down
199 changes: 0 additions & 199 deletions Source/CocoaMQTTReader5.swift

This file was deleted.

50 changes: 42 additions & 8 deletions Source/FrameSubAck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,52 @@ extension FrameSubAck: InitialWithBytes {

self.packetFixedHeaderType = packetFixedHeaderType

// the bytes length must bigger than 3
guard bytes.count >= 3 else {
return nil
var protocolVersion = "";
if let storage = CocoaMQTTStorage() {
protocolVersion = storage.queryMQTTVersion()
}

self.msgid = UInt16(bytes[0]) << 8 + UInt16(bytes[1])
self.grantedQos = []
for i in 2 ..< bytes.count {
guard let qos = CocoaMQTTQoS(rawValue: bytes[i]) else {
if (protocolVersion == "5.0"){
// the bytes length must bigger than 3
guard bytes.count >= 4 else {
return nil
}
self.grantedQos.append(qos)

self.msgid = UInt16(bytes[0]) << 8 + UInt16(bytes[1])
self.grantedQos = []
for i in 3 ..< bytes.count {
guard let qos = CocoaMQTTQoS(rawValue: bytes[i]) else {
return nil
}
self.grantedQos.append(qos)
}

self.reasonCodes = [CocoaMQTTSUBACKReasonCode]()
for i in 3 ..< bytes.count {
guard let qos = CocoaMQTTSUBACKReasonCode(rawValue: bytes[i]) else {
return nil
}
self.reasonCodes! += [qos]
}

self.subAckProperties = MqttDecodeSubAck()
self.subAckProperties!.decodeSubAck(fixedHeader: packetFixedHeaderType, pubAckData: bytes)

}else{
// the bytes length must bigger than 3
guard bytes.count >= 3 else {
return nil
}

self.msgid = UInt16(bytes[0]) << 8 + UInt16(bytes[1])
self.grantedQos = []
for i in 2 ..< bytes.count {
guard let qos = CocoaMQTTQoS(rawValue: bytes[i]) else {
return nil
}
self.grantedQos.append(qos)
}

}

}
Expand Down
Loading

0 comments on commit e3a853b

Please sign in to comment.