예제 #1
0
파일: DataFrame.cs 프로젝트: puxu1989/PXLib
 /// <summary>
 /// 清除缓存区
 /// </summary>
 public void Clear()
 {
     this._header = null;
     this._extend = null;
     Array.Clear(this._extend, 0, _extend.Length);
     Array.Clear(this._mask, 0, _extend.Length);
     Array.Clear(this._content, 0, _extend.Length);
 }
예제 #2
0
파일: DataFrame.cs 프로젝트: puxu1989/PXLib
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <remarks>主要用于解析接收数据</remarks>
 public DataFrame(byte[] buffer, ref int offset)
 {
     //格式化帧头
     _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);
     }
     //消息体 <126
     if (_extend.Length == 0)
     {
         _content = new byte[_header.Length];
         Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
         offset = 6;
     }
     else if (_extend.Length == 2)//<=65535
     {
         ulong PlayLenth = (UInt16)(buffer[2] << 8 | buffer[3]);
         _content = new byte[PlayLenth];
         Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
         offset = 8;
     }
     else
     {
         // UInt64 len = BitConverter.ToUInt64(buffer, 2);
         byte[] uInt64Bytes = new byte[8];
         for (int i = 0; i < 8; i++)
         {
             uInt64Bytes[i] = buffer[9 - i];
         }
         UInt64 len = BitConverter.ToUInt64(uInt64Bytes, 0);
         _content = new byte[len];
         Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
         offset = 14;
     }
     //如果有掩码,则需要还原原始数据
     if (_header.HasMask)
     {
         _content = Mask(_content, _mask);
     }
 }
예제 #3
0
파일: DataFrame.cs 프로젝트: puxu1989/PXLib
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <remarks>主要用于发送封装数据</remarks>
        public DataFrame(byte[] content, bool isText = false)
        {
            _content = content;
            sbyte opcode = 0;

            if (isText)
            {
                opcode = OpCode.Text;
            }
            else
            {
                opcode = OpCode.Binary;
            }
            int length = _content.Length;

            if (length < 126)
            {
                _extend = new byte[0];
                _header = new DataFrameHeader(true, false, false, false, opcode, false, length);
            }
            else if (length < 65536)
            {
                _extend    = new byte[2];
                _header    = new DataFrameHeader(true, false, false, false, opcode, 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, 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;
                    }
                }
            }
        }