Exemple #1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <remarks>主要用于解析接收数据</remarks>
 public DataFrame(byte[] buffer)
 {
     //格式化帧头
     _header = new DataFrameHeader(buffer);
     //填充扩展长度字节
     if (_header.Length == 126)
     {
         _extend = new byte[2];
         Buffer.BlockCopy(buffer, 2, _extend, 0, 2);
     }
     else if (_header.Length == 127)
     {
         _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)
     {
         _content = new byte[_header.Length];
         Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
     }
     else if (_extend.Length == 2)
     {
         _content = new byte[BitConverter.ToUInt16(_extend, 0)];
         Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
     }
     else
     {
         _content = new byte[BitConverter.ToUInt64(_extend, 0)];
         Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
     }
     //如果有掩码,则需要还原原始数据
     if (_header.HasMask)
     {
         _content = Mask(_content, _mask);
     }
 }
        byte[] readBytes()
        {
            byte[] data = mClient.ReceiveDatas(2);

            DataFrameHeader _header = new DataFrameHeader(data);

            byte[] _extend    = new byte[0];
            int    maskLength = _header.HasMask ? 4 : 0;

            if (_header.Length == 126)
            {
                _extend = mClient.ReceiveDatas(2);
            }
            else if (_header.Length == 127)
            {
                _extend = mClient.ReceiveDatas(8);
            }
            int contentLength;

            //消息体
            if (_extend.Length == 0)
            {
                contentLength = _header.Length + maskLength;
            }
            else if (_extend.Length == 2)
            {
                _extend       = new byte[] { _extend[1], _extend[0] };//低位 高位对调一下
                contentLength = BitConverter.ToUInt16(_extend, 0) + maskLength;
            }
            else
            {
                _extend       = new byte[] { _extend[7], _extend[6], _extend[5], _extend[4], _extend[3], _extend[2], _extend[1], _extend[0] };
                contentLength = (int)BitConverter.ToUInt64(_extend, 0) + maskLength;
            }
            int contentOffset = data.Length + _extend.Length;

            byte[] bufferData = new byte[data.Length + _extend.Length + contentLength];
            Array.Copy(data, bufferData, data.Length);
            Array.Copy(_extend, 0, bufferData, data.Length, _extend.Length);

            mClient.ReceiveDatas(bufferData, contentOffset, contentLength);
            return(bufferData);
        }
Exemple #3
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <remarks>主要用于发送封装数据</remarks>
        public DataFrame(string content)
        {
            _content = Encoding.UTF8.GetBytes(content);
            int length = _content.Length;

            if (length < 126)
            {
                _extend = new byte[0];
                _header = new DataFrameHeader(true, false, false, false, OpCode.Text, false, length);
            }
            else if (length < 65536)
            {
                _extend    = new byte[2];
                _header    = new DataFrameHeader(true, false, false, false, OpCode.Text, false, 126);
                _extend[0] = (byte)(length / 256);
                _extend[1] = (byte)(length % 256);
            }
            else
            {
                _extend = new byte[8];
                _header = new DataFrameHeader(true, false, false, false, OpCode.Text, 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;
                    }
                }
            }
        }
Exemple #4
0
        public void InitWithByteContent(byte[] content)
        {
            _content = content;
            int length = _content.Length;

            if (length < 126)
            {
                _extend = new byte[0];
                _header = new DataFrameHeader(true, false, false, false, OpCode.Binary, false, length);
            }
            else if (length < 65536)
            {
                _extend    = new byte[2];
                _header    = new DataFrameHeader(true, false, false, false, OpCode.Binary, false, 126);
                _extend[0] = (byte)(length / 256);
                _extend[1] = (byte)(length % 256);
            }
            else
            {
                _extend = new byte[8];
                _header = new DataFrameHeader(true, false, false, false, OpCode.Binary, 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;
                    }
                }
            }
        }