Skip to content

Commit

Permalink
MQTT-Client-Framework 0.3.6
Browse files Browse the repository at this point in the history
>Release date: 2015-11-06

[FIX] crashes when publishing from different threads closes #61
[PROBABLE FIX] crashes when publishing from different threads #59 #56 #53 #45
  • Loading branch information
Christoph Krey committed Nov 6, 2015
1 parent 142b2c4 commit 7700937
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 258 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
MQTT-Client-Framework iOS Release Notes
=======================================

## MQTT-Client-Framework 0.3.6
>Release date: 2015-11-06
[FIX] crashes when publishing from different threads closes #61
[PROBABLE FIX] crashes when publishing from different threads #59 #56 #53 #45

## MQTT-Client-Framework 0.3.5
>Release date: 2015-11-04
Expand Down
4 changes: 2 additions & 2 deletions MQTTClient.podspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Pod::Spec.new do |s|
s.name = "MQTTClient"
s.version = "0.3.5"
s.version = "0.3.6"
s.summary = "iOS, OSX and tvOS native ObjectiveC MQTT Framework"
s.homepage = "https://github.com/ckrey/MQTT-Client-Framework"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Christoph Krey" => "krey.christoph@gmail.com" }
s.source = { :git => "https://github.com/ckrey/MQTT-Client-Framework.git", :tag => "0.3.5" }
s.source = { :git => "https://github.com/ckrey/MQTT-Client-Framework.git", :tag => "0.3.6" }

s.source_files = "MQTTClient/MQTTClient", "MQTTClient/MQTTClient/**/*.{h,m}"
s.requires_arc = true
Expand Down
8 changes: 4 additions & 4 deletions MQTTClient/MQTTClient/MQTTEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
// based on
//
// Copyright (c) 2011, 2013, 2lemetry LLC
//
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
//
// Contributors:
// Kyle Roche - initial API and implementation and/or initial documentation
//
//

#import <Foundation/Foundation.h>
#import "MQTTMessage.h"
Expand Down Expand Up @@ -65,7 +65,7 @@ typedef enum {
- (void)close;
- (MQTTEncoderStatus)status;
- (void)stream:(NSStream*)sender handleEvent:(NSStreamEvent)eventCode;
- (void)encodeMessage:(MQTTMessage*)msg;
- (BOOL)encodeMessage:(MQTTMessage*)msg;

@end

139 changes: 71 additions & 68 deletions MQTTClient/MQTTClient/MQTTEncoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
// based on
//
// Copyright (c) 2011, 2013, 2lemetry LLC
//
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
//
// Contributors:
// Kyle Roche - initial API and implementation and/or initial documentation
//
//

#import "MQTTEncoder.h"

Expand Down Expand Up @@ -78,17 +78,17 @@ - (BOOL)applySSLSecurityPolicy:(NSStream *)writeStream withEvent:(NSStreamEvent)
if(!self.securityPolicy){
return YES;
}

// apply the policy only once.
if(self.securityPolicyAlreadyApplied){
return YES;
return YES;
}

SecTrustRef serverTrust = (__bridge SecTrustRef) [writeStream propertyForKey: (__bridge NSString *)kCFStreamPropertySSLPeerTrust];
if(!serverTrust){
return NO;
}

BOOL isValid = [self.securityPolicy evaluateServerTrust:serverTrust forDomain:self.securityDomain];
self.securityPolicyAlreadyApplied = isValid;
return isValid;
Expand All @@ -110,10 +110,10 @@ - (void)stream:(NSStream*)sender handleEvent:(NSStreamEvent)eventCode {
self.status = MQTTEncoderStatusError;
NSError * sslError = [NSError errorWithDomain:@"MQTT"
code:errSSLXCertChainInvalid
userInfo:@{NSLocalizedDescriptionKey : @"Unable to apply security policy, the SSL connection is insecure!"}];
userInfo:@{NSLocalizedDescriptionKey : @"Unable to apply security policy, the SSL connection is insecure!"}];
[self.delegate encoder:self handleEvent:MQTTEncoderEventErrorOccurred error:sslError];
}

