PROFILE_OBJ_INTERFACE.cs 4.67 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;

namespace FLY.Thick.Base.OBJ_INTERFACE
{
    public class PROFILE_OBJ_INTERFACE
    {
        #region Pack
        public class Pack_StringList : IPack
        {
            public List<string> list;
            #region IPack 成员

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

                byte[] bs;

                buf.AddRange(BitConverter.GetBytes(list.Count()));
                for (int i = 0; i < list.Count(); i++)
                {
                    bs = Misc.Converter.StringToBytes(list[i]);
                    buf.AddRange(BitConverter.GetBytes(bs.Count()));
                    buf.AddRange(bs);
                }
                return buf.ToArray();
            }
            /// <summary>
            /// 返回由字节数组中指定位置的9个字节转换来的数据。
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public bool TryParse(byte[] value)
            {
                int cnt = 4;
                if (value.Length < cnt)
                    return false;

                int idx = 0;
                int len = BitConverter.ToInt32(value, idx);
                idx += 4;
                cnt += len * 4;
                if (value.Length < cnt)
                    return false;
                if (list == null)
                    list = new List<string>();
                list.Clear();
                for (int i = 0; i < len; i++)
                {
                    int l = BitConverter.ToInt32(value, idx);
                    idx += 4;
                    cnt += l;
                    if (value.Length < cnt)
                        return false;

                    string s = Misc.Converter.BytesToString(value, idx, l);
                    idx += l;
                    list.Add(s);
                }
                return true;
            }


            #endregion
        }
        public class Pack_String
        {
            public string data;
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                if (data != null)
                {
                    byte[] bs = Misc.Converter.StringToBytes(data);
                    buf.AddRange(BitConverter.GetBytes(bs.Length));
                    buf.AddRange(bs);
                }
                else 
                {
                    buf.AddRange(BitConverter.GetBytes(0));
                }
                
                return buf.ToArray();
            }
            /// <summary>
            /// 返回由字节数组中指定位置的9个字节转换来的数据。
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public bool TryParse(byte[] value, int index, int count)
            {
                int cnt = 4;
                if (value.Length < cnt)
                    return false;

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

                cnt += len;
                if (value.Length < cnt)
                    return false;
                if (len <= 0)
                    data = null;
                else
                {
                    data = Misc.Converter.BytesToString(value, idx, len);
                    idx += len;
                }
                return true;
            }

            public bool TryParse(byte[] value) 
            {
                return TryParse(value, 0, value.Length);
            }
            #endregion
        }
        #endregion

        #region GetValue
        /// <summary>
        /// ProfileParam
        /// </summary>
        public const UInt16 GET_PARAMS = 0;
        #endregion
        #region SetValue
        /// <summary>
        /// ProfileParam
        /// </summary>
        public const UInt16 SET_PARAMS = 0;
        #endregion
        #region PushMsg
        /// <summary>
        /// ProfileParam
        /// </summary>
        public const UInt16 PUSH_PARAMS = 0;
        #endregion
        #region CallFunction
        /// <summary>
        /// request:null ;
        /// reponse:Pack_StringList
        /// </summary>
        public const UInt16 CALL_GETLIST = 0;
        /// <summary>
        /// request:Pack_String ;
        /// reponse:null
        /// </summary>
        public const UInt16 CALL_DEL = 1;
        /// <summary>
        /// request:Pack_String ;
        /// reponse:ProfileParam
        /// </summary>
        public const UInt16 CALL_READ = 2;
        #endregion
    }
}