Skip to content

Commit

Permalink
Changed strings to ansistrings allowing topics and payloads to exceed…
Browse files Browse the repository at this point in the history
… 255 bytes
  • Loading branch information
ZiCog committed Sep 18, 2013
1 parent 96049da commit d59ac6f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 29 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ Changes:
--------

1) Rewrote the reader thread loop so as to make it simpler and faster also fixes a bug whereby the
client would segfault if the server went down.
client would segfault if the server went down and a major bug where timeouts on rx would cause data corruption.

2) Replaced the original client demo code with a simpler demo that does not use forms. I am using
this in an embedded system with no display.

3) Also includes the parts of Ararat Synapse required to build.
3) Changed all strings to ansi strings so that it can have topics and payloads longer than 255 bytes.

4) Also includes the parts of Ararat Synapse required to build.

To build the demo:
------------------
Expand Down
40 changes: 20 additions & 20 deletions TMQTTClient/MQTT.pas
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ interface

TMQTTClient = class(TObject)
private
FClientID: string;
FHostname: string;
FClientID: ansistring;
FHostname: ansistring;
FPort: Integer;
FReadThread: TMQTTReadThread;
FSocket: TTCPBlockSocket;
Expand All @@ -81,8 +81,8 @@ TMQTTClient = class(TObject)
FUnSubAckEvent: TUnSubAckEvent;
// Gets a next Message ID and increases the Message ID Increment
function GetMessageID: TBytes;
// Takes a string and converts to An Array of Bytes preceded by 2 Length Bytes.
function StrToBytes(str: string; perpendLength: boolean): TUTF8Text;
// Takes a ansistring and converts to An Array of Bytes preceded by 2 Length Bytes.
function StrToBytes(str: ansistring; perpendLength: boolean): TUTF8Text;
// Byte Array Helper Functions
procedure AppendArray(var Dest: TUTF8Text; Source: Array of Byte);
procedure CopyIntoArray(var DestArray: Array of Byte; SourceArray: Array of Byte; StartIndex: integer);
Expand All @@ -92,7 +92,7 @@ TMQTTClient = class(TObject)
function RemainingLength(MessageLength: Integer): TRemainingLength;
// Variable Header per command creation funcs
function VariableHeaderConnect(KeepAlive: Word): TBytes;
function VariableHeaderPublish(topic: string): TBytes;
function VariableHeaderPublish(topic: ansistring): TBytes;
function VariableHeaderSubscribe: TBytes;
function VariableHeaderUnsubscribe: TBytes;
// Helper Function - Puts the seperate component together into an Array of Bytes for transmission
Expand All @@ -105,21 +105,21 @@ TMQTTClient = class(TObject)
procedure OnRTPingResp(Sender: TObject);
procedure OnRTSubAck(Sender: TObject; MessageID: integer; GrantedQoS: integer);
procedure OnRTUnSubAck(Sender: TObject; MessageID: integer);
procedure OnRTPublish(Sender: TObject; topic, payload: string);
procedure OnRTPublish(Sender: TObject; topic, payload: ansistring);
public
function isConnected: boolean;
function Connect: boolean;
function Disconnect: boolean;
procedure ForceDisconnect;
function Publish(Topic: string; sPayload: string): boolean; overload;
function Publish(Topic: string; sPayload: string; Retain: boolean): boolean; overload;
function Subscribe(Topic: string): integer;
function Unsubscribe(Topic: string): integer;
function Publish(Topic: ansistring; sPayload: ansistring): boolean; overload;
function Publish(Topic: ansistring; sPayload: ansistring; Retain: boolean): boolean; overload;
function Subscribe(Topic: ansistring): integer;
function Unsubscribe(Topic: ansistring): integer;
function PingReq: boolean;
constructor Create(Hostname: string; Port: integer); overload;
constructor Create(Hostname: ansistring; Port: integer); overload;
destructor Destroy; override;

property ClientID : string read FClientID write FClientID;
property ClientID : ansistring read FClientID write FClientID;
property OnConnAck : TConnAckEvent read FConnAckEvent write FConnAckEvent;
property OnPublish : TPublishEvent read FPublishEvent write FPublishEvent;
property OnPingResp : TPingRespEvent read FPingRespEvent write FPingRespEvent;
Expand Down Expand Up @@ -247,7 +247,7 @@ function TMQTTClient.PingReq: boolean;
@param Retain Should this message be retained for clients connecting subsequently
@return Returns whether the Data was written successfully to the socket.
------------------------------------------------------------------------------*}
function TMQTTClient.Publish(Topic, sPayload: string; Retain: boolean): boolean;
function TMQTTClient.Publish(Topic, sPayload: ansistring; Retain: boolean): boolean;
var
Data: TBytes;
FH: Byte;
Expand All @@ -273,7 +273,7 @@ function TMQTTClient.Publish(Topic, sPayload: string; Retain: boolean): boolean;
@param sPayload The Actual Payload of the message eg 18 degrees celcius
@return Returns whether the Data was written successfully to the socket.
------------------------------------------------------------------------------*}
function TMQTTClient.Publish(Topic, sPayload: string): boolean;
function TMQTTClient.Publish(Topic, sPayload: ansistring): boolean;
begin
Publish(Topic, sPayload, False);
end;
Expand All @@ -285,7 +285,7 @@ function TMQTTClient.Publish(Topic, sPayload: string): boolean;
@return Returns the Message ID used to send the message for the purpose of comparing
it to the Message ID used later in the SUBACK event handler.
------------------------------------------------------------------------------*}
function TMQTTClient.Subscribe(Topic: string): integer;
function TMQTTClient.Subscribe(Topic: ansistring): integer;
var
Data: TBytes;
FH: Byte;
Expand Down Expand Up @@ -314,7 +314,7 @@ function TMQTTClient.Subscribe(Topic: string): integer;
@return Returns the Message ID used to send the message for the purpose of comparing
it to the Message ID used later in the UNSUBACK event handler.
------------------------------------------------------------------------------*}
function TMQTTClient.Unsubscribe(Topic: string): integer;
function TMQTTClient.Unsubscribe(Topic: ansistring): integer;
var
Data: TBytes;
FH: Byte;
Expand Down Expand Up @@ -350,7 +350,7 @@ function TMQTTClient.isConnected: boolean;
@param Port Port of the MQTT Server
@return Instance
------------------------------------------------------------------------------*}
constructor TMQTTClient.Create(Hostname: string; Port: integer);
constructor TMQTTClient.Create(Hostname: ansistring; Port: integer);
begin
inherited Create;
Randomize;
Expand Down Expand Up @@ -404,7 +404,7 @@ function TMQTTClient.SocketWrite(Data: TBytes): boolean;
end;
end;