if (self.status == MQTTEncoderStatusInitializing) {
self.status = MQTTEncoderStatusReady;
[self.delegate encoder:self handleEvent:MQTTEncoderEventReady error:nil];
Expand Down Expand Up @@ -159,66 +159,69 @@ - (void)stream:(NSStream*)sender handleEvent:(NSStreamEvent)eventCode {
}
}

- (void)encodeMessage:(MQTTMessage*)msg {
UInt8 header;
NSInteger n, length;

if (self.status != MQTTEncoderStatusReady) {
if (DEBUGENC) NSLog(@"%@ not status ready %d", self, self.status);
return;
}

assert (self.buffer == NULL);
assert (self.byteIndex == 0);

self.buffer = [[NSMutableData alloc] init];

// encode fixed header
header = ([msg type] & 0x0f) << 4;
if (msg.dupFlag) {
header |= 0x08;
}
header |= ([msg qos] & 0x03) << 1;
if ([msg retainFlag]) {
header |= 0x01;
}
[self.buffer appendBytes:&header length:1];

// encode remaining length
length = [[msg data] length];
do {
UInt8 digit = length % 128;
length /= 128;
if (length > 0) {
digit |= 0x80;
- (BOOL)encodeMessage:(MQTTMessage*)msg {
@synchronized(self) {
UInt8 header;
NSInteger n, length;

if (self.status != MQTTEncoderStatusReady) {
if (DEBUGENC) NSLog(@"%@ not status ready %d", self, self.status);
return FALSE;
}
[self.buffer appendBytes:&digit length:1];
}
while (length > 0);


// encode message data
if ([msg data] != NULL) {
[self.buffer appendData:[msg data]];
}

if (DEBUGENC) NSLog(@"%@ buffer to write (%lu)=%@...", self, (unsigned long)self.buffer.length,
[self.buffer subdataWithRange:NSMakeRange(0, MIN(16, self.buffer.length))]);

[self.delegate encoder:self sending:msg.type qos:msg.qos retained:msg.retainFlag duped:msg.dupFlag mid:msg.mid data:self.buffer];

n = [self.stream write:[self.buffer bytes] maxLength:[self.buffer length]];
if (n == -1) {
self.status = MQTTEncoderStatusError;
[self.delegate encoder:self handleEvent:MQTTEncoderEventErrorOccurred error:self.stream.streamError];
}
else if (n < [self.buffer length]) {
self.byteIndex += n;
self.status = MQTTEncoderStatusSending;
}
else {
self.buffer = NULL;
// XXX [delegate encoder:self handleEvent:MQTTEncoderEventReady];

assert (self.buffer == NULL);
assert (self.byteIndex == 0);

self.buffer = [[NSMutableData alloc] init];

// encode fixed header
header = ([msg type] & 0x0f) << 4;
if (msg.dupFlag) {
header |= 0x08;
}
header |= ([msg qos] & 0x03) << 1;
if ([msg retainFlag]) {
header |= 0x01;
}
[self.buffer appendBytes:&header length:1];

// encode remaining length
length = [[msg data] length];
do {
UInt8 digit = length % 128;
length /= 128;
if (length > 0) {
digit |= 0x80;
}
[self.buffer appendBytes:&digit length:1];
}
while (length > 0);


// encode message data
if ([msg data] != NULL) {
[self.buffer appendData:[msg data]];
}

if (DEBUGENC) NSLog(@"%@ buffer to write (%lu)=%@...", self, (unsigned long)self.buffer.length,
[self.buffer subdataWithRange:NSMakeRange(0, MIN(16, self.buffer.length))]);

[self.delegate encoder:self sending:msg.type qos:msg.qos retained:msg.retainFlag duped:msg.dupFlag mid:msg.mid data:self.buffer];

n = [self.stream write:[self.buffer bytes] maxLength:[self.buffer length]];
if (n == -1) {
self.status = MQTTEncoderStatusError;
[self.delegate encoder:self handleEvent:MQTTEncoderEventErrorOccurred error:self.stream.streamError];
}
else if (n < [self.buffer length]) {
self.byteIndex += n;
self.status = MQTTEncoderStatusSending;
}
else {
self.buffer = NULL;
// XXX [delegate encoder:self handleEvent:MQTTEncoderEventReady];
}
return TRUE;
}
}

Expand Down
Loading

0 comments on commit 7700937

Please sign in to comment.