void Write(MqttPropertyId id, byte[] value) { if (value == null) { return; } _bufferWriter.WriteByte((byte)id); _bufferWriter.WriteBinaryData(value); }
public void Write_And_Read_Multiple_Times() { var writer = new MqttBufferWriter(4096, 65535); writer.WriteString("A relative short string."); writer.WriteBinaryData(new byte[1234]); writer.WriteByte(0x01); writer.WriteByte(0x02); writer.WriteVariableByteInteger(5647382); writer.WriteString("A relative short string."); writer.WriteVariableByteInteger(8574489); writer.WriteBinaryData(new byte[48]); writer.WriteByte(2); writer.WriteByte(0x02); writer.WriteString("fjgffiogfhgfhoihgoireghreghreguhreguireoghreouighreouighreughreguiorehreuiohruiorehreuioghreug"); writer.WriteBinaryData(new byte[3]); var readPayload = new ArraySegment <byte>(writer.GetBuffer(), 0, writer.Length).ToArray(); var reader = new MqttBufferReader(); reader.SetBuffer(readPayload, 0, readPayload.Length); for (var i = 0; i < 100000; i++) { reader.Seek(0); reader.ReadString(); reader.ReadBinaryData(); reader.ReadByte(); reader.ReadByte(); reader.ReadVariableByteInteger(); reader.ReadString(); reader.ReadVariableByteInteger(); reader.ReadBinaryData(); reader.ReadByte(); reader.ReadByte(); reader.ReadString(); reader.ReadBinaryData(); } }
public void Write_100_000_Messages() { var writer = new MqttBufferWriter(4096, 65535); for (var i = 0; i < 100000; i++) { writer.WriteString("A relative short string."); writer.WriteByte(0x01); writer.WriteByte(0x02); writer.WriteVariableByteInteger(5647382); writer.WriteString("A relative short string."); writer.WriteVariableByteInteger(8574589); writer.WriteBinaryData(_demoPayload); writer.WriteByte(2); writer.WriteByte(0x02); writer.WriteString("fjgffiogfhgfhoihgoireghreghreguhreguireoghreouighreouighreughreguiorehreuiohruiorehreuioghreug"); writer.WriteBinaryData(_demoPayload); writer.Reset(0); } }
public void GlobalSetup() { TestEnvironment.EnableLogger = false; var writer = new MqttBufferWriter(4096, 65535); writer.WriteString("A relative short string."); writer.WriteBinaryData(_demoPayload); writer.WriteByte(0x01); writer.WriteByte(0x02); writer.WriteVariableByteInteger(5647382); writer.WriteString("A relative short string."); writer.WriteVariableByteInteger(8574489); writer.WriteBinaryData(_demoPayload); writer.WriteByte(2); writer.WriteByte(0x02); writer.WriteString("fjgffiogfhgfhoihgoireghreghreguhreguireoghreouighreouighreughreguiorehreuiohruiorehreuioghreug"); writer.WriteBinaryData(_demoPayload); _readPayload = new ArraySegment <byte>(writer.GetBuffer(), 0, writer.Length).ToArray(); }
byte EncodeConnectPacketV311(MqttConnectPacket packet, MqttBufferWriter bufferWriter) { ValidateConnectPacket(packet); bufferWriter.WriteString("MQTT"); bufferWriter.WriteByte(4); // 3.1.2.2 Protocol Level 4 byte connectFlags = 0x0; if (packet.CleanSession) { connectFlags |= 0x2; } if (packet.WillFlag) { connectFlags |= 0x4; connectFlags |= (byte)((byte)packet.WillQoS << 3); if (packet.WillRetain) { connectFlags |= 0x20; } } if (packet.Password != null && packet.Username == null) { throw new MqttProtocolViolationException("If the User Name Flag is set to 0, the Password Flag MUST be set to 0 [MQTT-3.1.2-22]."); } if (packet.Password != null) { connectFlags |= 0x40; } if (packet.Username != null) { connectFlags |= 0x80; } bufferWriter.WriteByte(connectFlags); bufferWriter.WriteTwoByteInteger(packet.KeepAlivePeriod); bufferWriter.WriteString(packet.ClientId); if (packet.WillFlag) { bufferWriter.WriteString(packet.WillTopic); bufferWriter.WriteBinaryData(packet.WillMessage); } if (packet.Username != null) { bufferWriter.WriteString(packet.Username); } if (packet.Password != null) { bufferWriter.WriteBinaryData(packet.Password); } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Connect)); }
byte EncodeConnectPacket(MqttConnectPacket packet) { if (string.IsNullOrEmpty(packet.ClientId) && !packet.CleanSession) { throw new MqttProtocolViolationException("CleanSession must be set if ClientId is empty [MQTT-3.1.3-7]."); } _bufferWriter.WriteString("MQTT"); _bufferWriter.WriteByte(5); // [3.1.2.2 Protocol Version] byte connectFlags = 0x0; if (packet.CleanSession) { connectFlags |= 0x2; } if (packet.WillFlag) { connectFlags |= 0x4; connectFlags |= (byte)((byte)packet.WillQoS << 3); if (packet.WillRetain) { connectFlags |= 0x20; } } if (packet.Password != null && packet.Username == null) { throw new MqttProtocolViolationException("If the User Name Flag is set to 0, the Password Flag MUST be set to 0 [MQTT-3.1.2-22]."); } if (packet.Password != null) { connectFlags |= 0x40; } if (packet.Username != null) { connectFlags |= 0x80; } _bufferWriter.WriteByte(connectFlags); _bufferWriter.WriteTwoByteInteger(packet.KeepAlivePeriod); _propertiesWriter.WriteSessionExpiryInterval(packet.SessionExpiryInterval); _propertiesWriter.WriteAuthenticationMethod(packet.AuthenticationMethod); _propertiesWriter.WriteAuthenticationData(packet.AuthenticationData); _propertiesWriter.WriteRequestProblemInformation(packet.RequestProblemInformation); _propertiesWriter.WriteRequestResponseInformation(packet.RequestResponseInformation); _propertiesWriter.WriteReceiveMaximum(packet.ReceiveMaximum); _propertiesWriter.WriteTopicAliasMaximum(packet.TopicAliasMaximum); _propertiesWriter.WriteMaximumPacketSize(packet.MaximumPacketSize); _propertiesWriter.WriteUserProperties(packet.UserProperties); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); _bufferWriter.WriteString(packet.ClientId); if (packet.WillFlag) { _propertiesWriter.WritePayloadFormatIndicator(packet.WillPayloadFormatIndicator); _propertiesWriter.WriteMessageExpiryInterval(packet.WillMessageExpiryInterval); _propertiesWriter.WriteResponseTopic(packet.WillResponseTopic); _propertiesWriter.WriteCorrelationData(packet.WillCorrelationData); _propertiesWriter.WriteContentType(packet.WillContentType); _propertiesWriter.WriteUserProperties(packet.WillUserProperties); _propertiesWriter.WriteWillDelayInterval(packet.WillDelayInterval); _propertiesWriter.WriteTo(_bufferWriter); _propertiesWriter.Reset(); _bufferWriter.WriteString(packet.WillTopic); _bufferWriter.WriteBinaryData(packet.WillMessage); } if (packet.Username != null) { _bufferWriter.WriteString(packet.Username); } if (packet.Password != null) { _bufferWriter.WriteBinaryData(packet.Password); } return(MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Connect)); }