Exemple #1
0
        public byte[] ToBytes()
        {
            using (var ms = new MemoryStream())
            {
                ms.WriteByte((byte)((byte)Opcode | (Finish ? 0x80 : 0x00)));

                if (RawContent.Length < 126)
                {
                    ms.WriteByte((byte)RawContent.Length);
                }
                else if (RawContent.Length < ushort.MaxValue)
                {
                    ms.WriteByte(126);
                    var bytes = BitConverter.GetBytes((ushort)RawContent.Length);
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(bytes);
                    }
                    ms.Write(bytes, 0, bytes.Length);
                }
                else
                {
                    ms.WriteByte(127);
                    var bytes = BitConverter.GetBytes((ulong)RawContent.Length);
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(bytes);
                    }
                    ms.Write(bytes, 0, bytes.Length);
                }

                ms.Write(RawContent, 0, RawContent.Length);

                if (Additional != null)
                {
                    var additionalBytes = Additional.ToBytes();
                    ms.Write(additionalBytes, 0, additionalBytes.Length);
                }

                return(ms.ToArray());
            }
        }