예제 #1
0
        public void DecodePayload(byte[] buffer, int offset, ActorFrameHeader frameHeader, out byte[] payload, out int payloadOffset, out int payloadCount)
        {
            payload       = buffer;
            payloadOffset = offset + frameHeader.Length;
            payloadCount  = frameHeader.PayloadLength;

            if (frameHeader.IsMasked)
            {
                payload = new byte[payloadCount];

                for (var i = 0; i < payloadCount; i++)
                {
                    payload[i] = (byte)(buffer[payloadOffset + i] ^ buffer[offset + frameHeader.MaskingKeyOffset + i % MaskingKeyLength]);
                }

                payloadOffset = 0;
                payloadCount  = payload.Length;
            }
        }
예제 #2
0
        private ActorFrameHeader DecodeFrameHeader(byte[] buffer, int offset, int count)
        {
            if (count < 2)
            {
                return(null);
            }

            // parse fixed header
            var header = new ActorFrameHeader()
            {
                IsRSV0        = ((buffer[offset + 0] & 0x80) == 0x80),
                IsRSV1        = ((buffer[offset + 0] & 0x40) == 0x40),
                IsRSV2        = ((buffer[offset + 0] & 0x20) == 0x20),
                IsRSV3        = ((buffer[offset + 0] & 0x10) == 0x10),
                OpCode        = (OpCode)(buffer[offset + 0] & 0x0f),
                IsMasked      = ((buffer[offset + 1] & 0x80) == 0x80),
                PayloadLength = (buffer[offset + 1] & 0x7f),
                Length        = 2,
            };

            // parse extended payload length
            if (header.PayloadLength >= 126)
            {
                if (header.PayloadLength == 126)
                {
                    header.Length += 2;
                }
                else
                {
                    header.Length += 8;
                }

                if (count < header.Length)
                {
                    return(null);
                }

                if (header.PayloadLength == 126)
                {
                    header.PayloadLength = buffer[offset + 2] * 256 + buffer[offset + 3];
                }
                else
                {
                    int totalLength = 0;
                    int level       = 1;

                    for (int i = 7; i >= 0; i--)
                    {
                        totalLength += buffer[offset + i + 2] * level;
                        level       *= 256;
                    }

                    header.PayloadLength = totalLength;
                }
            }

            // parse masking key
            if (header.IsMasked)
            {
                if (count < header.Length + MaskingKeyLength)
                {
                    return(null);
                }

                header.MaskingKeyOffset = header.Length;
                header.Length          += MaskingKeyLength;
            }

            return(header);
        }
예제 #3
0
 public bool TryDecodeFrameHeader(byte[] buffer, int offset, int count, out ActorFrameHeader frameHeader)
 {
     frameHeader = DecodeFrameHeader(buffer, offset, count);
     return(frameHeader != null);
 }