Beispiel #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);
            }
        }
Beispiel #2
0
            public IValidation <WSFrame> Add(WSFrame fragment)
            {
                ValidateContinuationState(OpCode, fragment.OpCode);

                OpCode |= fragment.OpCode;
                _validate.Add(fragment.Payload);

                return(this);
            }
Beispiel #3
0
        public static WSFrame Valid(WSFrame f)
        {
            if (!Ops.AllPossible.Contains(f.OpCode))
            {
                throw OutOfBounds(f.OpCode);
            }
            if (Ops.ControlFrames.Contains(f.OpCode) && f.Payload.Length > 125)
            {
                throw BadLength;
            }

            return(f);
        }
Beispiel #4
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);
        }
Beispiel #5
0
 static bool IsNotClose(WSFrame f) => !IsClose(f);
Beispiel #6
0
 static bool IsClose(WSFrame f) => f.OpCode == Ops.Close;
 static bool IsControlcode(WSFrame f) => ((byte)f.OpCode & (byte)0b0000_1000) != 0;