コード例 #1
0
        public byte[] GetBytes()
        {
            List <byte> data = new List <byte>();

            data.Add((byte)Type);

            data.AddRange(BitConverter.GetBytes(SequenceNumber));

            var epBytes = Encoding.UTF8.GetBytes(EndPoint);

            if (epBytes.Length > byte.MaxValue)
            {
                throw new FormatException("EndPoint string is too long.");
            }

            data.Add((byte)epBytes.Length);
            data.AddRange(epBytes);

            var payload = GetPayload();

            if (payload.Length > ushort.MaxValue)
            {
                throw new FormatException("Payload size is too big");
            }

            data.AddRange(NumberConverter.GetBytes((ushort)payload.Length));
            data.AddRange(payload);

            byte[] bytes = new byte[data.Count + 4];
            data.CopyTo(bytes);
            Crc32Algorithm.ComputeAndWriteToEnd(bytes);

            return(bytes);
        }