/// <summary> /// Sends a PING message to the server. This is automatically done to keep the connection alive. Only call this method /// if you want to send additional PING messages, apart from the ones sent to keep the connection alive. /// </summary> public async Task PING() { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.PINGREQ << 4); Packet.WriteUInt(0); byte[] PacketData = Packet.GetPacket(); await this.Write(PacketData, 0, null); EventHandlerAsync h = this.OnPing; if (h != null) { try { await h(this, new EventArgs()); } catch (Exception ex) { this.Exception(ex); Log.Critical(ex); } } }
/// <summary> /// Unsubscribes from information earlier subscribed to. Topics can include wildcards. /// </summary> /// <param name="Topics">Topics</param> /// <returns>Packet identifier assigned to unsubscription.</returns> public async Task <ushort> UNSUBSCRIBE(params string[] Topics) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) { PacketIdentifier = this.packetIdentifier++; } Payload.WriteUInt16(PacketIdentifier); foreach (string Topic in Topics) { Payload.WriteString(Topic); } byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.UNSUBSCRIBE << 4); b |= 2; Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); await this.Write(PacketData, PacketIdentifier, null); return(PacketIdentifier); }
/// <summary> /// Unsubscribes from information earlier subscribed to. Topics can include wildcards. /// </summary> /// <param name="Topics">Topics</param> /// <returns>Packet identifier assigned to unsubscription.</returns> public async Task <ushort> UNSUBSCRIBE(params string[] Topics) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) { PacketIdentifier = this.packetIdentifier++; } Payload.WriteUInt16(PacketIdentifier); foreach (string Topic in Topics) { Payload.WriteString(Topic); } byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.UNSUBSCRIBE << 4); b |= 2; Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); if (this.HasSniffers) { StringBuilder sb = new StringBuilder(); bool First = true; sb.Append("UNSUBSCRIBE("); foreach (string Topic in Topics) { if (First) { First = false; } else { sb.Append(", "); } sb.Append(Topic); } sb.Append(')'); this.Information(sb.ToString()); } await this.Write(PacketData, PacketIdentifier, null); return(PacketIdentifier); }
private async Task <ushort> PUBLISH(string Topic, MqttQualityOfService QoS, bool Retain, bool Duplicate, byte[] Data) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; Payload.WriteString(Topic); if (QoS > MqttQualityOfService.AtMostOnce) { PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) { PacketIdentifier = this.packetIdentifier++; } Payload.WriteUInt16(PacketIdentifier); } else { PacketIdentifier = 0; } Payload.WriteBytes(Data); byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.PUBLISH << 4); if (Duplicate) { b |= 8; } b |= (byte)((int)QoS << 1); if (Retain) { b |= 1; } Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); if (this.HasSniffers) { this.Information("PUBLISH(" + QoS.ToString() + ":" + Topic + ")"); } await this.Write(PacketData, PacketIdentifier, null); return(PacketIdentifier); }
private Task PINGRESP() { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.PINGRESP << 4); Packet.WriteUInt(0); byte[] PacketData = Packet.GetPacket(); return(this.Write(PacketData, 0, null)); }
private void PINGRESP() { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.PINGRESP << 4); Packet.WriteUInt(0); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, 0, null); }
private void PUBREL(ushort PacketIdentifier) { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)(((int)MqttControlPacketType.PUBREL << 4) | 2)); Packet.WriteUInt(2); Packet.WriteUInt16(PacketIdentifier); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, PacketIdentifier, null); }
private void PUBREC(ushort PacketIdentifier) { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.PUBREC << 4); Packet.WriteUInt(2); Packet.WriteUInt16(PacketIdentifier); byte[] PacketData = Packet.GetPacket(); this.Write(PacketData, 0, null); }
private Task PUBCOMP(ushort PacketIdentifier) { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.PUBCOMP << 4); Packet.WriteUInt(2); Packet.WriteUInt16(PacketIdentifier); byte[] PacketData = Packet.GetPacket(); return(this.Write(PacketData, 0, null)); }
private ushort PUBLISH(string Topic, MqttQualityOfService QoS, bool Retain, bool Duplicate, byte[] Data) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; Payload.WriteString(Topic); if (QoS > MqttQualityOfService.AtMostOnce) { PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) { PacketIdentifier = this.packetIdentifier++; } Payload.WriteUInt16(PacketIdentifier); } else { PacketIdentifier = 0; } Payload.WriteBytes(Data); byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.PUBLISH << 4); if (Duplicate) { b |= 8; } b |= (byte)((int)QoS << 1); if (Retain) { b |= 1; } Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, PacketIdentifier, null); return(PacketIdentifier); }
private Task PINGRESP() { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.PINGRESP << 4); Packet.WriteUInt(0); byte[] PacketData = Packet.GetPacket(); if (this.HasSniffers) { this.Information("PINGRESP"); } return(this.Write(PacketData, 0, null)); }
private void CONNECT(int KeepAliveSeconds) { this.State = MqttState.Authenticating; this.keepAliveSeconds = KeepAliveSeconds; this.nextPing = DateTime.Now.AddMilliseconds(KeepAliveSeconds * 500); this.secondTimer = new Timer(this.SecondTimer_Elapsed, null, 1000, 1000); BinaryOutput Payload = new BinaryOutput(); Payload.WriteString("MQTT"); Payload.WriteByte(4); // v3.1.1 byte b = 2; // Clean session. Payload.WriteByte(b); Payload.WriteByte((byte)(KeepAliveSeconds >> 8)); Payload.WriteByte((byte)KeepAliveSeconds); Payload.WriteString(this.clientId); if (!string.IsNullOrEmpty(this.userName)) { b |= 128; Payload.WriteString(this.userName); if (!string.IsNullOrEmpty(this.password)) { b |= 64; Payload.WriteString(this.password); } } byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.CONNECT << 4); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); this.BeginWrite(PacketData, 0, null); this.inputState = 0; Task T = this.ReadLoop(); }
private Task PUBCOMP(ushort PacketIdentifier) { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.PUBCOMP << 4); Packet.WriteUInt(2); Packet.WriteUInt16(PacketIdentifier); byte[] PacketData = Packet.GetPacket(); if (this.HasSniffers) { this.Information("PUBCOMP"); } return(this.Write(PacketData, 0, null)); }
private async Task DISCONNECT() { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.DISCONNECT << 4); Packet.WriteUInt(2); byte[] PacketData = Packet.GetPacket(); TaskCompletionSource <bool> Done = new TaskCompletionSource <bool>(); await this.Write(PacketData, 0, (sender, e) => { this.State = MqttState.Offline; Done.TrySetResult(true); }); await Task.WhenAny(Done.Task, Task.Delay(1000)); }
private void DISCONNECT() { BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.DISCONNECT << 4); Packet.WriteUInt(2); byte[] PacketData = Packet.GetPacket(); ManualResetEvent Done = new ManualResetEvent(false); this.BeginWrite(PacketData, 0, (sender, e) => { this.State = MqttState.Offline; Done.Set(); }); Done.WaitOne(1000); }
/// <summary> /// Subscribes to information from a set of topics. Topics can include wildcards. /// </summary> /// <param name="Topics">Topics together with Quality of Service levels for each topic.</param> /// <returns>Packet identifier assigned to subscription.</returns> public async Task <ushort> SUBSCRIBE(params KeyValuePair <string, MqttQualityOfService>[] Topics) { BinaryOutput Payload = new BinaryOutput(); ushort PacketIdentifier; PacketIdentifier = this.packetIdentifier++; if (PacketIdentifier == 0) { PacketIdentifier = this.packetIdentifier++; } Payload.WriteUInt16(PacketIdentifier); foreach (KeyValuePair <string, MqttQualityOfService> Pair in Topics) { Payload.WriteString(Pair.Key); Payload.WriteByte((byte)Pair.Value); } byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); byte b = (byte)((int)MqttControlPacketType.SUBSCRIBE << 4); b |= 2; Packet.WriteByte(b); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); await this.Write(PacketData, PacketIdentifier, null); return(PacketIdentifier); }
private async Task CONNECT(int KeepAliveSeconds) { this.State = MqttState.Authenticating; this.keepAliveSeconds = KeepAliveSeconds; this.nextPing = DateTime.Now.AddMilliseconds(KeepAliveSeconds * 500); this.secondTimer = new Timer(this.SecondTimer_Elapsed, null, 1000, 1000); BinaryOutput Payload = new BinaryOutput(); Payload.WriteString("MQTT"); Payload.WriteByte(4); // v3.1.1 byte b = 2; // Clean session. if (this.will) { b |= 4; b |= (byte)(((int)this.willQoS) << 3); if (this.willRetain) { b |= 32; } } if (!string.IsNullOrEmpty(this.userName)) { b |= 128; if (!string.IsNullOrEmpty(this.password)) { b |= 64; } } Payload.WriteByte(b); Payload.WriteByte((byte)(KeepAliveSeconds >> 8)); Payload.WriteByte((byte)KeepAliveSeconds); Payload.WriteString(this.clientId); if (this.will) { Payload.WriteString(this.willTopic); int l = this.willData.Length; Payload.WriteByte((byte)(l >> 8)); Payload.WriteByte((byte)l); Payload.WriteBytes(this.willData); } if (!string.IsNullOrEmpty(this.userName)) { Payload.WriteString(this.userName); if (!string.IsNullOrEmpty(this.password)) { Payload.WriteString(this.password); } } byte[] PayloadData = Payload.GetPacket(); BinaryOutput Packet = new BinaryOutput(); Packet.WriteByte((byte)MqttControlPacketType.CONNECT << 4); Packet.WriteUInt((uint)PayloadData.Length); Packet.WriteBytes(PayloadData); byte[] PacketData = Packet.GetPacket(); await this.Write(PacketData, 0, null); this.inputState = 0; }
/// <summary> /// Publishes information on a topic. /// </summary> /// <param name="Topic">Topic name</param> /// <param name="QoS">Quality of service</param> /// <param name="Retain">If topic should retain information.</param> /// <param name="Data">Binary data to send.</param> /// <returns>Packet identifier assigned to data.</returns> public Task <ushort> PUBLISH(string Topic, MqttQualityOfService QoS, bool Retain, BinaryOutput Data) { return(this.PUBLISH(Topic, QoS, Retain, false, Data.GetPacket())); }