Esempio n. 1
0
 public void Write(WebSocketOpCode opCode, byte[] payload, bool isLastFrame)
 {
     using (var memoryStream = new MemoryStream()) {
         var finBitSetAsByte = isLastFrame ? (byte)0x80 : (byte)0x00;
         var byte1           = (byte)(finBitSetAsByte | (byte)opCode);
         memoryStream.WriteByte(byte1);
         if (payload.Length < 126)
         {
             var byte2 = (byte)payload.Length;
             memoryStream.WriteByte(byte2);
         }
         else if (payload.Length <= ushort.MaxValue)
         {
             byte byte2 = 126;
             memoryStream.WriteByte(byte2);
             BinaryReaderWriter.WriteUShort((ushort)payload.Length, memoryStream);
         }
         else
         {
             byte byte2 = 127;
             memoryStream.WriteByte(byte2);
             BinaryReaderWriter.WriteULong((ulong)payload.Length, memoryStream);
         }
         memoryStream.Write(payload, 0, payload.Length);
         var buffer = memoryStream.ToArray();
         _stream.Write(buffer, 0, buffer.Length);
     }
 }
Esempio n. 2
0
        private static uint ReadLength(byte byte2, Stream stream)
        {
            const byte payloadLenFlag = 0x7F;
            var        lenght         = (uint)(byte2 & payloadLenFlag);
            const uint maxLen         = 2147483648;

            if (lenght > maxLen)
            {
                throw new ArgumentOutOfRangeException($"Payload length out of range. Min 0 max 2GB. Actual {lenght:#,##0} bytes.");
            }
            if (lenght == 126)
            {
                var lenBuffer = BinaryReaderWriter.ReadExactly(2, stream);
                lenght = BitConverter.ToUInt16(lenBuffer, 0);
            }
            else if (lenght == 127)
            {
                var lenBuffer = BinaryReaderWriter.ReadExactly(8, stream);
                lenght = (uint)BitConverter.ToUInt64(lenBuffer, 0);
            }
            return(lenght);
        }
Esempio n. 3
0
        public WebSocketFrame Read(NetworkStream stream)
        {
            var byte1 = (byte)stream.ReadByte();

            if (!stream.DataAvailable && byte1 == 0xFF)
            {
                return(new WebSocketFrame(true, WebSocketOpCode.ConnectionClose, new byte[0], false));
            }
            const byte finBitFlag   = 0x80;
            const byte opCodeFlag   = 0x0F;
            var        isFinBitSet  = (byte1 & finBitFlag) == finBitFlag;
            var        opCode       = (WebSocketOpCode)(byte1 & opCodeFlag);
            var        byte2        = (byte)stream.ReadByte();
            const byte maskFlag     = 0x80;
            var        isMaskBitSet = (byte2 & maskFlag) == maskFlag;
            var        len          = ReadLength(byte2, stream);

            byte[] decodedPayload;
            if (isMaskBitSet)
            {
                const int maskKeyLen     = 4;
                var       maskKey        = BinaryReaderWriter.ReadExactly(maskKeyLen, stream);
                var       encodedPayload = BinaryReaderWriter.ReadExactly((int)len, stream);
                decodedPayload = new byte[len];
                for (var i = 0; i < encodedPayload.Length; i++)
                {
                    decodedPayload[i] = (byte)(encodedPayload[i] ^ maskKey[i % maskKeyLen]);
                }
            }
            else
            {
                decodedPayload = BinaryReaderWriter.ReadExactly((int)len, stream);
            }

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

            return(frame);
        }