function TMQTTClient.StrToBytes(str: string; perpendLength: boolean): TUTF8Text;
function TMQTTClient.StrToBytes(str: ansistring; perpendLength: boolean): TUTF8Text;
var
i, offset: integer;
begin
Expand Down Expand Up @@ -479,7 +479,7 @@ function TMQTTClient.VariableHeaderConnect(KeepAlive: Word): TBytes;
Result[iByteIndex] := KeepAlive;
end;

function TMQTTClient.VariableHeaderPublish(topic: string): TBytes;
function TMQTTClient.VariableHeaderPublish(topic: ansistring): TBytes;
var
BytesTopic: TUTF8Text;
begin
Expand Down Expand Up @@ -550,7 +550,7 @@ procedure TMQTTClient.OnRTPingResp(Sender: TObject);
if Assigned(OnPingResp) then OnPingResp(Self);
end;

procedure TMQTTClient.OnRTPublish(Sender: TObject; topic, payload: string);
procedure TMQTTClient.OnRTPublish(Sender: TObject; topic, payload: ansistring);
begin
if Assigned(OnPublish) then OnPublish(Self, topic, payload);
end;
Expand Down
8 changes: 4 additions & 4 deletions TMQTTClient/MQTTReadThread.pas
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface
PTCPBlockSocket = ^TTCPBlockSocket;

TConnAckEvent = procedure (Sender: TObject; ReturnCode: integer) of object;
TPublishEvent = procedure (Sender: TObject; topic, payload: string) of object;
TPublishEvent = procedure (Sender: TObject; topic, payload: ansistring) of object;
TPingRespEvent = procedure (Sender: TObject) of object;
TSubAckEvent = procedure (Sender: TObject; MessageID: integer; GrantedQoS: integer) of object;
TUnSubAckEvent = procedure (Sender: TObject; MessageID: integer) of object;
Expand All @@ -67,7 +67,7 @@ TMQTTReadThread = class(TThread)
FUnSubAckEvent: TUnSubAckEvent;
// This takes a 1-4 Byte Remaining Length bytes as per the spec and returns the Length value it represents
// Increases the size of the Dest array and Appends NewBytes to the end of DestArray
// Takes a 2 Byte Length array and returns the length of the string it preceeds as per the spec.
// Takes a 2 Byte Length array and returns the length of the ansistring it preceeds as per the spec.
function BytesToStrLength(LengthBytes: TBytes): integer;
// This is our data processing and event firing command. To be called via Synchronize.
procedure HandleData;
Expand Down Expand Up @@ -158,8 +158,8 @@ procedure TMQTTReadThread.HandleData;
MessageType: Byte;
DataLen: integer;
QoS: integer;
Topic: string;
Payload: string;
Topic: ansistring;
Payload: ansistring;
ResponseVH: TBytes;
ConnectReturn: Integer;
begin
Expand Down
4 changes: 2 additions & 2 deletions examples/embeddedApp/embeddedApp.pas
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
Procedure OnPingResp(Sender: TObject);
Procedure OnSubAck(Sender: TObject; MessageID : longint; GrantedQoS : longint);
Procedure OnUnSubAck(Sender: TObject);
Procedure OnPublish(Sender: TObject; topic, payload: String);
Procedure OnPublish(Sender: TObject; topic, payload: ansistring);
public
Procedure run ();
End;
Expand All @@ -69,7 +69,7 @@
writeln ('Connection Acknowledged, Return Code: ' + IntToStr(Ord(ReturnCode)));
End;

Procedure TembeddedApp.OnPublish(Sender: TObject; topic, payload: String);
Procedure TembeddedApp.OnPublish(Sender: TObject; topic, payload: ansistring);
Begin
writeln ('Publish Received. Topic: '+ topic + ' Payload: ' + payload);
End;
Expand Down
34 changes: 33 additions & 1 deletion examples/embeddedApp/payload.txt
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789;
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.
All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy.

0 comments on commit d59ac6f

Please sign in to comment.