BOLTCREATEINFO_OBJ_INTERFACE.cs 4.06 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 131 132 133 134 135 136 137 138
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;

namespace FLY.Thick.Base.OBJ_INTERFACE
{
    public class BOLTCREATEINFO_OBJ_INTERFACE
    {
        public const UInt32 ID = OBJ_INTERFACE_ID.BOLTCREATEINFO_ID;
        #region Pack
        public class Pack_CustomBolt:IPack 
        {
            public int no;
            public int position;

            /// <summary>
            /// 转换为8个bytes
            /// </summary>
            /// <returns></returns>
            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.AddRange(BitConverter.GetBytes(no));
                buf.AddRange(BitConverter.GetBytes(position));
                
                return buf.ToArray();
            }
            /// <summary>
            /// 由8个bytes 转换为 CustomBolt
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public bool TryParse(byte[] value)
            {
                return TryParse(value, 0);
            }
            public bool TryParse(byte[] value, int index) 
            {
                if ((value.Length-index) < 8)
                    return false;
                int idx = index;
                no = BitConverter.ToInt32(value, idx);
                idx += 4;
                position = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }
        public class Pack_Params : IPack
        {
            public int poslength;
            public int boltwidth;
            public int boltNo1st;
            public int boltCnt;
            public Pack_CustomBolt[] custom;

            #region IPack 成员

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

                buf.AddRange(BitConverter.GetBytes(poslength));
                buf.AddRange(BitConverter.GetBytes(boltwidth));
                buf.AddRange(BitConverter.GetBytes(boltNo1st));
                buf.AddRange(BitConverter.GetBytes(boltCnt));
                if (custom != null)
                {
                    buf.AddRange(BitConverter.GetBytes(custom.Length));
                    for (int i = 0; i < custom.Length; i++)
                        buf.AddRange(custom[i].ToBytes());
                }
                else 
                {
                    int len = 0;
                    buf.AddRange(BitConverter.GetBytes(len));
                }
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < 20)
                    return false;
                int idx = 0;
                poslength = BitConverter.ToInt32(value, idx);
                idx += 4;
                boltwidth = BitConverter.ToInt32(value, idx);
                idx += 4;
                boltNo1st = BitConverter.ToInt32(value, idx);
                idx += 4;

                boltCnt = BitConverter.ToInt32(value, idx);
                idx += 4;

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

                if (value.Length < (20 + len * 8))
                    return false;

                custom = new Pack_CustomBolt[len];
                for (int i = 0; i < len; i++)
                { 
                    custom[i]=new Pack_CustomBolt();
                    custom[i].TryParse(value, idx);
                    idx+=8;
                }
                return true;
            }
            
            #endregion
        }
        #endregion

        #region GetValue
        /// <summary>
        /// Pack_Params
        /// </summary>
        public const UInt16 GET_PARAMS = 0;
        #endregion
        #region SetValue
        /// <summary>
        /// Pack_Params
        /// </summary>
        public const UInt16 SET_PARAMS = 0;
        #endregion
        #region PushMsg
        /// <summary>
        /// Pack_Params
        /// </summary>
        public const UInt16 PUSH_PARAMS = 0;
        #endregion
    }
}