示例#1
0
        public WsFrame(Fin fin, Opcode opcode, Mask mask, PayloadData payload, bool compressed)
        {
            this.Fin    = fin;
            this.Rsv1   = ((!WsFrame.isData(opcode) || !compressed) ? Rsv.Off : Rsv.On);
            this.Rsv2   = Rsv.Off;
            this.Rsv3   = Rsv.Off;
            this.Opcode = opcode;
            this.Mask   = mask;
            ulong length = payload.Length;
            byte  b      = (length >= 126UL) ? ((length >= 65536UL) ? 127 : 126) : ((byte)length);

            this.PayloadLen    = b;
            this.ExtPayloadLen = ((b >= 126) ? ((b != 126) ? length.ToByteArrayInternally(ByteOrder.Big) : ((ushort)length).ToByteArrayInternally(ByteOrder.Big)) : new byte[0]);
            bool flag = mask == Mask.Mask;

            byte[] maskingKey = (!flag) ? new byte[0] : WsFrame.createMaskingKey();
            this.MaskingKey = maskingKey;
            if (flag)
            {
                payload.Mask(maskingKey);
            }
            this.PayloadData = payload;
        }
示例#2
0
        private static WsFrame parse(byte[] header, Stream stream, bool unmask)
        {
            Fin    fin    = ((header[0] & 128) != 128) ? Fin.More : Fin.Final;
            Rsv    rsv    = ((header[0] & 64) != 64) ? Rsv.Off : Rsv.On;
            Rsv    rsv2   = ((header[0] & 32) != 32) ? Rsv.Off : Rsv.On;
            Rsv    rsv3   = ((header[0] & 16) != 16) ? Rsv.Off : Rsv.On;
            Opcode opcode = (Opcode)(header[0] & 15);
            Mask   mask   = ((header[1] & 128) != 128) ? Mask.Unmask : Mask.Mask;
            byte   b      = header[1] & 127;
            string text   = (!WsFrame.isControl(opcode) || fin != Fin.More) ? ((WsFrame.isData(opcode) || rsv != Rsv.On) ? null : "A non data frame is compressed.") : "A control frame is fragmented.";

            if (text != null)
            {
                throw new WebSocketException(CloseStatusCode.IncorrectData, text);
            }
            if (WsFrame.isControl(opcode) && b > 125)
            {
                throw new WebSocketException(CloseStatusCode.InconsistentData, "The payload data length of a control frame is greater than 125 bytes.");
            }
            WsFrame wsFrame = new WsFrame
            {
                Fin        = fin,
                Rsv1       = rsv,
                Rsv2       = rsv2,
                Rsv3       = rsv3,
                Opcode     = opcode,
                Mask       = mask,
                PayloadLen = b
            };
            int num = (b >= 126) ? ((b != 126) ? 8 : 2) : 0;

            byte[] array = (num <= 0) ? new byte[0] : stream.ReadBytes(num);
            if (num > 0 && array.Length != num)
            {
                throw new WebSocketException("The 'Extended Payload Length' of a frame cannot be read from the data source.");
            }
            wsFrame.ExtPayloadLen = array;
            bool flag = mask == Mask.Mask;

            byte[] array2 = (!flag) ? new byte[0] : stream.ReadBytes(4);
            if (flag && array2.Length != 4)
            {
                throw new WebSocketException("The 'Masking Key' of a frame cannot be read from the data source.");
            }
            wsFrame.MaskingKey = array2;
            ulong num2 = (b >= 126) ? ((b != 126) ? array.ToUInt64(ByteOrder.Big) : ((ulong)array.ToUInt16(ByteOrder.Big))) : ((ulong)b);

            byte[] array3;
            if (num2 > 0UL)
            {
                if (b > 126 && num2 > 9223372036854775807UL)
                {
                    throw new WebSocketException(CloseStatusCode.TooBig, "The 'Payload Data' length is greater than the allowable length.");
                }
                array3 = ((b <= 126) ? stream.ReadBytes((int)num2) : stream.ReadBytes((long)num2, 1024));
                if (array3.LongLength != (long)num2)
                {
                    throw new WebSocketException("The 'Payload Data' of a frame cannot be read from the data source.");
                }
            }
            else
            {
                array3 = new byte[0];
            }
            PayloadData payloadData = new PayloadData(array3, flag);

            if (flag && unmask)
            {
                payloadData.Mask(array2);
                wsFrame.Mask       = Mask.Unmask;
                wsFrame.MaskingKey = new byte[0];
            }
            wsFrame.PayloadData = payloadData;
            return(wsFrame);
        }