MqttProtocolVersion DeserializeAndDetectVersion(MqttPacketFormatterAdapter packetFormatterAdapter, byte[] buffer) { var channel = new TestMqttChannel(buffer); var adapter = new MqttChannelAdapter(channel, packetFormatterAdapter, null, new MqttNetLogger()); adapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult(); return(packetFormatterAdapter.ProtocolVersion); //using (var headerStream = new MemoryStream(buffer)) //{ // //var fixedHeader = new byte[2]; // //var header = new MqttPacketReader(channel).ReadFixedHeaderAsync(fixedHeader, CancellationToken.None).GetAwaiter().GetResult().FixedHeader; // //using (var bodyStream = new MemoryStream(buffer, (int)headerStream.Position, (int)header.RemainingLength)) // //{ // // var reader = ReaderFactory(bodyStream.ToArray()); // // var packet = new ReceivedMqttPacket(header.Flags, reader, 0); // // packetFormatterAdapter.DetectProtocolVersion(packet); // // return adapter.ProtocolVersion; // //} //} }
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))); //} } }
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())); } } } }
MqttProtocolVersion DeserializeAndDetectVersion(MqttPacketFormatterAdapter packetFormatterAdapter, byte[] buffer) { var channel = new TestMqttChannel(buffer); var adapter = new MqttChannelAdapter(channel, packetFormatterAdapter, null, new MqttNetEventLogger()); adapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult(); return(packetFormatterAdapter.ProtocolVersion); }
public MqttPacket Decode(MqttPacketBuffer buffer) { using (var channel = new MemoryMqttChannel(buffer.ToArray())) { var formatterAdapter = new MqttPacketFormatterAdapter(_protocolVersion, new MqttBufferWriter(4096, 65535)); var adapter = new MqttChannelAdapter(channel, formatterAdapter, null, MqttNetNullLogger.Instance); return adapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult(); } }
public void Receive_10000_Messages() { _stream.Position = 0; for (var i = 0; i < 10000; i++) { _channelAdapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult(); } _stream.Position = 0; }
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()); } }
public void Deserialize_10000_Messages() { var channel = new BenchmarkMqttChannel(_serializedPacket); var reader = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(new MqttBufferWriter(4096, 65535)), null, new MqttNetEventLogger()); for (var i = 0; i < 10000; i++) { channel.Reset(); var header = reader.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult(); } }