Ejemplo n.º 1
0
        /// <summary>
        /// 获取一条可用数据,返回值标记是否有数据
        /// </summary>
        /// <param name="tmpSocketData"></param>
        /// <returns></returns>
        public bool GetData(out TmpSocketData tmpSocketData)
        {
            tmpSocketData = new TmpSocketData();
            if (_buffLength <= 0)
            {
                UpdateDataLength();
            }

            if (_buffLength > 0 && _curBuffPosition >= _buffLength)
            {
                tmpSocketData.buffLength    = _buffLength;
                tmpSocketData.dataLength    = _dataLength;
                tmpSocketData.protocallType = _protocalType;
                tmpSocketData.data          = new byte[_dataLength];
                Array.Copy(_buff, Constants.HEAD_LEN, tmpSocketData.data, 0, _dataLength);
                _curBuffPosition -= _buffLength;

                // 粘包,把剩下的字节取出给_buff
                byte[] _tmpBuff = new byte[_curBuffPosition < _minBuffLen ? _minBuffLen : _curBuffPosition];
                Array.Copy(_buff, _buffLength, _tmpBuff, 0, _curBuffPosition);
                _buff = _tmpBuff;

                _buffLength   = 0;
                _dataLength   = 0;
                _protocalType = 0;
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 数据转网络结构
        /// TODO 这个转换没什么用,可以去掉
        /// </summary>
        /// <param name="_protocalType"></param>
        /// <param name="_data"></param>
        /// <returns></returns>
        private TmpSocketData BytesToSocketData(uint _protocalType, byte[] _data)
        {
            TmpSocketData tmpSocketData = new TmpSocketData();

            tmpSocketData.buffLength    = (uint)(Constants.HEAD_LEN + _data.Length);
            tmpSocketData.dataLength    = (uint)_data.Length;
            tmpSocketData.protocallType = _protocalType;
            tmpSocketData.data          = _data;
            return(tmpSocketData);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 网络结构转数据
        /// </summary>
        /// <param name="tmpSocketData"></param>
        /// <returns></returns>
        private byte[] SocketDataToBytes(TmpSocketData tmpSocketData)
        {
            byte[] _tmpBuff             = new byte[tmpSocketData.buffLength];
            byte[] _dataLength          = BitConverter.GetBytes(tmpSocketData.buffLength - Constants.HEAD_LEN);
            byte[] _protocallTypeLength = BitConverter.GetBytes((uint)tmpSocketData.protocallType);

            // 消息体长度
            Array.Copy(_dataLength, 0, _tmpBuff, 0, Constants.HEAD_DATA_LEN);
            // 协议类型
            Array.Copy(_protocallTypeLength, 0, _tmpBuff, Constants.HEAD_DATA_LEN, Constants.HEAD_TYPE_LEN);
            // 协议数据
            Array.Copy(tmpSocketData.data, 0, _tmpBuff, Constants.HEAD_LEN, tmpSocketData.dataLength);

            return(_tmpBuff);
        }