Esempio n. 1
0
        public void setByte(byte[] contents)
        {
            _content = contents;
            int length = _content.Length;

            if (length < 126)
            {
                _extend = new byte[0];
                _header = new DataFrameHeader(true, false, false, false, 2, false, length);
            }
            else if (length < 65536)
            {
                _extend    = new byte[2];
                _header    = new DataFrameHeader(true, false, false, false, 2, false, 126);
                _extend[0] = (byte)(length / 256);
                _extend[1] = (byte)(length % 256);
            }
            else
            {
                _extend = new byte[8];
                _header = new DataFrameHeader(true, false, false, false, 2, false, 127);
                int left = length;
                int unit = 256;
                for (int i = 7; i > 1; i--)
                {
                    _extend[i] = (byte)(left % unit);
                    left       = left / unit;
                    if (left == 0)
                    {
                        break;
                    }
                }
            }
        }
        protected override byte[] packageData(byte[] alldata, Socket soc, SslStream Stream, byte[] tempDataList)
        {
            if (alldata.Length > 0)
            {
                DataFrameHeader dfh       = null;
                byte[]          tempbtyes = null;
                if (tempDataList.Length > 0)
                {
                    tempbtyes = new byte[tempDataList.Length + alldata.Length];
                    Array.Copy(tempDataList, 0, tempbtyes, 0, tempDataList.Length);
                    Array.Copy(alldata, 0, tempbtyes, tempDataList.Length, tempbtyes.Length);
                }
                else
                {
                    tempbtyes = new byte[alldata.Length];
                    Array.Copy(alldata, 0, tempbtyes, 0, tempbtyes.Length);
                }
                byte[]    masks    = new byte[4];
                int       lens     = 0;
                int       paylen   = 0;
                DataFrame df       = new DataFrame();
                byte[]    tempbtye = df.GetData(tempbtyes, ref masks, ref lens, ref paylen, ref dfh);
                if (dfh == null || dfh.OpCode != 2)
                {
                    tempDataList = tempbtyes;
                    return(new byte[0]);
                }
                else
                {
                    if (tempbtyes.Length > lens + paylen)
                    {
                        tempDataList = new byte[tempbtyes.Length - (lens + paylen)];
                        Buffer.BlockCopy(tempbtyes, lens + paylen, tempDataList, 0, tempDataList.Length);
                    }
                    else if (tempbtyes.Length == lens + paylen)
                    {
                        tempDataList = new byte[0];
                    }
                }

                alldata = tempbtye;
            }
            return(base.packageData(alldata, soc, Stream, tempDataList));
        }
Esempio n. 3
0
        public byte[] GetData(byte[] buffer, ref byte[] masks, ref int lens, ref int payload_len, ref DataFrameHeader dfh)
        {
            var isFinal      = (buffer[0] & 128) != 0;
            var reservedBits = (buffer[0] & 112);
            var frameType    = (FrameType)(buffer[0] & 15);
            var isMasked     = (buffer[1] & 128) != 0;
            var length       = (buffer[1] & 127);

            if (!isMasked ||
                !Enum.IsDefined(typeof(FrameType), frameType) ||
                reservedBits != 0 ||  //Must be zero per spec 5.2
                (frameType == FrameType.Continuation))
            {
                return(null);
            }
            lens = 0;
            //帧头
            _header     = new DataFrameHeader(buffer);
            dfh         = _header;
            payload_len = 0;
            if (_header.OpCode != 2)
            {
            }
            //扩展长度
            if (_header.Length == 126)
            {
                lens    = 8;
                _extend = new byte[2];
                Buffer.BlockCopy(buffer, 2, _extend, 0, 2);
            }
            else if (_header.Length == 127)
            {
                lens    = 14;
                _extend = new byte[8];
                Buffer.BlockCopy(buffer, 2, _extend, 0, 8);
            }
            //是否有掩码
            if (_header.HasMask)
            {
                _mask = new byte[4];
                Buffer.BlockCopy(buffer, _extend.Length + 2, _mask, 0, 4);
            }
            //消息体
            if (_extend.Length == 0)
            {
                payload_len = _content.Length;
                _content    = new byte[_header.Length];
                lens        = _extend.Length + _mask.Length + 2;
                try
                {
                    Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
                }
                catch (Exception e)
                { }
            }
            else if (_extend.Length == 2)
            {
                int contentLength = (int)_extend[0] * 256 + (int)_extend[1];
                payload_len = contentLength;
                lens        = _extend.Length + _mask.Length + 2;
                _content    = new byte[contentLength];
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, contentLength > 1024 * 100 ? 1024 * 100 : contentLength);
            }
            else
            {
                long len = 0;
                int  n   = 1;
                for (int i = 7; i >= 0; i--)
                {
                    len += (int)_extend[i] * n;
                    n   *= 256;
                }
                payload_len = (int)len;
                _content    = new byte[len];
                lens        = _extend.Length + _mask.Length + 2;
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
            }
            if (_header.HasMask)
            {
                _content = Mask(_content, _mask);
            }
            return(_content);
        }