Beispiel #1
0
        public byte[] Serialize(MqttBasePacket packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            using (var stream = new MemoryStream())
                using (var writer = new MqttPacketWriter(stream))
                {
                    var header = new List <byte> {
                        SerializePacket(packet, writer)
                    };
                    var body = stream.ToArray();
                    MqttPacketWriter.BuildLengthHeader(body.Length, header);
                    var headerArray = header.ToArray();
                    var writeBuffer = new byte[header.Count + body.Length];
                    Buffer.BlockCopy(headerArray, 0, writeBuffer, 0, headerArray.Length);
                    Buffer.BlockCopy(body, 0, writeBuffer, headerArray.Length, body.Length);

                    return(writeBuffer);
                }
        }