Skip to content

Commit

Permalink
puback is now sent only on QoS 1+
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Nov 16, 2016
1 parent 481313a commit b7631c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
21 changes: 20 additions & 1 deletion src/Emitter.Runtime/Network/Mqtt/MqttContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,29 @@ namespace Emitter.Network
/// </summary>
public sealed class MqttContext
{
/// <summary>
/// Constructs a context from the connect packet.
/// </summary>
/// <param name="packet">The packet to use.</param>
internal MqttContext(MqttConnectPacket packet)
{
this.Version = packet.ProtocolVersion;
this.QoS = packet.QoS;
this.IsEmitter = packet.IsEmitter;
this.ClientId = packet.ClientId;
this.Username = packet.Username;
}

/// <summary>
/// Creates a new context.
/// </summary>
/// <param name="version">The version of MQTT.</param>
/// <param name="isEmitter">Whether this is our special implementation.</param>
/// <param name="id">The client id specified in the MQTT connect packet.</param>
public MqttContext(MqttProtocolVersion version, bool isEmitter, string id, string username)
public MqttContext(MqttProtocolVersion version, QoS qos, bool isEmitter, string id, string username)
{
this.Version = version;
this.QoS = qos;
this.IsEmitter = isEmitter;
this.ClientId = id;
this.Username = username;
Expand All @@ -48,6 +62,11 @@ public MqttContext(MqttProtocolVersion version, bool isEmitter, string id, strin
/// </summary>
public readonly bool IsEmitter;

/// <summary>
/// Gets the quality of service requested
/// </summary>
public readonly QoS QoS;

/// <summary>
/// Gets the MQTT client id passed during the connect.
/// </summary>
Expand Down
12 changes: 4 additions & 8 deletions src/Emitter.Runtime/Network/Mqtt/MqttHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ internal static unsafe class MqttHandler
public static ProcessingState OnConnect(IClient client, MqttConnectPacket packet)
{
// Set the protocol version to the client
client.Context = new MqttContext(
packet.ProtocolVersion,
packet.IsEmitter,
packet.ClientId,
packet.Username
);

client.Context = new MqttContext(packet);
switch (packet.ProtocolVersion)
{
case MqttProtocolVersion.V3_1: break;
Expand Down Expand Up @@ -131,7 +125,9 @@ public static ProcessingState OnPublish(IClient client, MqttPublishPacket packet
HandlePublish.Process(client, packet.Channel, packet.Message);

// Send the ack and stop the processing
client.SendMqttPuback(packet.MessageId);
if (client.Context.QoS > QoS.AtMostOnce)
client.SendMqttPuback(packet.MessageId);

return ProcessingState.Stop;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Emitter.Runtime/Network/Mqtt/MqttSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public MqttContext Context
{
get
{
return new MqttContext(MqttProtocolVersion.VEmitter, true, "broker", "broker");
return new MqttContext(MqttProtocolVersion.VEmitter, QoS.AtMostOnce, true, "broker", "broker");
}
set
{
Expand Down

0 comments on commit b7631c5

Please sign in to comment.