Beispiel #1
0
        public void Write(WsOpCode opCode, byte[] payload, bool isLastFrame)
        {
            //best to write everything to a memory stream before we push it onto the wire
            //not really necessary but good advice
            using (MemoryStream memoryStream = new MemoryStream()) {
                byte finBitSetAsByte = isLastFrame ? (byte)0x80 : (byte)0x00;
                byte byte1           = (byte)(finBitSetAsByte | (byte)opCode);
                memoryStream.WriteByte(byte1);

                //Don't set the mask flag.  No need to mask data from server to client
                //Depending on the size of the length we want to write it as a byte, ushort, or ulong.
                if (payload.Length < 126)
                {
                    memoryStream.WriteByte((byte)payload.Length);
                }
                else if (payload.Length <= ushort.MaxValue)
                {
                    memoryStream.WriteByte(126);
                    BinaryReaderWriter.WriteUShort((ushort)payload.Length, memoryStream, false);
                }
                else
                {
                    memoryStream.WriteByte(127);
                    BinaryReaderWriter.WriteULong((ulong)payload.Length, memoryStream, false);
                }

                memoryStream.Write(payload, 0, payload.Length);
                byte[] buffer = memoryStream.ToArray();
                _stream.Write(buffer, 0, buffer.Length);
            }
        }
        public WsFrame Read(Stream stream, Socket socket)
        {
            byte byte1;

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

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

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

            byte[] decodePayload;

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

                //apply the mask key
                for (int i = 0; i < encodePayload.Length; i++)
                {
                    decodePayload[i] = (byte)(encodePayload[i] ^ maskKey[i % maskKeyLen]);
                }
            }
            else
            {
                decodePayload = BinaryReaderWriter.ReadExactly((int)len, stream);
            }
            WsFrame frame = new WsFrame(isFinBitSet, opCode, decodePayload, true);

            return(frame);
        }
        private static uint ReadLength(byte byte2, Stream stream)
        {
            byte payloadLenFlag = 0x7F;
            uint len            = (uint)(byte2 & payloadLenFlag);

            //read a short length or a long length depending on the value of len
            if (len == 126)
            {
                len = BinaryReaderWriter.ReadUShortExactly(stream, false);
            }
            else if (len == 127)
            {
                len = (uint)BinaryReaderWriter.ReadULongExactly(stream, false);
                const uint maxLen = 2147483648; //2GB
                //protect ourselves against bad data
                if (len > maxLen)
                {
                    throw new ArgumentOutOfRangeException(string.Format("Payload length out of range. Min 0 Max 2GB. Actual {0:#,##0} bytes.", len));
                }
            }
            return(len);
        }