예제 #1
0
        public static IEnumerable <byte> Frame(WSFrame f)
        {
            yield return((byte)f.OpCode);

            var l = f.Payload.Length;

            if (l < 126)
            {
                yield return((byte)l);
            }
            else if (l <= ushort.MaxValue)
            {
                yield return(0x7E);

                foreach (var b in Bits.To2Bytes((ushort)l))
                {
                    yield return(b);
                }
            }
            else
            {
                yield return(0x7F);

                foreach (var b in Bits.To8Bytes((ulong)l))
                {
                    yield return(b);
                }
            }

            foreach (var b in f.Payload)
            {
                yield return(b);
            }
        }
예제 #2
0
 public static int ExtendedLength(byte[] xln, int snd)
 {
     if (snd == 126)
     {
         return(Bits.From2Bytes(xln));
     }
     if (snd == 127)
     {
         return((int)Bits.From8Bytes(xln));
     }
     else
     {
         return(snd);
     }
 }
예제 #3
0
        public static WSFrame ClosePayload(WSFrame f)
        {
            if (f.Payload.Length == 0)
            {
                return(f);
            }
            if (f.Payload.Length == 1)
            {
                throw BadCloseLength;
            }

            var c = Bytes.From2Bytes(f.Payload.Take(2));

            if (!Ops.ValidCloseCodes.Contains(c))
            {
                throw BadCloseCode(c);
            }

            var valid = new Utf8Validation();
            var _     = valid.Add(f.Payload.Skip(2).ToArray()).Result;

            return(f);
        }