public static double UnPackDouble(ref byte[] bytes)
    {
        double outValue = BitConverter.ToDouble(bytes, 0);

        CommonToolUtility.ByteSub(ref bytes, sizeof(double));
        return(outValue);
    }
    public static char UnPackChar(ref byte[] bytes)
    {
        char outValue = BitConverter.ToChar(bytes, 0);

        CommonToolUtility.ByteSub(ref bytes, sizeof(char));
        return(outValue);
    }
    public static float UnPackFloat(ref byte[] bytes)
    {
        float outValue = BitConverter.ToSingle(bytes, 0);

        CommonToolUtility.ByteSub(ref bytes, sizeof(float));
        return(outValue);
    }
    public static bool UnPackBool(ref byte[] bytes)
    {
        bool outValue = BitConverter.ToBoolean(bytes, 0);

        CommonToolUtility.ByteSub(ref bytes, sizeof(bool));
        return(outValue);
    }
    //特殊封包操作
    //1.类结构
    //public static void PacketClass(ref byte[] bytes, object classObject)
    //{
    //    var fieldInfos = classObject.GetType().GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
    //    foreach (var item in fieldInfos)
    //    {
    //        if(item.FieldType == typeof(int))
    //        {

    //        }
    //    }
    //}

    #endregion


    #region 解包

    public static int UnPackInt(ref byte[] bytes)
    {
        int outValue = BitConverter.ToInt32(bytes, 0);

        CommonToolUtility.ByteSub(ref bytes, sizeof(int));
        return(outValue);
    }
예제 #6
0
    private void HandleTempList()
    {
        //现有缓存数据长度
        int curLength = tempList.Count - 4;
        //总数据长度 = 保存于缓存[0,3]字节
        int sumLength = BitConverter.ToInt32(tempList.ToArray(), 0);
        //所需数据长度
        int needLength = sumLength - curLength;
        //可取长度
        int canGetLength = Math.Min(this.startIndex, needLength);//其实这里可能肯定就是startIndex,不然就是协议异常了..

        for (int i = 0; i < canGetLength; i++)
        {
            tempList.Add(data[i]);
        }
        //去除已取出部分字节
        CommonToolUtility.ByteSub(ref data, canGetLength);
        this.startIndex -= canGetLength;
        curLength        = tempList.Count - 4;
        //获取到完整数据开始解析(正常情况下,肯定是完整了)
        if (sumLength - curLength <= 0)
        {
            ProtocolEventType type     = ProtocolEventType.Null;
            byte[]            bytesArr = tempList.ToArray();
            object            o        = ProtocolManager.Instance.StartUnPack(ref bytesArr, ref type);
            if (type != ProtocolEventType.Null)
            {
                EventSystem.Instance.Dispatch(type, (ProtocolData)o);
            }
            //清空缓存
            tempList.Clear();
        }
    }
    public static string UnPackString(ref byte[] bytes, int count)//count是[字节]个数
    {
        string str = Encoding.Unicode.GetString(bytes, 0, count);

        //str = str.Replace("\0", "");// "\0"是byte缺省值,要替换为""
        CommonToolUtility.ByteSub(ref bytes, count);
        return(str);
    }