Exemplo n.º 1
0
        public WebSocketFrame Read(Stream stream, Socket socket)
        {
            byte byte1;

            try
            {
                byte1 = (byte)stream.ReadByte();
            }
            catch (IOException)
            {
                if (socket.Connected)
                {
                    throw;
                }
                else
                {
                    return(null);
                }
            }

            // process first byte
            byte            finBitFlag  = 0x80;
            byte            opCodeFlag  = 0x0F;
            bool            isFinBitSet = (byte1 & finBitFlag) == finBitFlag;
            WebSocketOpCode opCode      = (WebSocketOpCode)(byte1 & opCodeFlag);

            // read and process second byte
            byte byte2        = (byte)stream.ReadByte();
            byte maskFlag     = 0x80;
            bool isMaskBitSet = (byte2 & maskFlag) == maskFlag;
            uint len          = ReadLength(byte2, stream);

            byte[] decodedPayload;

            // use the masking key to decode the data if needed
            if (isMaskBitSet)
            {
                const int maskKeyLen     = 4;
                byte[]    maskKey        = BinaryReaderWriter.ReadExactly(maskKeyLen, stream);
                byte[]    encodedPayload = BinaryReaderWriter.ReadExactly((int)len, stream);
                decodedPayload = new byte[len];

                // apply the mask key
                for (int i = 0; i < encodedPayload.Length; i++)
                {
                    decodedPayload[i] = (Byte)(encodedPayload[i] ^ maskKey[i % maskKeyLen]);
                }
            }
            else
            {
                decodedPayload = BinaryReaderWriter.ReadExactly((int)len, stream);
            }

            WebSocketFrame frame = new WebSocketFrame(isFinBitSet, opCode, decodedPayload, true);

            return(frame);
        }
Exemplo n.º 2
0
        public static long ReadLongExactly(Stream stream, bool isLittleEndian)
        {
            byte[] lenBuffer = BinaryReaderWriter.ReadExactly(8, stream);

            if (!isLittleEndian)
            {
                Array.Reverse(lenBuffer); // big endian
            }

            return(BitConverter.ToInt64(lenBuffer, 0));
        }
Exemplo n.º 3
0
        public static ushort ReadUShortExactly(Stream stream, bool isLittleEndian)
        {
            byte[] lenBuffer = BinaryReaderWriter.ReadExactly(2, stream);

            if (!isLittleEndian)
            {
                Array.Reverse(lenBuffer); // big endian
            }

            return(BitConverter.ToUInt16(lenBuffer, 0));
        }
        public WebSocketFrame Read(Stream stream, Socket socket)
        {
            byte byte1;

            try
            {
                byte1 = (byte)stream.ReadByte();
            }
            catch (IOException)
            {
                if (socket.Connected)
                {
                    throw;
                }
                else
                {
                    return(null);
                }
            }

            // process first byte
            byte            finBitFlag  = 0x80;
            byte            opCodeFlag  = 0x0F;
            bool            isFinBitSet = (byte1 & finBitFlag) == finBitFlag;
            WebSocketOpCode opCode      = (WebSocketOpCode)(byte1 & opCodeFlag);

            // read and process second byte
            byte byte2        = (byte)stream.ReadByte();
            byte maskFlag     = 0x80;
            bool isMaskBitSet = (byte2 & maskFlag) == maskFlag;
            uint len          = ReadLength(byte2, stream);

            byte[] payload;

            // use the masking key to decode the data if needed
            if (isMaskBitSet)
            {
                byte[] maskKey = BinaryReaderWriter.ReadExactly(WebSocketFrameCommon.MaskKeyLength, stream);
                payload = BinaryReaderWriter.ReadExactly((int)len, stream);

                // apply the mask key to the payload (which will be mutated)
                WebSocketFrameCommon.ToggleMask(maskKey, payload);
            }
            else
            {
                payload = BinaryReaderWriter.ReadExactly((int)len, stream);
            }

            WebSocketFrame frame = new WebSocketFrame(isFinBitSet, opCode, payload, true);

            return(frame);
        }