BUFFER_OBJ_INTERFACE.cs 3.27 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;

namespace FLY.Thick.RemoteHistory
{
    public class BUFFER_OBJ_INTERFACE
    {
        #region Pack
        public class Pack_List<T> : IPack2
            where T : IPack2, new()
        {
            public bool isAdd;//不是add,就是changed
            public T[] list;

            #region IPack2 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.AddRange(BitConverter.GetBytes(isAdd));
                if (list == null)
                {
                    buf.AddRange(BitConverter.GetBytes(0));
                }
                else
                {
                    buf.AddRange(BitConverter.GetBytes(list.Count()));
                    for (int i = 0; i < list.Count(); i++)
                    {
                        buf.AddRange(list[i].ToBytes());
                    }
                }
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int cnt;
                return TryParse(value, 0, out cnt);
            }

            public bool TryParse(byte[] value, int index, out int cnt)
            {
                cnt = 1 + 4;
                if (value.Length < (index + cnt))
                    return false;
                int idx = index;

                isAdd = BitConverter.ToBoolean(value, idx);
                idx += 1;

                int len = BitConverter.ToInt32(value, idx);
                idx += 4;
                if (len <= 0)
                {
                    list = null;
                    return true;
                }

                list = new T[len];
                for (int i = 0; i < len; i++)
                {
                    int c;
                    list[i] = new T();
                    if (!list[i].TryParse(value, idx, out c))
                        return false;
                    idx += c;
                    cnt += c;
                }
                return true;
            }
            #endregion

        }
        public class Pack_Params
        {
            public int size;

            #region IPack实现
            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.AddRange(BitConverter.GetBytes(size));

                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int cnt = 4;
                if (value.Length < cnt)
                    return false;

                int idx = 0;
                size = BitConverter.ToInt32(value, idx);
                idx += 4;

                return true;
            }
            #endregion
        }
        #endregion

        #region GetValue
        public const UInt16 GET_PARAMS = 0;
        /// <summary>
        /// Pack_List
        /// </summary>
        public const UInt16 GET_LIST = 1;

        #endregion

        #region SetValue
        public const UInt16 SET_PARAMS = 0;
        #endregion
        #region PushMsg

        public const UInt16 PUSH_PARAMS = 0;
        /// <summary>
        /// Pack_List
        /// </summary>
        public const UInt16 PUSH_LIST = 1;
        #endregion
    }
}