void DeserializeAndCompare(MqttPacket packet, string expectedBase64Value, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311) { var writer = WriterFactory(); var serializer = MqttPacketFormatterAdapter.GetMqttPacketFormatter(protocolVersion, writer); var buffer1 = serializer.Encode(packet); using (var headerStream = new MemoryStream(buffer1.Join().ToArray())) { using (var channel = new TestMqttChannel(headerStream)) { using (var adapter = new MqttChannelAdapter( channel, new MqttPacketFormatterAdapter(protocolVersion, new MqttBufferWriter(4096, 65535)), null, new MqttNetEventLogger())) { var receivedPacket = adapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult(); var buffer2 = serializer.Encode(receivedPacket); Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(buffer2.Join().ToArray())); } } } }
TPacket Roundtrip <TPacket>(TPacket packet, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311) where TPacket : MqttBasePacket { var writer = WriterFactory(); var serializer = MqttPacketFormatterAdapter.GetMqttPacketFormatter(protocolVersion, writer); var buffer = serializer.Encode(packet); var channel = new TestMqttChannel(buffer.ToArray()); var adapter = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(protocolVersion, writer), null, new MqttNetLogger()); return((TPacket)adapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult()); //using (var headerStream = new MemoryStream(buffer1.ToArray())) //{ // //var fixedHeader = new byte[2]; // //var header = new MqttPacketReader(channel).ReadFixedHeaderAsync(fixedHeader, CancellationToken.None).GetAwaiter().GetResult().FixedHeader; // //using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, (int)header.RemainingLength)) // //{ // // var reader = ReaderFactory(bodyStream.ToArray()); // // return (T)serializer.Decode(new ReceivedMqttPacket(header.Flags, reader, 0)); // //} //} }
void DeserializeAndCompare(MqttBasePacket packet, string expectedBase64Value, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311) { var writer = WriterFactory(); var serializer = MqttPacketFormatterAdapter.GetMqttPacketFormatter(protocolVersion, writer); var buffer1 = serializer.Encode(packet); using (var headerStream = new MemoryStream(buffer1.ToArray())) { var channel = new TestMqttChannel(headerStream); var adapter = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(protocolVersion, writer), null, new MqttNetLogger()); var receivedPacket = adapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult(); var buffer2 = serializer.Encode(receivedPacket); Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(buffer2.ToArray())); //adapter.ReceivePacketAsync(CancellationToken.None); //var fixedHeader = new byte[2]; //var header = new MqttPacketReader(channel).ReadFixedHeaderAsync(fixedHeader, CancellationToken.None).GetAwaiter().GetResult().FixedHeader; //using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, header.RemainingLength)) //{ // var reader = ReaderFactory(bodyStream.ToArray()); // var deserializedPacket = serializer.Decode(new ReceivedMqttPacket(header.Flags, reader, 0)); // var buffer2 = serializer.Encode(deserializedPacket); // Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(Join(buffer2))); //} } }
public MqttPacketSerializationHelper(MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311, MqttBufferWriter bufferWriter = null) { _protocolVersion = protocolVersion; if (bufferWriter == null) { bufferWriter = new MqttBufferWriter(4096, 65535); } _packetFormatter = MqttPacketFormatterAdapter.GetMqttPacketFormatter(_protocolVersion, bufferWriter); }
TPacket Roundtrip <TPacket>(TPacket packet, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311, MqttBufferWriter bufferWriter = null) where TPacket : MqttPacket { var writer = bufferWriter ?? WriterFactory(); var serializer = MqttPacketFormatterAdapter.GetMqttPacketFormatter(protocolVersion, writer); var buffer = serializer.Encode(packet); using (var channel = new TestMqttChannel(buffer.Join().ToArray())) { var adapter = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(protocolVersion, new MqttBufferWriter(4096, 65535)), null, new MqttNetEventLogger()); return((TPacket)adapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult()); } }
private T Roundtrip <T>(T packet, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311) where T : MqttBasePacket { var writer = WriterFactory(); var serializer = MqttPacketFormatterAdapter.GetMqttPacketFormatter(protocolVersion, writer); var buffer1 = serializer.Encode(packet); using (var headerStream = new MemoryStream(Join(buffer1))) { var channel = new TestMqttChannel(headerStream); var fixedHeader = new byte[2]; var header = new MqttPacketReader(channel).ReadFixedHeaderAsync(fixedHeader, CancellationToken.None).GetAwaiter().GetResult().FixedHeader; using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, (int)header.RemainingLength)) { var reader = ReaderFactory(bodyStream.ToArray()); return((T)serializer.Decode(new ReceivedMqttPacket(header.Flags, reader, 0))); } } }
byte[] Serialize(MqttPacket packet, MqttProtocolVersion protocolVersion) { return(MqttPacketFormatterAdapter.GetMqttPacketFormatter(protocolVersion, WriterFactory()).Encode(packet).Join().ToArray()); }
private byte[] Serialize(MqttBasePacket packet, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311) { var serializer = MqttPacketFormatterAdapter.GetMqttPacketFormatter(protocolVersion, WriterFactory()); return(Join(serializer.Encode(packet))); }