Beispiel #1
0
    /// <summary>
    /// 获取一条可用数据,返回值标记是否有数据
    /// </summary>
    /// <param name="_tmpSocketData"></param>
    /// <returns></returns>
    public bool GetData(out sSocketData _tmpSocketData)
    {
        _tmpSocketData = new sSocketData();

        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;
            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);
    }
Beispiel #2
0
    /// <summary>
    /// 获取一条可用数据,返回值标记是否有数据
    /// </summary>
    /// <param name="_tmpSocketData"></param>
    /// <returns></returns>
    public bool GetData(out sSocketData _tmpSocketData)
    {
        _tmpSocketData = new sSocketData();

        if (_msgLength == 0)
        {
            UpdateMsgLengthAndType();
            if (_msgLength == 0)
            {
                return(false);
            }
        }

        if (_curBuffPosition < _msgLength + Constants.HEAD_MSG_LEN)
        {
            return(false);
        }

        int dataLength = (int)_msgLength - Constants.HEAD_TYPE_LEN;

        _tmpSocketData._protocallType = _protocalType;
        _tmpSocketData._data          = new byte[dataLength];
        Array.Copy(_buff, Constants.HEAD_LEN, _tmpSocketData._data, 0, dataLength);
        int doneLength = (int)_msgLength + Constants.HEAD_MSG_LEN;

        _curBuffPosition -= doneLength;
        byte[] _tmpBuff = new byte[_curBuffPosition < _minBuffLen ? _minBuffLen : _curBuffPosition];
        Array.Copy(_buff, doneLength, _tmpBuff, 0, _curBuffPosition);
        _buff = _tmpBuff;

        _msgLength    = 0;
        _protocalType = 0;
        return(true);
    }  // GetData()
Beispiel #3
0
    /// <summary>
    /// 数据转网络结构
    /// </summary>
    /// <param name="_protocalType"></param>
    /// <param name="_data"></param>
    /// <returns></returns>
    private sSocketData BytesToSocketData(UInt16 _protocalType, byte[] _data)
    {
        sSocketData tmpSocketData = new sSocketData();

        tmpSocketData._protocallType = _protocalType;
        tmpSocketData._data          = _data;
        return(tmpSocketData);
    }
Beispiel #4
0
    /// <summary>
    /// 数据转网络结构
    /// </summary>
    /// <param name="_protocalType"></param>
    /// <param name="_data"></param>
    /// <returns></returns>
    private sSocketData BytesToSocketData(eProtocalCommand _protocalType, byte[] _data)
    {
        sSocketData tmpSocketData = new sSocketData();

        tmpSocketData._buffLength    = Constants.HEAD_LEN + _data.Length;
        tmpSocketData._dataLength    = _data.Length;
        tmpSocketData._protocallType = _protocalType;
        tmpSocketData._data          = _data;
        return(tmpSocketData);
    }
Beispiel #5
0
    /// <summary>
    /// 网络结构转数据
    /// </summary>
    /// <param name="tmpSocketData"></param>
    /// <returns></returns>
    private byte[] SocketDataToBytes(sSocketData tmpSocketData)
    {
        byte[] _tmpBuff       = new byte[tmpSocketData._buffLength];
        byte[] _tmpBuffLength = BitConverter.GetBytes(tmpSocketData._buffLength);
        byte[] _tmpDataLenght = BitConverter.GetBytes((UInt16)tmpSocketData._protocallType);

        Array.Copy(_tmpBuffLength, 0, _tmpBuff, 0, Constants.HEAD_DATA_LEN);                         //缓存总长度
        Array.Copy(_tmpDataLenght, 0, _tmpBuff, Constants.HEAD_DATA_LEN, Constants.HEAD_TYPE_LEN);   //协议类型
        Array.Copy(tmpSocketData._data, 0, _tmpBuff, Constants.HEAD_LEN, tmpSocketData._dataLength); //协议数据

        return(_tmpBuff);
    }
Beispiel #6
0
    /// <summary>
    /// 网络结构转数据
    /// </summary>
    /// <param name="tmpSocketData"></param>
    /// <returns></returns>
    private byte[] SocketDataToBytes(sSocketData tmpSocketData)
    {
        int dataLength = tmpSocketData._data.Length;
        int buffLength = dataLength + Constants.HEAD_LEN;

        byte[] _tmpBuff  = new byte[buffLength];
        int    msgLength = dataLength + Constants.HEAD_TYPE_LEN;

        byte[] _tmpMsgLength = BitConverter.GetBytes(msgLength);
        byte[] _tmpType      = BitConverter.GetBytes((UInt16)tmpSocketData._protocallType);

        Array.Copy(_tmpMsgLength, 0, _tmpBuff, 0, Constants.HEAD_MSG_LEN);                  //缓存总长度
        Array.Copy(_tmpType, 0, _tmpBuff, Constants.HEAD_MSG_LEN, Constants.HEAD_TYPE_LEN); //协议类型
        Array.Copy(tmpSocketData._data, 0, _tmpBuff, Constants.HEAD_LEN, dataLength);       //协议数据

        return(_tmpBuff);
    }